-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookController.java
More file actions
109 lines (87 loc) · 3.42 KB
/
BookController.java
File metadata and controls
109 lines (87 loc) · 3.42 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package com.mehmetpekdemir.librarymanagementsystem.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
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.Book;
import com.mehmetpekdemir.librarymanagementsystem.service.AuthorService;
import com.mehmetpekdemir.librarymanagementsystem.service.BookService;
import com.mehmetpekdemir.librarymanagementsystem.service.CategoryService;
import com.mehmetpekdemir.librarymanagementsystem.service.PublisherService;
@Controller
public class BookController {
private final BookService bookService;
private final AuthorService authorService;
private final CategoryService categoryService;
private final PublisherService publisherService;
@Autowired
public BookController(BookService bookService, AuthorService authorService, CategoryService categoryService,
PublisherService publisherService) {
this.bookService = bookService;
this.authorService = authorService;
this.categoryService = categoryService;
this.publisherService = publisherService;
}
@RequestMapping("/books")
public String findAllBooks(Model model) {
final List<Book> books = bookService.findAllBooks();
model.addAttribute("books", books);
return "list-books";
}
@RequestMapping("/searchBook")
public String searchBook(@Param("keyword") String keyword, Model model) {
final List<Book> books = bookService.searchBooks(keyword);
model.addAttribute("books", books);
model.addAttribute("keyword", keyword);
return "list-books";
}
@RequestMapping("/book/{id}")
public String findBookById(@PathVariable("id") Long id, Model model) {
final Book book = bookService.findBookById(id);
model.addAttribute("book", book);
return "list-book";
}
@GetMapping("/add")
public String showCreateForm(Book book, Model model) {
model.addAttribute("categories", categoryService.findAllCategories());
model.addAttribute("authors", authorService.findAllAuthors());
model.addAttribute("publishers", publisherService.findAllPublishers());
return "add-book";
}
@RequestMapping("/add-book")
public String createBook(Book book, BindingResult result, Model model) {
if (result.hasErrors()) {
return "add-book";
}
bookService.createBook(book);
model.addAttribute("book", bookService.findAllBooks());
return "redirect:/books";
}
@GetMapping("/update/{id}")
public String showUpdateForm(@PathVariable("id") Long id, Model model) {
final Book book = bookService.findBookById(id);
model.addAttribute("book", book);
return "update-book";
}
@RequestMapping("/update-book/{id}")
public String updateBook(@PathVariable("id") Long id, Book book, BindingResult result, Model model) {
if (result.hasErrors()) {
book.setId(id);
return "update-book";
}
bookService.updateBook(book);
model.addAttribute("book", bookService.findAllBooks());
return "redirect:/books";
}
@RequestMapping("/remove-book/{id}")
public String deleteBook(@PathVariable("id") Long id, Model model) {
bookService.deleteBook(id);
model.addAttribute("book", bookService.findAllBooks());
return "redirect:/books";
}
}