diff --git a/server/src/main/java/server/database/RecipeRepository.java b/server/src/main/java/server/database/RecipeRepository.java index 60ec130..8657873 100644 --- a/server/src/main/java/server/database/RecipeRepository.java +++ b/server/src/main/java/server/database/RecipeRepository.java @@ -19,4 +19,6 @@ import org.springframework.data.jpa.repository.JpaRepository; import commons.Recipe; -public interface RecipeRepository extends JpaRepository {} \ No newline at end of file +public interface RecipeRepository extends JpaRepository { + boolean existsByName(String name); +} \ No newline at end of file diff --git a/server/src/main/java/service/RecipeService.java b/server/src/main/java/service/RecipeService.java new file mode 100644 index 0000000..57cdf16 --- /dev/null +++ b/server/src/main/java/service/RecipeService.java @@ -0,0 +1,87 @@ +package service; + +import commons.Recipe; +import org.springframework.data.domain.PageRequest; +import org.springframework.stereotype.Service; +import server.database.IngredientRepository; +import server.database.RecipeIngredientRepository; +import server.database.RecipeRepository; + +import java.util.List; +import java.util.Optional; + +@Service +public class RecipeService { + RecipeRepository recipeRepository; + IngredientRepository ingredientRepository; + RecipeIngredientRepository recipeIngredientRepository; + + public RecipeService(RecipeRepository recipeRepository, + IngredientRepository ingredientRepository, + RecipeIngredientRepository recipeIngredientRepository) { + this.recipeRepository = recipeRepository; + this.ingredientRepository = ingredientRepository; + this.recipeIngredientRepository = recipeIngredientRepository; + } + + Optional findById(Long id) { + return recipeRepository.findById(id); + } + + List findAll() { + return recipeRepository.findAll(); + } + + List findAll(int limit) { + return recipeRepository.findAll(PageRequest.of(0, limit)).toList(); + } + + /** + * Creates a new recipe. Returns empty if the recipe with the same name already exists. + * @param recipe Recipe to be saved in the db. + * @return The created recipe (the recipe arg with a new assigned id). + */ + public Optional create(Recipe recipe) { + if (recipeRepository.existsByName(recipe.getName())) { + return Optional.empty(); + } + + return Optional.of(saveWithDependencies(recipe)); + } + + /** + * Updates a recipe. The recipe with the provided id will be replaced with the provided recipe. + * Automatically updates ingredients and any dependencies of recipe. + * @param id id of the recipe to update. + * @param recipe Recipe to be saved in the db. + * @return The created recipe (the recipe arg with a new assigned id.) + */ + public Optional update(Long id, Recipe recipe) { + assert id.equals(recipe.getId()) : "The id of the updated recipe doesn't match the provided recipes id."; + if (!recipeRepository.existsById(id)) { + return Optional.empty(); + } + + return Optional.of(saveWithDependencies(recipe)); + } + + public void delete(Long id) { + // TODO: Propagate deletion to ingredients. + recipeRepository.deleteById(id); + } + + private Recipe saveWithDependencies(Recipe recipe) { + // TODO: try to automate this with JFX somehow. + recipe.getIngredients() + .forEach(recipeIngredient -> + recipeIngredient.setIngredient( + ingredientRepository + .findByName(recipeIngredient.getIngredient().name) + .orElseGet(() -> ingredientRepository.save(recipeIngredient.getIngredient()) + )) + ); + recipeIngredientRepository.saveAll(recipe.getIngredients()); + return recipeRepository.save(recipe); + } + +}