Fixed saving of RecipeStarEntities.

This commit is contained in:
JesseBrault0709 2024-06-29 18:11:28 +02:00
parent b10451203b
commit a335fcd9c4
5 changed files with 42 additions and 12 deletions

View File

@ -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<User> getStarGazers();
Set<RecipeStar> getStars();
boolean isPublic();
Set<User> getViewers();
Set<RecipeComment> getComments();

View File

@ -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<UserEntity> starGazers = new HashSet<>();
@OneToMany(mappedBy = "recipe")
private Set<RecipeStarEntity> stars = new HashSet<>();
@OneToMany(mappedBy = "recipe")
private Set<RecipeCommentEntity> comments = new HashSet<>();
@ -137,16 +139,16 @@ public final class RecipeEntity implements Recipe {
}
@Override
public Set<User> getStarGazers() {
return Set.copyOf(this.starGazers);
public Set<RecipeStar> getStars() {
return Set.copyOf(this.stars);
}
public Set<UserEntity> getStarGazerEntities() {
return this.starGazers;
public Set<RecipeStarEntity> getStarEntities() {
return this.stars;
}
public void setStarGazers(Set<UserEntity> starGazers) {
this.starGazers = starGazers;
public void setStarEntities(Set<RecipeStarEntity> starGazers) {
this.stars = starGazers;
}
@Override

View File

@ -11,6 +11,6 @@ public interface RecipeRepository extends JpaRepository<RecipeEntity, Long> {
List<RecipeEntity> findAllByViewersContaining(UserEntity viewer);
List<RecipeEntity> findAllByOwner(UserEntity owner);
@Query("SELECT r FROM Recipe r WHERE size(r.starGazers) > ?1")
List<RecipeEntity> findAllByStarGazersGreaterThanEqual(long stars);
@Query("SELECT r FROM Recipe r WHERE size(r.stars) >= ?1")
List<RecipeEntity> findAllByStarsGreaterThanEqual(long stars);
}

View File

@ -71,7 +71,7 @@ public final class RecipeServiceImpl implements RecipeService {
@Override
public List<Recipe> getByMinimumStars(long minimumStars) {
return List.copyOf(this.recipeRepository.findAllByStarGazersGreaterThanEqual(minimumStars));
return List.copyOf(this.recipeRepository.findAllByStarsGreaterThanEqual(minimumStars));
}
@Override

View File

@ -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<Recipe> zeroStars = this.recipeService.getByMinimumStars(0);
final List<Recipe> oneStar = this.recipeService.getByMinimumStars(1);
final List<Recipe> twoStars = this.recipeService.getByMinimumStars(2);
assertThat(zeroStars.size(), is(3));
assertThat(oneStar.size(), is(2));
assertThat(twoStars.size(), is(1));
}
}