diff --git a/commons/src/main/java/commons/Recipe.java b/commons/src/main/java/commons/Recipe.java index 0a8cfaa..6ee2497 100644 --- a/commons/src/main/java/commons/Recipe.java +++ b/commons/src/main/java/commons/Recipe.java @@ -83,7 +83,7 @@ public class Recipe { private List preparationSteps = new ArrayList<>(); @SuppressWarnings("unused") - private Recipe() { + public Recipe() { // for object mapper } diff --git a/server/src/main/java/server/api/RecipeController.java b/server/src/main/java/server/api/RecipeController.java index 09fbe56..9b1753e 100644 --- a/server/src/main/java/server/api/RecipeController.java +++ b/server/src/main/java/server/api/RecipeController.java @@ -2,6 +2,7 @@ package server.api; import commons.Recipe; +import org.springframework.data.domain.Example; import org.springframework.data.domain.PageRequest; import org.springframework.http.ResponseEntity; @@ -86,11 +87,20 @@ public class RecipeController { * Inserts a new recipe into the repository *

* @param recipe The new recipe as a request body - * @return 200 OK with the recipe you added; or 400 Bad Request if the recipe already exists + * @return 200 OK with the recipe you added; or 400 Bad Request if the recipe already exists by name */ @PutMapping("/recipe/new") public ResponseEntity createRecipe(@RequestBody Recipe recipe) { - if (recipeRepository.existsById(recipe.getId())) { + + // We initialize a new example recipe with the name of input recipe + // This is the only attribute we are concerned about making sure it's unique + Recipe example = new Recipe(); + example.setName(recipe.getName()); + + /* Here we use very funny JPA magic repository.exists(Example) + We check if any recipe in the repository has the same name as the input + */ + if (recipeRepository.exists(Example.of(example))) { return ResponseEntity.badRequest().build(); }