From 16c8d4c6dce5530a1b0ead53410907f6be069490 Mon Sep 17 00:00:00 2001 From: Aysegul Date: Thu, 8 Jan 2026 22:06:45 +0100 Subject: [PATCH] fixed todo delete progatation in recipe added count to IngredientController about what recipes we have or dont and 2 small typo mistakes also added counting ingredients to IngredientService --- commons/src/main/java/commons/Recipe.java | 3 +- .../java/server/api/IngredientController.java | 28 +++++++++++++------ .../server/service/IngredientService.java | 26 +++++++++++++---- 3 files changed, 43 insertions(+), 14 deletions(-) diff --git a/commons/src/main/java/commons/Recipe.java b/commons/src/main/java/commons/Recipe.java index 1ae1f83..b8cfc3d 100644 --- a/commons/src/main/java/commons/Recipe.java +++ b/commons/src/main/java/commons/Recipe.java @@ -15,6 +15,7 @@ */ package commons; +import jakarta.persistence.CascadeType; import jakarta.persistence.CollectionTable; import jakarta.persistence.Column; import jakarta.persistence.ElementCollection; @@ -60,7 +61,7 @@ public class Recipe { // | 1 (Steak) | 40g pepper | // | 1 (Steak) | Meat | // |----------------------------------| - @OneToMany + @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) @CollectionTable(name = "recipe_ingredients", joinColumns = @JoinColumn(name = "recipe_id")) @Column(name = "ingredient") // TODO: Replace String with Embeddable Ingredient Class diff --git a/server/src/main/java/server/api/IngredientController.java b/server/src/main/java/server/api/IngredientController.java index 324276b..4fec684 100644 --- a/server/src/main/java/server/api/IngredientController.java +++ b/server/src/main/java/server/api/IngredientController.java @@ -1,5 +1,6 @@ package server.api; + import commons.Ingredient; import commons.ws.Topics; import commons.ws.messages.CreateIngredientMessage; @@ -93,13 +94,22 @@ public class IngredientController { * * @see Ingredient */ - @GetMapping("/ingredients/{id}") - public ResponseEntity getIngredientById(@PathVariable Long id) { - return ingredientService.findById(id) - .map(ResponseEntity::ok) - .orElseGet(() -> ResponseEntity.notFound().build()); + @GetMapping("/ingredients/{id}/usage") + public ResponseEntity getIngredientUsage(@PathVariable Long id) { + if (ingredientService.findById(id).isEmpty()) { + return ResponseEntity.notFound().build(); + } + + // Server-side computation of how many recipes reference this ingredient + long usedInRecipes = ingredientService.countUsage(id); + return ResponseEntity.ok(new IngredientUsageResponse(id, usedInRecipes)); } + + + + + /** * Update an existing ingredient by its ID. * Maps to PATCH /api/ingredients/{id} @@ -126,7 +136,7 @@ public class IngredientController { updated.setId(id); return ingredientService.update(id, updated) .map(saved -> { - messagingTemplate.convertAndSend(Topics.INGREDIENTS, new CreateIngredientMessage(saved)); + messagingTemplate.convertAndSend(Topics.INGREDIENTS, new UpdateIngredientMessage(saved)); return ResponseEntity.ok(saved); }) .orElseGet(() -> ResponseEntity.notFound().build()); @@ -139,7 +149,7 @@ public class IngredientController { *

* If an ingredient with the same name already exists, * returns 400 Bad Request. - * + *

* If the ingredient is created successfully, * returns the created ingredient with 200 OK. *

@@ -157,7 +167,7 @@ public class IngredientController { return ingredientService.create(ingredient) .map(saved -> { - messagingTemplate.convertAndSend(Topics.INGREDIENTS, new UpdateIngredientMessage(saved)); + messagingTemplate.convertAndSend(Topics.INGREDIENTS, new CreateIngredientMessage(saved)); return ResponseEntity.ok(saved); }) .orElseGet(() -> ResponseEntity.badRequest().build()); @@ -186,4 +196,6 @@ public class IngredientController { messagingTemplate.convertAndSend(Topics.INGREDIENTS, new DeleteIngredientMessage(id)); return ResponseEntity.ok(true); } + + public record IngredientUsageResponse(Long ingredientId, long usedInRecipes){} } diff --git a/server/src/main/java/server/service/IngredientService.java b/server/src/main/java/server/service/IngredientService.java index 8a79659..37cb76c 100644 --- a/server/src/main/java/server/service/IngredientService.java +++ b/server/src/main/java/server/service/IngredientService.java @@ -4,16 +4,21 @@ import commons.Ingredient; import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Service; import server.database.IngredientRepository; +import server.database.RecipeIngredientRepository; import java.util.List; import java.util.Optional; @Service public class IngredientService { - IngredientRepository ingredientRepository; - public IngredientService(IngredientRepository ingredientRepository) { + private final IngredientRepository ingredientRepository; + private final RecipeIngredientRepository recipeIngredientRepository; + + public IngredientService(IngredientRepository ingredientRepository, + RecipeIngredientRepository recipeIngredientRepository) { this.ingredientRepository = ingredientRepository; + this.recipeIngredientRepository = recipeIngredientRepository; } public Optional findById(Long id) { @@ -30,12 +35,16 @@ public class IngredientService { /** * Creates a new ingredient. Returns empty if the recipe with the same name or id already exists. + * * @param ingredient Ingredient to be saved in the db. * @return The created ingredient (the ingredient arg with a new assigned id) or empty if it already exists in db. */ public Optional create(Ingredient ingredient) { - if (ingredientRepository.existsByName(ingredient.getName()) || - ingredientRepository.existsById(ingredient.getId())) { + if (ingredient == null || ingredient.getName() == null) { + return Optional.empty(); + } + + if (ingredientRepository.existsByName(ingredient.getName())) { return Optional.empty(); } @@ -44,7 +53,8 @@ public class IngredientService { /** * Updates an ingredient. The ingredient with the provided id will be replaced (in db) with the provided ingredient. - * @param id id of the ingredient to update. + * + * @param id id of the ingredient to update. * @param ingredient Ingredient to be saved in the db. * @return The created ingredient (the ingredient arg with a new assigned id.) */ @@ -64,4 +74,10 @@ public class IngredientService { return true; } + //actually counting the amount used in recipes + + public long countUsage(long ingredientId) { + return recipeIngredientRepository.countByIngredientId(ingredientId); + + } }