Made delete() return a deepcopy of recipe.

This commit is contained in:
Oskar Rasieński 2026-01-16 19:11:48 +01:00
commit face654aa5

View file

@ -75,11 +75,28 @@ public class RecipeService {
return Optional.of(saveWithDependencies(recipe));
}
public boolean delete(Long id) {
/**
* Deletes a recipe by id and returns the deleted object.
* @param id id of the recipe to delete.
* @return The deleted recipe (deep copy).
*/
public Optional<Recipe> delete(Long id) {
// TODO: Propagate deletion to ingredients.
if (!recipeRepository.existsById(id)) return false;
Optional<Recipe> recipe = recipeRepository.findById(id);
if (recipe.isEmpty()) return Optional.empty();
// Make deep copy before removal. ( Had some lazy loading issues otherwise )
Recipe r = recipe.get();
Recipe copy = new Recipe(
r.getId(),
r.getName(),
r.getLocale(),
List.copyOf(r.getIngredients()),
List.copyOf(r.getPreparationSteps())
);
recipeRepository.deleteById(id);
return true;
return Optional.of(copy);
}
private Recipe saveWithDependencies(Recipe recipe) {