Advertisement
Guest User

Hyperskill Recipe Project

a guest
Feb 22nd, 2022
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.82 KB | None | 0 0
  1. RecipeController.java
  2. package recipes.presentation;
  3.  
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.http.HttpStatus;
  6. import org.springframework.web.bind.annotation.*;
  7. import recipes.buissnesslayer.Recipe;
  8. import recipes.buissnesslayer.RecipeService;
  9.  
  10. import javax.transaction.Transactional;
  11. import java.util.Map;
  12.  
  13. @RestController
  14. public class RecipeController {
  15.  
  16.     @Autowired
  17.     private RecipeService recipeService;
  18.  
  19.     @PostMapping("/api/recipe/new")
  20.     public Map<String, Long> postRecipe(@RequestBody Recipe recipe) {
  21.         recipeService.save(recipe);
  22.  
  23.         return Map.of("id", recipe.getId());
  24.     }
  25.  
  26.     @GetMapping("/api/recipe/{id}")
  27.     public Recipe getRecipe(@PathVariable long id) {
  28.         return recipeService.findRecipeById(id);
  29.     }
  30.  
  31.     @DeleteMapping("/api/recipe/{id}")
  32.     public HttpStatus deleteRecipe(@PathVariable Long id) {
  33.         recipeService.deleteRecipeById(id);
  34.         return HttpStatus.NO_CONTENT;
  35.     }
  36. }
  37.  
  38.  
  39. RecipeService.java
  40. package recipes.buissnesslayer;
  41.  
  42. import org.springframework.beans.factory.annotation.Autowired;
  43. import org.springframework.stereotype.Service;
  44. import recipes.persistence.RecipeRepository;
  45.  
  46. import javax.transaction.Transactional;
  47.  
  48.  
  49. @Service
  50. public class RecipeService {
  51.     private final RecipeRepository recipeRepository;
  52.  
  53.     @Autowired
  54.     public RecipeService(RecipeRepository recipeRepository) {
  55.         this.recipeRepository = recipeRepository;
  56.     }
  57.  
  58.     public Recipe findRecipeById(long id) {
  59.         return recipeRepository.findRecipeById(id);
  60.     }
  61.  
  62.     public void save(Recipe savedRecipe) {
  63.         recipeRepository.save(savedRecipe);
  64.     }
  65.  
  66.     public boolean existsById(Long id) { return recipeRepository.existsById(id); }
  67.  
  68.     public void deleteRecipeById(Long id) {
  69.         recipeRepository.deleteRecipeById(id);
  70.     }
  71. }
  72.  
  73. RecipeRepository.java
  74. package recipes.persistence;
  75.  
  76. import org.springframework.data.repository.CrudRepository;
  77. import org.springframework.stereotype.Repository;
  78. import recipes.buissnesslayer.Recipe;
  79.  
  80. @Repository
  81. public interface RecipeRepository extends CrudRepository<Recipe, Long> {
  82.     Recipe findRecipeById(long id);
  83.  
  84.     @Override
  85.     boolean existsById(Long aLong);
  86.  
  87.     Recipe deleteRecipeById(Long id);
  88. }
  89.  
  90.  
  91. Recipe.java
  92. package recipes.buissnesslayer;
  93.  
  94.  
  95. import com.fasterxml.jackson.annotation.JsonIgnore;
  96.  
  97. import javax.persistence.*;
  98. import javax.validation.constraints.NotEmpty;
  99. import java.util.List;
  100.  
  101. @Entity
  102. public class Recipe {
  103.     @Id
  104.     @GeneratedValue
  105.     @JsonIgnore
  106.     private Long id;
  107.     @NotEmpty
  108.     private String name;
  109.     @NotEmpty
  110.     private String description;
  111.     @ElementCollection
  112.     private List<String> ingredients;
  113.     @ElementCollection
  114.     private List<String> directions;
  115.  
  116.     public Long getId() { return id; }
  117.  
  118.     public String getName() {
  119.         return name;
  120.     }
  121.  
  122.     public void setName(String name) {
  123.         this.name = name;
  124.     }
  125.  
  126.     public String getDescription() {
  127.         return description;
  128.     }
  129.  
  130.     public void setDescription(String description) {
  131.         this.description = description;
  132.     }
  133.  
  134.     public List<String> getIngredients() {
  135.         return ingredients;
  136.     }
  137.  
  138.     public void setIngredients(List<String> ingredients) {
  139.         this.ingredients = ingredients;
  140.     }
  141.  
  142.     public List<String> getDirections() {
  143.         return directions;
  144.     }
  145.  
  146.     public void setDirections(List<String> directions) {
  147.         this.directions = directions;
  148.     }
  149.  
  150.     public Recipe(String name, String description, List<String> ingredients, List<String> directions) {
  151.         this.name = name;
  152.         this.description = description;
  153.         this.ingredients = ingredients;
  154.         this.directions = directions;
  155.     }
  156.  
  157.     public Recipe() {
  158.     }
  159. }
  160.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement