Skip to content
Snippets Groups Projects

Ingrediens api

Merged Gunnar Antoni Solli Olsen requested to merge Ingrediens-API into main
9 files
+ 263
7
Compare changes
  • Side-by-side
  • Inline
Files
9
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