diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/RecipeController.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/RecipeController.java new file mode 100644 index 0000000000000000000000000000000000000000..28ed7cf54d0c3d6ce1861a878b3bd04cd6da41a1 --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/RecipeController.java @@ -0,0 +1,56 @@ +package ntnu.idatt2016.v233.SmartMat.controller; + +import org.springframework.web.bind.annotation.RestController; + +import lombok.AllArgsConstructor; +import ntnu.idatt2016.v233.SmartMat.entity.Recipe; +import ntnu.idatt2016.v233.SmartMat.service.RecipeService; + +import java.util.List; + +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +/** + * This class is responsible for handling requests related to recipes + * + * @author Stian Lyng + * @version 1.0 + */ +@AllArgsConstructor +@RestController +@RequestMapping("/api/recipe") +public class RecipeController { + + /** + * The recipe service + */ + private final RecipeService recipeService; + + /** + * Gets a recipe by its id + * @param id the id of the recipe + * @return the recipe if it exists, otherwise 404 + */ + @GetMapping("/{id}") + public ResponseEntity<Recipe> getRecipeById(@PathVariable("id") Long id) { + return recipeService.getRecipeById(id) + .map(ResponseEntity::ok) + .orElseGet(() -> ResponseEntity.notFound().build()); + } + + /** + * Gets all recipes with a given name + * + * @param name the name of the recipe + * @return a list of recipes with the given name, otherwise 404 + */ + @GetMapping("/name/{name}") + public ResponseEntity<List<Recipe>> getRecipeByName(@PathVariable("name") String name) { + List<Recipe> recipes = recipeService.getRecipesByName(name); + if (recipes.isEmpty()) { + return ResponseEntity.notFound().build(); + } + return ResponseEntity.ok(recipes); + } +} diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/dto/request/RecipeRequest.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/dto/request/RecipeRequest.java new file mode 100644 index 0000000000000000000000000000000000000000..fcc9451ce21396ce2fe067869d2d19f93fc30e1b --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/dto/request/RecipeRequest.java @@ -0,0 +1,29 @@ +package ntnu.idatt2016.v233.SmartMat.dto.request; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; + +/** + * This class represents a recipe request + */ +@Data +@Builder +@AllArgsConstructor +public class RecipeRequest { + /** + * The unique id of the recipe + */ + private long id; + + /** + * The name of the recipe + */ + private String recipeName; + + /** + * The description of the recipe + */ + private String recipeDescription; + +} diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/RecipeRepository.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/RecipeRepository.java index 3682ef5aef8929f508865a15edfaece16f157d7b..1272ccf8b38b5936bcac5661d0ed9e1606bd5b56 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/RecipeRepository.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/RecipeRepository.java @@ -1,6 +1,6 @@ package ntnu.idatt2016.v233.SmartMat.repository; -import java.util.Optional; +import java.util.List; import ntnu.idatt2016.v233.SmartMat.entity.Recipe; import org.springframework.data.jpa.repository.JpaRepository; @@ -18,6 +18,6 @@ public interface RecipeRepository extends JpaRepository<Recipe, Long> { * @param name the name of the recipe * @return an optional containing the recipe if it exists */ - Optional<Recipe> getByName(String name); + List<Recipe> getByName(String name); } 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 diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/product/ProductService.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/product/ProductService.java index 6419db2063d4fa26ca1e3bdf66f8ef8963ef23f9..2155ade614420bca5186af4aa764007ecf8b4c3f 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/product/ProductService.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/product/ProductService.java @@ -15,6 +15,7 @@ import java.util.Optional; * @since 05.04.2023 */ public class ProductService { + ProductRepository productRepository;