-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthorController.java
More file actions
84 lines (64 loc) · 2.38 KB
/
AuthorController.java
File metadata and controls
84 lines (64 loc) · 2.38 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.Author;
import com.mehmetpekdemir.librarymanagementsystem.service.AuthorService;
@Controller
public class AuthorController {
private final AuthorService authorService;
public AuthorController(AuthorService authorService) {
this.authorService = authorService;
}
@RequestMapping("/authors")
public String findAllAuthors(Model model) {
final List<Author> authors = authorService.findAllAuthors();
model.addAttribute("authors", authors);
return "list-authors";
}
@RequestMapping("/author/{id}")
public String findAuthorById(@PathVariable("id") Long id, Model model) {
final Author author = authorService.findAuthorById(id);
model.addAttribute("author", author);
return "list-author";
}
@GetMapping("/addAuthor")
public String showCreateForm(Author author) {
return "add-author";
}
@RequestMapping("/add-author")
public String createAuthor(Author author, BindingResult result, Model model) {
if (result.hasErrors()) {
return "add-author";
}
authorService.createAuthor(author);
model.addAttribute("author", authorService.findAllAuthors());
return "redirect:/authors";
}
@GetMapping("/updateAuthor/{id}")
public String showUpdateForm(@PathVariable("id") Long id, Model model) {
final Author author = authorService.findAuthorById(id);
model.addAttribute("author", author);
return "update-author";
}
@RequestMapping("/update-author/{id}")
public String updateAuthor(@PathVariable("id") Long id, Author author, BindingResult result, Model model) {
if (result.hasErrors()) {
author.setId(id);
return "update-author";
}
authorService.updateAuthor(author);
model.addAttribute("author", authorService.findAllAuthors());
return "redirect:/authors";
}
@RequestMapping("/remove-author/{id}")
public String deleteAuthor(@PathVariable("id") Long id, Model model) {
authorService.deleteAuthor(id);
model.addAttribute("author", authorService.findAllAuthors());
return "redirect:/authors";
}
}