Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
Backend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD 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 Scrum Team 02
Backend
Merge requests
!6
Ingrediens api
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Ingrediens api
Ingrediens-API
into
main
Overview
0
Commits
7
Pipelines
1
Changes
9
Merged
Gunnar Antoni Solli Olsen
requested to merge
Ingrediens-API
into
main
2 years ago
Overview
0
Commits
7
Pipelines
1
Changes
9
Expand
0
0
Merge request reports
Compare
main
main (base)
and
latest version
latest version
9bb00697
7 commits,
2 years ago
9 files
+
263
−
7
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
9
Search (e.g. *.vue) (Ctrl+P)
src/main/java/edu/ntnu/idatt210602/matsvinnbackend/controller/RecipeController.java
0 → 100644
+
62
−
0
Options
package
edu.ntnu.idatt210602.matsvinnbackend.controller
;
import
edu.ntnu.idatt210602.matsvinnbackend.model.Recipe
;
import
edu.ntnu.idatt210602.matsvinnbackend.repo.RecipeRepository
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.server.ResponseStatusException
;
import
java.util.List
;
@Controller
@CrossOrigin
@RequestMapping
(
path
=
"/recipe"
)
public
class
RecipeController
{
//TODO: logging?
@Autowired
private
RecipeRepository
recipeRepo
;
/**
* Get all recipes
*
* This endpoint responds with a list
* of all recipes in the recipe database
* throws 404 exception if none found
* @return the list of all recipes
*/
@GetMapping
(
"/"
)
@ResponseStatus
(
code
=
HttpStatus
.
OK
)
public
@ResponseBody
List
<
Recipe
>
getRecipes
(){
List
<
Recipe
>
recipes
=
recipeRepo
.
findAll
();
/*
SHOULD RETURN EMPTY LIST, NOT EXCEPTION
if (recipes.isEmpty()) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}
*/
return
recipes
;
}
/**
* Gets single recipe by given id
*
* This endpoint responds with a recipe
*
* throws 404 exception if recipe found
*
* @param id - Recipe identification
* @return recipe with given id
*/
@GetMapping
(
"/{id}"
)
public
@ResponseBody
Recipe
getRecipe
(
@PathVariable
Integer
id
){
Recipe
recipe
=
recipeRepo
.
findById
(
id
).
orElseThrow
(()
->
{
return
new
ResponseStatusException
(
HttpStatus
.
NOT_FOUND
);
//.orElseThrow throws exception
//returned by lambda
});
return
recipe
;
}
}
Loading