62 lines
1.8 KiB
Java
62 lines
1.8 KiB
Java
|
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);
|
||
|
}
|
||
|
}
|