Created recipe service.
This commit is contained in:
parent
b01d313c9a
commit
299385c7b1
2 changed files with 90 additions and 1 deletions
|
|
@ -19,4 +19,6 @@ import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
import commons.Recipe;
|
import commons.Recipe;
|
||||||
|
|
||||||
public interface RecipeRepository extends JpaRepository<Recipe, Long> {}
|
public interface RecipeRepository extends JpaRepository<Recipe, Long> {
|
||||||
|
boolean existsByName(String name);
|
||||||
|
}
|
||||||
87
server/src/main/java/service/RecipeService.java
Normal file
87
server/src/main/java/service/RecipeService.java
Normal file
|
|
@ -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<Recipe> findById(Long id) {
|
||||||
|
return recipeRepository.findById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Recipe> findAll() {
|
||||||
|
return recipeRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Recipe> 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<Recipe> 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<Recipe> 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue