-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPublisherController.java
More file actions
84 lines (64 loc) · 2.6 KB
/
PublisherController.java
File metadata and controls
84 lines (64 loc) · 2.6 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
82
83
84
package com.mehmetpekdemir.librarymanagementsystem.controller;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.mehmetpekdemir.librarymanagementsystem.entity.Publisher;
import com.mehmetpekdemir.librarymanagementsystem.service.PublisherService;
@Controller
public class PublisherController {
private final PublisherService publisherService;
public PublisherController(PublisherService publisherService) {
this.publisherService = publisherService;
}
@RequestMapping("/publishers")
public String findAllPublishers(Model model) {
final List<Publisher> publishers = publisherService.findAllPublishers();
model.addAttribute("publishers", publishers);
return "list-publishers";
}
@RequestMapping("/publisher/{id}")
public String findPublisherById(@PathVariable("id") Long id, Model model) {
final Publisher publisher = publisherService.findPublisherById(id);
model.addAttribute("publisher", publisher);
return "list-publisher";
}
@GetMapping("/addPublisher")
public String showCreateForm(Publisher publisher) {
return "add-publisher";
}
@RequestMapping("/add-publisher")
public String createPublisher(Publisher publisher, BindingResult result, Model model) {
if (result.hasErrors()) {
return "add-publisher";
}
publisherService.createPublisher(publisher);
model.addAttribute("publisher", publisherService.findAllPublishers());
return "redirect:/publishers";
}
@GetMapping("/updatePublisher/{id}")
public String showUpdateForm(@PathVariable("id") Long id, Model model) {
final Publisher publisher = publisherService.findPublisherById(id);
model.addAttribute("publisher", publisher);
return "update-publisher";
}
@RequestMapping("/update-publisher/{id}")
public String updatePublisher(@PathVariable("id") Long id, Publisher publisher, BindingResult result, Model model) {
if (result.hasErrors()) {
publisher.setId(id);
return "update-publishers";
}
publisherService.updatePublisher(publisher);
model.addAttribute("publisher", publisherService.findAllPublishers());
return "redirect:/publishers";
}
@RequestMapping("/remove-publisher/{id}")
public String deletePublisher(@PathVariable("id") Long id, Model model) {
publisherService.deletePublisher(id);
model.addAttribute("publisher", publisherService.findAllPublishers());
return "redirect:/publishers";
}
}