From 6dd0d1483e74a4e6d704994c0cf872578b1de68d Mon Sep 17 00:00:00 2001 From: JesseBrault0709 <62299747+JesseBrault0709@users.noreply.github.com> Date: Sat, 29 Jun 2024 18:26:45 +0200 Subject: [PATCH] Added public/viewable check to Recipes for adding stars. --- .../api/recipe/RecipeException.java | 2 +- .../api/recipe/RecipeRepository.java | 5 ++++ .../api/recipe/RecipeService.java | 2 +- .../api/recipe/RecipeServiceImpl.java | 23 ++++++++++++++++--- .../api/recipe/RecipeServiceTests.java | 10 +++++--- 5 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/main/java/app/mealsmadeeasy/api/recipe/RecipeException.java b/src/main/java/app/mealsmadeeasy/api/recipe/RecipeException.java index 82fdb2d..12f6de8 100644 --- a/src/main/java/app/mealsmadeeasy/api/recipe/RecipeException.java +++ b/src/main/java/app/mealsmadeeasy/api/recipe/RecipeException.java @@ -3,7 +3,7 @@ package app.mealsmadeeasy.api.recipe; public class RecipeException extends Exception { public enum Type { - INVALID_OWNER_USERNAME, INVALID_STAR, INVALID_ID + INVALID_OWNER_USERNAME, INVALID_STAR, NOT_VIEWABLE, INVALID_ID } private final Type type; diff --git a/src/main/java/app/mealsmadeeasy/api/recipe/RecipeRepository.java b/src/main/java/app/mealsmadeeasy/api/recipe/RecipeRepository.java index 604d3a8..1c10ead 100644 --- a/src/main/java/app/mealsmadeeasy/api/recipe/RecipeRepository.java +++ b/src/main/java/app/mealsmadeeasy/api/recipe/RecipeRepository.java @@ -1,6 +1,7 @@ package app.mealsmadeeasy.api.recipe; import app.mealsmadeeasy.api.user.UserEntity; +import org.springframework.data.jpa.repository.EntityGraph; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; @@ -13,4 +14,8 @@ public interface RecipeRepository extends JpaRepository { @Query("SELECT r FROM Recipe r WHERE size(r.stars) >= ?1") List findAllByStarsGreaterThanEqual(long stars); + + @Query("SELECT r FROM Recipe r WHERE r.id = ?1") + @EntityGraph(attributePaths = { "viewers" }) + RecipeEntity getByIdWithViewers(long id); } diff --git a/src/main/java/app/mealsmadeeasy/api/recipe/RecipeService.java b/src/main/java/app/mealsmadeeasy/api/recipe/RecipeService.java index f455b51..dbfce66 100644 --- a/src/main/java/app/mealsmadeeasy/api/recipe/RecipeService.java +++ b/src/main/java/app/mealsmadeeasy/api/recipe/RecipeService.java @@ -22,7 +22,7 @@ public interface RecipeService { Recipe updateOwner(Recipe recipe, String newOwnerUsername) throws RecipeException; - RecipeStar addStar(Recipe recipe, User giver); + RecipeStar addStar(Recipe recipe, User giver) throws RecipeException; void deleteStarByUser(Recipe recipe, User giver) throws RecipeException; void deleteStar(RecipeStar recipeStar); diff --git a/src/main/java/app/mealsmadeeasy/api/recipe/RecipeServiceImpl.java b/src/main/java/app/mealsmadeeasy/api/recipe/RecipeServiceImpl.java index cf629a7..4ba72b7 100644 --- a/src/main/java/app/mealsmadeeasy/api/recipe/RecipeServiceImpl.java +++ b/src/main/java/app/mealsmadeeasy/api/recipe/RecipeServiceImpl.java @@ -120,11 +120,28 @@ public final class RecipeServiceImpl implements RecipeService { } @Override - public RecipeStar addStar(Recipe recipe, User giver) { - final RecipeEntity entity = (RecipeEntity) recipe; + public RecipeStar addStar(Recipe recipe, User giver) throws RecipeException { + boolean viewable = false; + if (recipe.isPublic()) { + viewable = true; + } else { + final RecipeEntity withViewers = this.recipeRepository.getByIdWithViewers(recipe.getId()); + for (final var viewer : withViewers.getViewers()) { + if (viewer.getId() != null && viewer.getId().equals(giver.getId())) { + viewable = true; + break; + } + } + } + if (!viewable) { + throw new RecipeException( + RecipeException.Type.NOT_VIEWABLE, + "Recipe with id " + recipe.getId() + " is not viewable by User with id " + giver.getId() + ); + } final RecipeStarEntity star = new RecipeStarEntity(); star.setOwner((UserEntity) giver); - star.setRecipe(entity); + star.setRecipe((RecipeEntity) recipe); return this.recipeStarRepository.save(star); } diff --git a/src/test/java/app/mealsmadeeasy/api/recipe/RecipeServiceTests.java b/src/test/java/app/mealsmadeeasy/api/recipe/RecipeServiceTests.java index f1be083..668b0b7 100644 --- a/src/test/java/app/mealsmadeeasy/api/recipe/RecipeServiceTests.java +++ b/src/test/java/app/mealsmadeeasy/api/recipe/RecipeServiceTests.java @@ -61,9 +61,13 @@ public class RecipeServiceTests { final User u0 = this.createTestUser("u0"); final User u1 = this.createTestUser("u1"); - final Recipe r0 = this.createTestRecipe(owner); - final Recipe r1 = this.createTestRecipe(owner); - final Recipe r2 = this.createTestRecipe(owner); + Recipe r0 = this.createTestRecipe(owner); + Recipe r1 = this.createTestRecipe(owner); + Recipe r2 = this.createTestRecipe(owner); + + r0 = this.recipeService.setPublic(r0, true); + r1 = this.recipeService.setPublic(r1, true); + r2 = this.recipeService.setPublic(r2, true); // r0.stars = 0, r1.stars = 1, r2.stars = 2 this.recipeService.addStar(r1, u0);