This repository has been archived on 2024-06-18. You can view files and clone it, but cannot push or open issues or pull requests.

62 lines
1.8 KiB
Java
Raw Normal View History

2022-08-06 19:09:20 -04:00
package com.wyattjmiller.home.recipefoliobackend.controller;
import java.util.List;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.PathVariable;
import com.wyattjmiller.home.recipefoliobackend.model.Recipe;
@RestController
public class RecipeController {
private final RecipeRepository repository;
public RecipeController(RecipeRepository repository) {
this.repository = repository;
}
@GetMapping("/recipe")
List<Recipe> GetAllRecipes() {
return repository.findAll();
}
@GetMapping("/recipe/{id}")
Recipe GetOneRecipe(@PathVariable long id) {
return repository.findById(id).get();
}
@PostMapping("/recipe")
@ResponseStatus(HttpStatus.CREATED)
Recipe PostRecipe(@RequestBody Recipe recipe) {
return repository.save(recipe);
}
@PutMapping("/recipe/{id}")
Recipe PutRecipe(@RequestBody Recipe recipe, @PathVariable long id) {
return repository.findById(id)
.map(r -> {
r.setName(recipe.getName());
r.setDescription(recipe.getDescription());
r.setIngredients(recipe.getIngredients());
return repository.save(r);
})
.orElseGet(() -> {
recipe.setId(id);
return repository.save(recipe);
});
}
@DeleteMapping("/recipe/{id}")
void DeleteRecipe(@PathVariable long id) {
repository.deleteById(id);
}
}