From a335fcd9c49407e3e75a7effd46e0544ae7c2d36 Mon Sep 17 00:00:00 2001 From: JesseBrault0709 <62299747+JesseBrault0709@users.noreply.github.com> Date: Sat, 29 Jun 2024 18:11:28 +0200 Subject: [PATCH] Fixed saving of RecipeStarEntities. --- .../app/mealsmadeeasy/api/recipe/Recipe.java | 3 ++- .../api/recipe/RecipeEntity.java | 18 +++++++------ .../api/recipe/RecipeRepository.java | 4 +-- .../api/recipe/RecipeServiceImpl.java | 2 +- .../api/recipe/RecipeServiceTests.java | 27 +++++++++++++++++++ 5 files changed, 42 insertions(+), 12 deletions(-) diff --git a/src/main/java/app/mealsmadeeasy/api/recipe/Recipe.java b/src/main/java/app/mealsmadeeasy/api/recipe/Recipe.java index 6de85f8..5e0de08 100644 --- a/src/main/java/app/mealsmadeeasy/api/recipe/Recipe.java +++ b/src/main/java/app/mealsmadeeasy/api/recipe/Recipe.java @@ -1,6 +1,7 @@ package app.mealsmadeeasy.api.recipe; import app.mealsmadeeasy.api.recipe.comment.RecipeComment; +import app.mealsmadeeasy.api.recipe.star.RecipeStar; import app.mealsmadeeasy.api.user.User; import org.jetbrains.annotations.Nullable; @@ -14,7 +15,7 @@ public interface Recipe { String getTitle(); String getRawText(); User getOwner(); - Set getStarGazers(); + Set getStars(); boolean isPublic(); Set getViewers(); Set getComments(); diff --git a/src/main/java/app/mealsmadeeasy/api/recipe/RecipeEntity.java b/src/main/java/app/mealsmadeeasy/api/recipe/RecipeEntity.java index 2b3f0c2..4044173 100644 --- a/src/main/java/app/mealsmadeeasy/api/recipe/RecipeEntity.java +++ b/src/main/java/app/mealsmadeeasy/api/recipe/RecipeEntity.java @@ -2,6 +2,8 @@ package app.mealsmadeeasy.api.recipe; import app.mealsmadeeasy.api.recipe.comment.RecipeComment; import app.mealsmadeeasy.api.recipe.comment.RecipeCommentEntity; +import app.mealsmadeeasy.api.recipe.star.RecipeStar; +import app.mealsmadeeasy.api.recipe.star.RecipeStarEntity; import app.mealsmadeeasy.api.user.User; import app.mealsmadeeasy.api.user.UserEntity; import jakarta.persistence.*; @@ -40,8 +42,8 @@ public final class RecipeEntity implements Recipe { @JoinColumn(name = "owner_id", nullable = false) private UserEntity owner; - @OneToMany - private Set starGazers = new HashSet<>(); + @OneToMany(mappedBy = "recipe") + private Set stars = new HashSet<>(); @OneToMany(mappedBy = "recipe") private Set comments = new HashSet<>(); @@ -137,16 +139,16 @@ public final class RecipeEntity implements Recipe { } @Override - public Set getStarGazers() { - return Set.copyOf(this.starGazers); + public Set getStars() { + return Set.copyOf(this.stars); } - public Set getStarGazerEntities() { - return this.starGazers; + public Set getStarEntities() { + return this.stars; } - public void setStarGazers(Set starGazers) { - this.starGazers = starGazers; + public void setStarEntities(Set starGazers) { + this.stars = starGazers; } @Override diff --git a/src/main/java/app/mealsmadeeasy/api/recipe/RecipeRepository.java b/src/main/java/app/mealsmadeeasy/api/recipe/RecipeRepository.java index b7bba04..604d3a8 100644 --- a/src/main/java/app/mealsmadeeasy/api/recipe/RecipeRepository.java +++ b/src/main/java/app/mealsmadeeasy/api/recipe/RecipeRepository.java @@ -11,6 +11,6 @@ public interface RecipeRepository extends JpaRepository { List findAllByViewersContaining(UserEntity viewer); List findAllByOwner(UserEntity owner); - @Query("SELECT r FROM Recipe r WHERE size(r.starGazers) > ?1") - List findAllByStarGazersGreaterThanEqual(long stars); + @Query("SELECT r FROM Recipe r WHERE size(r.stars) >= ?1") + List findAllByStarsGreaterThanEqual(long stars); } diff --git a/src/main/java/app/mealsmadeeasy/api/recipe/RecipeServiceImpl.java b/src/main/java/app/mealsmadeeasy/api/recipe/RecipeServiceImpl.java index 89ecb24..cf629a7 100644 --- a/src/main/java/app/mealsmadeeasy/api/recipe/RecipeServiceImpl.java +++ b/src/main/java/app/mealsmadeeasy/api/recipe/RecipeServiceImpl.java @@ -71,7 +71,7 @@ public final class RecipeServiceImpl implements RecipeService { @Override public List getByMinimumStars(long minimumStars) { - return List.copyOf(this.recipeRepository.findAllByStarGazersGreaterThanEqual(minimumStars)); + return List.copyOf(this.recipeRepository.findAllByStarsGreaterThanEqual(minimumStars)); } @Override diff --git a/src/test/java/app/mealsmadeeasy/api/recipe/RecipeServiceTests.java b/src/test/java/app/mealsmadeeasy/api/recipe/RecipeServiceTests.java index d9868d8..f1be083 100644 --- a/src/test/java/app/mealsmadeeasy/api/recipe/RecipeServiceTests.java +++ b/src/test/java/app/mealsmadeeasy/api/recipe/RecipeServiceTests.java @@ -8,6 +8,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.annotation.DirtiesContext; +import java.util.List; + import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; @@ -52,4 +54,29 @@ public class RecipeServiceTests { assertThat(byId.getRawText(), is("Hello!")); } + @Test + @DirtiesContext + public void getByMinimumStars() throws RecipeException { + final User owner = this.createTestUser("recipeOwner"); + 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); + + // r0.stars = 0, r1.stars = 1, r2.stars = 2 + this.recipeService.addStar(r1, u0); + this.recipeService.addStar(r2, u0); + this.recipeService.addStar(r2, u1); + + final List zeroStars = this.recipeService.getByMinimumStars(0); + final List oneStar = this.recipeService.getByMinimumStars(1); + final List twoStars = this.recipeService.getByMinimumStars(2); + + assertThat(zeroStars.size(), is(3)); + assertThat(oneStar.size(), is(2)); + assertThat(twoStars.size(), is(1)); + } + }