Added getStar to RecipeController and related.
This commit is contained in:
parent
c467bd75b3
commit
f47e767612
@ -150,6 +150,23 @@ public class RecipeControllerTests {
|
||||
.andExpect(jsonPath("$.date").exists());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void getStarForRecipe() throws Exception {
|
||||
final User owner = this.createTestUser("recipe-owner");
|
||||
final User starer = this.createTestUser("recipe-starer");
|
||||
final Recipe recipe = this.createTestRecipe(owner, true);
|
||||
this.recipeStarService.create(recipe.getId(), starer.getUsername());
|
||||
this.mockMvc.perform(
|
||||
get("/recipes/{username}/{slug}/stars", recipe.getOwner().getUsername(), recipe.getSlug())
|
||||
.header("Authorization", "Bearer " + this.getAccessToken(starer))
|
||||
)
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.isStarred").value(true))
|
||||
.andExpect(jsonPath("$.star").isMap())
|
||||
.andExpect(jsonPath("$.star.date").exists());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void deleteStarFromRecipe() throws Exception {
|
||||
|
@ -1,11 +1,13 @@
|
||||
package app.mealsmadeeasy.api.recipe.star;
|
||||
|
||||
import app.mealsmadeeasy.api.recipe.Recipe;
|
||||
import app.mealsmadeeasy.api.recipe.RecipeException;
|
||||
import app.mealsmadeeasy.api.recipe.RecipeService;
|
||||
import app.mealsmadeeasy.api.recipe.spec.RecipeCreateSpec;
|
||||
import app.mealsmadeeasy.api.user.User;
|
||||
import app.mealsmadeeasy.api.user.UserCreateException;
|
||||
import app.mealsmadeeasy.api.user.UserService;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
@ -72,6 +74,21 @@ public class RecipeStarServiceTests {
|
||||
assertThat(star.getDate(), is(notNullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void find() throws RecipeException {
|
||||
final User owner = this.getTestUser("recipe-owner");
|
||||
final User starer = this.getTestUser("recipe-starer");
|
||||
final Recipe recipe = this.getTestRecipe(owner, "test-recipe", true);
|
||||
this.recipeStarService.create(recipe.getId(), starer.getUsername());
|
||||
final @Nullable RecipeStar star = this.recipeStarService.find(
|
||||
recipe.getOwner().getUsername(),
|
||||
recipe.getSlug(),
|
||||
starer
|
||||
).orElse(null);
|
||||
assertThat(star, is(notNullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void deleteViaUsernameAndSlug() {
|
||||
|
@ -6,6 +6,7 @@ import app.mealsmadeeasy.api.recipe.view.FullRecipeView;
|
||||
import app.mealsmadeeasy.api.recipe.view.RecipeExceptionView;
|
||||
import app.mealsmadeeasy.api.recipe.view.RecipeInfoView;
|
||||
import app.mealsmadeeasy.api.user.User;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Slice;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@ -74,6 +75,20 @@ public class RecipeController {
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(this.recipeStarService.create(username, slug, principal));
|
||||
}
|
||||
|
||||
@GetMapping("/{username}/{slug}/stars")
|
||||
public ResponseEntity<Map<String, Object>> getStar(
|
||||
@PathVariable String username,
|
||||
@PathVariable String slug,
|
||||
@AuthenticationPrincipal User principal
|
||||
) throws RecipeException {
|
||||
final @Nullable RecipeStar star = this.recipeStarService.find(username, slug, principal).orElse(null);
|
||||
if (star != null) {
|
||||
return ResponseEntity.ok(Map.of("isStarred", true, "star", star));
|
||||
} else {
|
||||
return ResponseEntity.ok(Map.of("isStarred", false));
|
||||
}
|
||||
}
|
||||
|
||||
@DeleteMapping("/{username}/{slug}/stars")
|
||||
public ResponseEntity<Object> removeStar(
|
||||
@PathVariable String username,
|
||||
|
@ -3,10 +3,14 @@ package app.mealsmadeeasy.api.recipe.star;
|
||||
import app.mealsmadeeasy.api.recipe.RecipeException;
|
||||
import app.mealsmadeeasy.api.user.User;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface RecipeStarService {
|
||||
RecipeStar create(long recipeId, String ownerUsername);
|
||||
RecipeStar create(String recipeOwnerUsername, String recipeSlug, User starer) throws RecipeException;
|
||||
RecipeStar get(long recipeId, String ownerUsername) throws RecipeException;
|
||||
|
||||
Optional<RecipeStar> find(String recipeOwnerUsername, String recipeSlug, User starer) throws RecipeException;
|
||||
|
||||
void delete(long recipeId, String ownerUsername);
|
||||
void delete(String recipeOwnerUsername, String recipeSlug, User starer) throws RecipeException;
|
||||
}
|
||||
|
@ -45,13 +45,10 @@ public class RecipeStarServiceImpl implements RecipeStarService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public RecipeStar get(long recipeId, String ownerUsername) throws RecipeException {
|
||||
return this.recipeStarRepository.findByRecipeIdAndOwnerUsername(recipeId, ownerUsername).orElseThrow(
|
||||
() -> new RecipeException(
|
||||
RecipeException.Type.INVALID_ID,
|
||||
"No such RecipeStar for recipeId: " + recipeId + " and ownerUsername: " + ownerUsername
|
||||
)
|
||||
);
|
||||
public Optional<RecipeStar> find(String recipeOwnerUsername, String recipeSlug, User starer) throws RecipeException {
|
||||
final Recipe recipe = this.recipeService.getByUsernameAndSlug(recipeOwnerUsername, recipeSlug, starer);
|
||||
return this.recipeStarRepository.findByRecipeIdAndOwnerUsername(recipe.getId(), starer.getUsername())
|
||||
.map(RecipeStar.class::cast);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user