From 04a999147ca940c409b387299bd51ac3b8d80333 Mon Sep 17 00:00:00 2001 From: Steven Liu Date: Mon, 22 Dec 2025 03:01:10 +0200 Subject: [PATCH] refactor(server): server-side integration of new recipe modelling --- .../java/server/api/IngredientController.java | 2 +- .../java/server/api/RecipeController.java | 25 ++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/server/src/main/java/server/api/IngredientController.java b/server/src/main/java/server/api/IngredientController.java index 531b8f3..946a62d 100644 --- a/server/src/main/java/server/api/IngredientController.java +++ b/server/src/main/java/server/api/IngredientController.java @@ -167,7 +167,7 @@ public class IngredientController { Ingredient example = new Ingredient(); example.name = ingredient.name; - if (ingredientRepository.exists(Example.of(example))) { + if (ingredientRepository.existsById(ingredient.id) || ingredientRepository.exists(Example.of(example))) { return ResponseEntity.badRequest().build(); } diff --git a/server/src/main/java/server/api/RecipeController.java b/server/src/main/java/server/api/RecipeController.java index b921bf0..3a479cb 100644 --- a/server/src/main/java/server/api/RecipeController.java +++ b/server/src/main/java/server/api/RecipeController.java @@ -1,5 +1,6 @@ package server.api; +import commons.Ingredient; import commons.Recipe; import commons.ws.Topics; @@ -21,6 +22,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import server.database.RecipeIngredientRepository; import server.database.RecipeRepository; import java.util.List; @@ -31,11 +33,17 @@ import java.util.Optional; public class RecipeController { private final RecipeRepository recipeRepository; // JPA repository used in this controller private final SimpMessagingTemplate messagingTemplate; + private final RecipeIngredientRepository recipeIngredientRepository; + private final IngredientController ingredientController; public RecipeController(RecipeRepository recipeRepository, - SimpMessagingTemplate messagingTemplate) { + SimpMessagingTemplate messagingTemplate, + IngredientController ingredientController, + RecipeIngredientRepository recipeIngredientRepository) { this.recipeRepository = recipeRepository; this.messagingTemplate = messagingTemplate; + this.recipeIngredientRepository = recipeIngredientRepository; + this.ingredientController = ingredientController; } /** @@ -75,16 +83,21 @@ public class RecipeController { /** * Mapping for POST /recipe/{id}. + * Also creates the ingredient elements if they do not exist. * @param id The recipe id to replace * @param recipe The new recipe to be replaced from the original * @return The changed recipe; returns 400 Bad Request if the recipe does not exist + * @see IngredientController#createIngredient(Ingredient) */ @PostMapping("/recipe/{id}") public ResponseEntity updateRecipe(@PathVariable Long id, @RequestBody Recipe recipe) { if (!recipeRepository.existsById(id)) { return ResponseEntity.badRequest().build(); } - + recipe.getIngredients().stream() + .map(recipeIngredient -> recipeIngredient.ingredient) + .forEach(ingredientController::createIngredient); + recipeIngredientRepository.saveAll(recipe.getIngredients()); Recipe saved = recipeRepository.save(recipe); messagingTemplate.convertAndSend(Topics.RECIPES, new UpdateRecipeMessage(saved)); @@ -93,11 +106,13 @@ public class RecipeController { /** * Mapping for PUT /recipe/new. + * Includes same transient object handling as the POST handler. *

* 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 by name + * @see RecipeController#updateRecipe(Long, Recipe) */ @PutMapping("/recipe/new") public ResponseEntity createRecipe(@RequestBody Recipe recipe) { @@ -113,7 +128,11 @@ public class RecipeController { if (recipeRepository.exists(Example.of(example))) { return ResponseEntity.badRequest().build(); } - + // FIXME reduce code duplication + recipe.getIngredients().stream() + .map(recipeIngredient -> recipeIngredient.ingredient) + .forEach(ingredientController::createIngredient); + recipeIngredientRepository.saveAll(recipe.getIngredients()); Recipe saved = recipeRepository.save(recipe); messagingTemplate.convertAndSend(Topics.RECIPES, new CreateRecipeMessage(saved));