Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
backend
Manage
Activity
Members
Plan
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
idatt2106-v23-03
backend
Commits
7f928e90
Commit
7f928e90
authored
2 years ago
by
Stian Lyng
Browse files
Options
Downloads
Patches
Plain Diff
create controller
parent
76287974
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/ntnu/idatt2016/v233/SmartMat/controller/RecipeController.java
+56
-0
56 additions, 0 deletions
.../idatt2016/v233/SmartMat/controller/RecipeController.java
with
56 additions
and
0 deletions
src/main/java/ntnu/idatt2016/v233/SmartMat/controller/RecipeController.java
0 → 100644
+
56
−
0
View file @
7f928e90
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
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment