diff --git a/src/integrationTest/java/app/mealsmadeeasy/api/recipe/RecipeControllerTests.java b/src/integrationTest/java/app/mealsmadeeasy/api/recipe/RecipeControllerTests.java index 1039e57..69a5c44 100644 --- a/src/integrationTest/java/app/mealsmadeeasy/api/recipe/RecipeControllerTests.java +++ b/src/integrationTest/java/app/mealsmadeeasy/api/recipe/RecipeControllerTests.java @@ -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 { diff --git a/src/integrationTest/java/app/mealsmadeeasy/api/recipe/star/RecipeStarServiceTests.java b/src/integrationTest/java/app/mealsmadeeasy/api/recipe/star/RecipeStarServiceTests.java index 77513be..a9c9d2e 100644 --- a/src/integrationTest/java/app/mealsmadeeasy/api/recipe/star/RecipeStarServiceTests.java +++ b/src/integrationTest/java/app/mealsmadeeasy/api/recipe/star/RecipeStarServiceTests.java @@ -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() { diff --git a/src/main/java/app/mealsmadeeasy/api/recipe/RecipeController.java b/src/main/java/app/mealsmadeeasy/api/recipe/RecipeController.java index 5bf3770..257590c 100644 --- a/src/main/java/app/mealsmadeeasy/api/recipe/RecipeController.java +++ b/src/main/java/app/mealsmadeeasy/api/recipe/RecipeController.java @@ -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> 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 removeStar( @PathVariable String username, diff --git a/src/main/java/app/mealsmadeeasy/api/recipe/star/RecipeStarService.java b/src/main/java/app/mealsmadeeasy/api/recipe/star/RecipeStarService.java index bf04e54..9444b5e 100644 --- a/src/main/java/app/mealsmadeeasy/api/recipe/star/RecipeStarService.java +++ b/src/main/java/app/mealsmadeeasy/api/recipe/star/RecipeStarService.java @@ -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 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; } diff --git a/src/main/java/app/mealsmadeeasy/api/recipe/star/RecipeStarServiceImpl.java b/src/main/java/app/mealsmadeeasy/api/recipe/star/RecipeStarServiceImpl.java index bdbe84a..c1c1ee6 100644 --- a/src/main/java/app/mealsmadeeasy/api/recipe/star/RecipeStarServiceImpl.java +++ b/src/main/java/app/mealsmadeeasy/api/recipe/star/RecipeStarServiceImpl.java @@ -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 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