Added public/viewable check to Recipes for adding stars.
This commit is contained in:
parent
a335fcd9c4
commit
6dd0d1483e
@ -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;
|
||||
|
@ -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<RecipeEntity, Long> {
|
||||
|
||||
@Query("SELECT r FROM Recipe r WHERE size(r.stars) >= ?1")
|
||||
List<RecipeEntity> findAllByStarsGreaterThanEqual(long stars);
|
||||
|
||||
@Query("SELECT r FROM Recipe r WHERE r.id = ?1")
|
||||
@EntityGraph(attributePaths = { "viewers" })
|
||||
RecipeEntity getByIdWithViewers(long id);
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user