From 7f928e90e6b33bbca30b14ef0c7a0d34ebd089a8 Mon Sep 17 00:00:00 2001
From: Stian Lyng <stianlyng@protonmail.com>
Date: Fri, 21 Apr 2023 11:43:33 +0200
Subject: [PATCH] create controller

---
 .../SmartMat/controller/RecipeController.java | 56 +++++++++++++++++++
 1 file changed, 56 insertions(+)
 create mode 100644 src/main/java/ntnu/idatt2016/v233/SmartMat/controller/RecipeController.java

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 00000000..28ed7cf5
--- /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);
+    }
+}
-- 
GitLab