diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/RecipeService.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/RecipeService.java new file mode 100644 index 0000000000000000000000000000000000000000..f72b9bb06addb33bf39b000ebf781a58cf14ffef --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/RecipeService.java @@ -0,0 +1,87 @@ +package ntnu.idatt2016.v233.SmartMat.service; + +import ntnu.idatt2016.v233.SmartMat.entity.Recipe; +import ntnu.idatt2016.v233.SmartMat.repository.RecipeRepository; + +import java.util.List; +import java.util.Optional; + +/** + * This class defines the methods for the recipe service + * + * @author Stian Lyng + * @version 1.0 + */ +public class RecipeService { + + /** + * The recipe repository + */ + private RecipeRepository recipeRepository; + + /** + * Creates a new recipe service + * @param recipeRepository + */ + public RecipeService (RecipeRepository recipeRepository) { + this.recipeRepository = recipeRepository; + } + + /** + * Gets a recipe by its id + * + * @param id the id of the recipe + * @return an optional containing the recipe if it exists + */ + public Optional<Recipe> getRecipeById(Long id) { + return recipeRepository.findById(id); + } + + /** + * Gets all recipes with a given name + * + * @param name the name of the recipe + * @return a list of recipes with the given name + */ + public List<Recipe> getRecipesByName(String name) { + return recipeRepository.getByName(name); + } + + /** + * Gets all recipes + * + * @return a list of all recipes + */ + public List<Recipe> getAllRecipes() { + return recipeRepository.findAll(); + } + + /** + * Saves a recipe + * + * @param recipe the recipe to save + * @return the saved recipe + */ + public Recipe saveRecipe(Recipe recipe) { + return recipeRepository.save(recipe); + } + + /** + * Deletes a recipe + * + * @param recipe a recipe object to delete + */ + public void deleteRecipe(Recipe recipe) { + recipeRepository.delete(recipe); + } + + /** + * Deletes a recipe by its id + * + * @param id the id of the recipe to delete + */ + public void deleteRecipeById(Long id) { + recipeRepository.deleteById(id); + } + +} \ No newline at end of file