-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathRecipeController.java
48 lines (41 loc) · 1.6 KB
/
RecipeController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package guru.springframework.controllers;
import guru.springframework.commands.RecipeCommand;
import guru.springframework.services.RecipeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Slf4j
@Controller
public class RecipeController {
private final RecipeService recipeService;
public RecipeController(RecipeService recipeService) {
this.recipeService = recipeService;
}
@GetMapping("/recipe/{id}/show")
public String showById(@PathVariable String id, Model model){
model.addAttribute("recipe",recipeService.findById(Long.valueOf(id)));
return "recipe/show";
}
@GetMapping("recipe/new")
public String newRecipe(Model model){
model.addAttribute("recipe", new RecipeCommand());
return "recipe/recipeform";
}
@GetMapping("recipe/{id}/update")
public String updateRecipe(@PathVariable String id, Model model){
model.addAttribute("recipe", recipeService.findCommandById(Long.valueOf(id)));
return "recipe/recipeform";
}
@PostMapping("recipe")
public String serveOrUpdate(@ModelAttribute RecipeCommand command){
RecipeCommand savedCommand = recipeService.saveRecipeCommand(command);
return "redirect:/recipe/" + savedCommand.getId() + "/show";
}
@GetMapping("recipe/{id}/delete")
public String deleteById(@PathVariable String id){
log.debug("Deleting id: " +id);
recipeService.deleteById(Long.valueOf(id));
return "redirect:/";
}
}