Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- RecipeController.java
- package recipes.presentation;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.http.HttpStatus;
- import org.springframework.web.bind.annotation.*;
- import recipes.buissnesslayer.Recipe;
- import recipes.buissnesslayer.RecipeService;
- import javax.transaction.Transactional;
- import java.util.Map;
- @RestController
- public class RecipeController {
- @Autowired
- private RecipeService recipeService;
- @PostMapping("/api/recipe/new")
- public Map<String, Long> postRecipe(@RequestBody Recipe recipe) {
- recipeService.save(recipe);
- return Map.of("id", recipe.getId());
- }
- @GetMapping("/api/recipe/{id}")
- public Recipe getRecipe(@PathVariable long id) {
- return recipeService.findRecipeById(id);
- }
- @DeleteMapping("/api/recipe/{id}")
- public HttpStatus deleteRecipe(@PathVariable Long id) {
- recipeService.deleteRecipeById(id);
- return HttpStatus.NO_CONTENT;
- }
- }
- RecipeService.java
- package recipes.buissnesslayer;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import recipes.persistence.RecipeRepository;
- import javax.transaction.Transactional;
- @Service
- public class RecipeService {
- private final RecipeRepository recipeRepository;
- @Autowired
- public RecipeService(RecipeRepository recipeRepository) {
- this.recipeRepository = recipeRepository;
- }
- public Recipe findRecipeById(long id) {
- return recipeRepository.findRecipeById(id);
- }
- public void save(Recipe savedRecipe) {
- recipeRepository.save(savedRecipe);
- }
- public boolean existsById(Long id) { return recipeRepository.existsById(id); }
- public void deleteRecipeById(Long id) {
- recipeRepository.deleteRecipeById(id);
- }
- }
- RecipeRepository.java
- package recipes.persistence;
- import org.springframework.data.repository.CrudRepository;
- import org.springframework.stereotype.Repository;
- import recipes.buissnesslayer.Recipe;
- @Repository
- public interface RecipeRepository extends CrudRepository<Recipe, Long> {
- Recipe findRecipeById(long id);
- @Override
- boolean existsById(Long aLong);
- Recipe deleteRecipeById(Long id);
- }
- Recipe.java
- package recipes.buissnesslayer;
- import com.fasterxml.jackson.annotation.JsonIgnore;
- import javax.persistence.*;
- import javax.validation.constraints.NotEmpty;
- import java.util.List;
- @Entity
- public class Recipe {
- @Id
- @GeneratedValue
- @JsonIgnore
- private Long id;
- @NotEmpty
- private String name;
- @NotEmpty
- private String description;
- @ElementCollection
- private List<String> ingredients;
- @ElementCollection
- private List<String> directions;
- public Long getId() { return id; }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getDescription() {
- return description;
- }
- public void setDescription(String description) {
- this.description = description;
- }
- public List<String> getIngredients() {
- return ingredients;
- }
- public void setIngredients(List<String> ingredients) {
- this.ingredients = ingredients;
- }
- public List<String> getDirections() {
- return directions;
- }
- public void setDirections(List<String> directions) {
- this.directions = directions;
- }
- public Recipe(String name, String description, List<String> ingredients, List<String> directions) {
- this.name = name;
- this.description = description;
- this.ingredients = ingredients;
- this.directions = directions;
- }
- public Recipe() {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement