Moved services
This commit is contained in:
parent
b7116a0882
commit
0050671944
2 changed files with 5 additions and 3 deletions
65
server/src/main/java/server/service/IngredientService.java
Normal file
65
server/src/main/java/server/service/IngredientService.java
Normal file
|
|
@ -0,0 +1,65 @@
|
||||||
|
package server.service;
|
||||||
|
|
||||||
|
import commons.Ingredient;
|
||||||
|
import org.springframework.data.domain.PageRequest;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import server.database.IngredientRepository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class IngredientService {
|
||||||
|
IngredientRepository ingredientRepository;
|
||||||
|
|
||||||
|
public IngredientService(IngredientRepository ingredientRepository) {
|
||||||
|
this.ingredientRepository = ingredientRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Ingredient> findById(Long id) {
|
||||||
|
return ingredientRepository.findById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Ingredient> findAll() {
|
||||||
|
return ingredientRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Ingredient> findAll(int limit) {
|
||||||
|
return ingredientRepository.findAll(PageRequest.of(0, limit)).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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<Ingredient> create(Ingredient ingredient) {
|
||||||
|
if (ingredientRepository.existsByName(ingredient.getName()) ||
|
||||||
|
ingredientRepository.existsById(ingredient.getId())) {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
return Optional.of(ingredientRepository.save(ingredient));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 ingredient Ingredient to be saved in the db.
|
||||||
|
* @return The created ingredient (the ingredient arg with a new assigned id.)
|
||||||
|
*/
|
||||||
|
public Optional<Ingredient> update(Long id, Ingredient ingredient) {
|
||||||
|
assert id.equals(ingredient.getId()) :
|
||||||
|
"The id of the updated ingredient doesn't match the provided ingredient's id.";
|
||||||
|
if (!ingredientRepository.existsById(id)) {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
return Optional.of(ingredientRepository.save(ingredient));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void delete(Long id) {
|
||||||
|
ingredientRepository.deleteById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
89
server/src/main/java/server/service/RecipeService.java
Normal file
89
server/src/main/java/server/service/RecipeService.java
Normal file
|
|
@ -0,0 +1,89 @@
|
||||||
|
package server.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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Recipe> findById(Long id) {
|
||||||
|
return recipeRepository.findById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Recipe> findAll() {
|
||||||
|
return recipeRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public 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) or empty if it already exists in db.
|
||||||
|
*/
|
||||||
|
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 boolean delete(Long id) {
|
||||||
|
// TODO: Propagate deletion to ingredients.
|
||||||
|
if (!recipeRepository.existsById(id)) return false;
|
||||||
|
recipeRepository.deleteById(id);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
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…
Reference in a new issue