Made delete() return a deepcopy of recipe.
This commit is contained in:
parent
cce24ab61d
commit
face654aa5
1 changed files with 20 additions and 3 deletions
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue