forked from NeilAlishev/SpringCourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPeopleController.java
More file actions
81 lines (65 loc) · 2.31 KB
/
PeopleController.java
File metadata and controls
81 lines (65 loc) · 2.31 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package ru.alishev.springcourse.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import ru.alishev.springcourse.dao.PersonDAO;
import ru.alishev.springcourse.models.Person;
import ru.alishev.springcourse.util.PersonValidator;
import javax.validation.Valid;
/**
* @author Neil Alishev
*/
@Controller
@RequestMapping("/people")
public class PeopleController {
private final PersonDAO personDAO;
private final PersonValidator personValidator;
@Autowired
public PeopleController(PersonDAO personDAO, PersonValidator personValidator) {
this.personDAO = personDAO;
this.personValidator = personValidator;
}
@GetMapping()
public String index(Model model) {
model.addAttribute("people", personDAO.index());
return "people/index";
}
@GetMapping("/{id}")
public String show(@PathVariable("id") int id, Model model) {
model.addAttribute("person", personDAO.show(id));
return "people/show";
}
@GetMapping("/new")
public String newPerson(@ModelAttribute("person") Person person) {
return "people/new";
}
@PostMapping()
public String create(@ModelAttribute("person") @Valid Person person,
BindingResult bindingResult) {
personValidator.validate(person, bindingResult);
if (bindingResult.hasErrors())
return "people/new";
personDAO.save(person);
return "redirect:/people";
}
@GetMapping("/{id}/edit")
public String edit(Model model, @PathVariable("id") int id) {
model.addAttribute("person", personDAO.show(id));
return "people/edit";
}
@PatchMapping("/{id}")
public String update(@ModelAttribute("person") @Valid Person person, BindingResult bindingResult,
@PathVariable("id") int id) {
if (bindingResult.hasErrors())
return "people/edit";
personDAO.update(id, person);
return "redirect:/people";
}
@DeleteMapping("/{id}")
public String delete(@PathVariable("id") int id) {
personDAO.delete(id);
return "redirect:/people";
}
}