diff --git a/src/main/java/ru/ip/labworks/labworks/bookshop/controller/AuthorDto.java b/src/main/java/ru/ip/labworks/labworks/bookshop/controller/AuthorDto.java index 4c36257..7bd814b 100644 --- a/src/main/java/ru/ip/labworks/labworks/bookshop/controller/AuthorDto.java +++ b/src/main/java/ru/ip/labworks/labworks/bookshop/controller/AuthorDto.java @@ -22,4 +22,9 @@ public class AuthorDto { public String getFirstname(){return firstname;} public String getLastname(){return lastname;} public String getPhoto(){return photo;} + + public void setId(Long id){this.id = id;} + public void setFirstname(String firstname){this.firstname = firstname;} + public void setLastname(String lastname){this.lastname = lastname;} + public void setPhoto(String photo){this.photo = photo;} } diff --git a/src/main/java/ru/ip/labworks/labworks/bookshop/controller/AuthorMvcController.java b/src/main/java/ru/ip/labworks/labworks/bookshop/controller/AuthorMvcController.java index 28bb0f3..34daa98 100644 --- a/src/main/java/ru/ip/labworks/labworks/bookshop/controller/AuthorMvcController.java +++ b/src/main/java/ru/ip/labworks/labworks/bookshop/controller/AuthorMvcController.java @@ -3,17 +3,22 @@ import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; import ru.ip.labworks.labworks.bookshop.service.AuthorService; +import ru.ip.labworks.labworks.bookshop.service.BookService; import java.io.IOException; +import java.util.Base64; @Controller @RequestMapping("/author") public class AuthorMvcController { private final AuthorService authorService; - public AuthorMvcController(AuthorService authorService) + private final BookService bookService; + public AuthorMvcController(AuthorService authorService, BookService bookService) { this.authorService = authorService; + this.bookService = bookService; } @GetMapping @@ -39,14 +44,16 @@ public class AuthorMvcController { @PostMapping(value = {"/", "/{id}"}) public String saveAuthor(@PathVariable(required = false) Long id, - @ModelAttribute("authorDto") AuthorDto authorDto, - BindingResult bindingResult, - Model model) throws IOException { + @RequestParam(value = "multipartFile") MultipartFile multipartFile, + @ModelAttribute("authorDto") AuthorDto authorDto, + BindingResult bindingResult, + Model model) throws IOException { if (bindingResult.hasErrors()) { model.addAttribute("errors", bindingResult.getAllErrors()); return "author-update"; } + authorDto.setPhoto("data:" + multipartFile.getContentType() + ";base64," + Base64.getEncoder().encodeToString(multipartFile.getBytes())); if (id == null || id <= 0) { authorService.addAuthor(authorDto); } else { @@ -60,4 +67,31 @@ public class AuthorMvcController { authorService.deleteAuthor(id); return "redirect:/author"; } + + @GetMapping("/{id}/books") + public String getAuthorBooks(@PathVariable Long id, Model model){ + model.addAttribute("author", + new AuthorDto(authorService.findAuthor(id))); + model.addAttribute("authorbooks", + authorService.authorBooks(id).stream() + .map(BookDto::new) + .toList()); + model.addAttribute("books", + bookService.findAllBooks().stream() + .map(BookDto::new) + .toList()); + return "author-mtm"; + } + + @PostMapping("/{id}/books") + public String addBookToAuthor(@PathVariable Long id, @RequestParam(value = "bookid") Long bookid){ + authorService.addBookToAuthor(id, bookid); + return "redirect:/author/" + id.toString() + "/books"; + } + + @PostMapping("/{id}/books/{bookid}") + public String removeBookFromAuthor(@PathVariable Long id, @PathVariable Long bookid){ + authorService.removeBookFromAuthor(id, bookid); + return "redirect:/author/" + id.toString() + "/books"; + } } diff --git a/src/main/java/ru/ip/labworks/labworks/bookshop/controller/BookDto.java b/src/main/java/ru/ip/labworks/labworks/bookshop/controller/BookDto.java index 5e895dd..26caf34 100644 --- a/src/main/java/ru/ip/labworks/labworks/bookshop/controller/BookDto.java +++ b/src/main/java/ru/ip/labworks/labworks/bookshop/controller/BookDto.java @@ -23,4 +23,9 @@ public class BookDto { public String getName(){return name;} public Date getRelease(){return release;} public String getCover(){return cover;} + + public void setId(Long id){this.id = id;} + public void setName(String name){this.name = name;} + public void setRelease(Date release){this.release = release;} + public void setCover(String cover){this.cover = cover;} } diff --git a/src/main/java/ru/ip/labworks/labworks/bookshop/controller/BookMvcController.java b/src/main/java/ru/ip/labworks/labworks/bookshop/controller/BookMvcController.java index c9d72b0..6818846 100644 --- a/src/main/java/ru/ip/labworks/labworks/bookshop/controller/BookMvcController.java +++ b/src/main/java/ru/ip/labworks/labworks/bookshop/controller/BookMvcController.java @@ -3,16 +3,21 @@ import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; import ru.ip.labworks.labworks.bookshop.service.BookService; +import ru.ip.labworks.labworks.bookshop.service.GenreService; import java.io.IOException; +import java.util.Base64; @Controller @RequestMapping("/book") public class BookMvcController { private final BookService bookService; - public BookMvcController(BookService bookService){ + private final GenreService genreService; + public BookMvcController(BookService bookService, GenreService genreService){ this.bookService = bookService; + this.genreService = genreService; } @GetMapping @@ -38,14 +43,16 @@ public class BookMvcController { @PostMapping(value = {"/", "/{id}"}) public String saveBook(@PathVariable(required = false) Long id, - @ModelAttribute("bookDto") BookDto bookDto, - BindingResult bindingResult, - Model model) throws IOException { + @RequestParam(value = "multipartFile") MultipartFile multipartFile, + @ModelAttribute("bookDto") BookDto bookDto, + BindingResult bindingResult, + Model model) throws IOException { if (bindingResult.hasErrors()) { model.addAttribute("errors", bindingResult.getAllErrors()); return "book-update"; } + bookDto.setCover("data:" + multipartFile.getContentType() + ";base64," + Base64.getEncoder().encodeToString(multipartFile.getBytes())); if (id == null || id <= 0) { bookService.addBook(bookDto); } else { @@ -59,4 +66,31 @@ public class BookMvcController { bookService.deleteBook(id); return "redirect:/book"; } + + @GetMapping("/{id}/genres") + public String getBookGenres(@PathVariable Long id, Model model){ + model.addAttribute("book", + new BookDto(bookService.findBook(id))); + model.addAttribute("bookgenres", + bookService.bookGenres(id).stream() + .map(GenreDto::new) + .toList()); + model.addAttribute("genres", + genreService.findAllGenres().stream() + .map(GenreDto::new) + .toList()); + return "book-mtm"; + } + + @PostMapping("/{id}/genres") + public String addGenreToBook(@PathVariable Long id, @RequestParam(value = "genreid") Long genreid){ + bookService.addGenreToBook(id, genreid); + return "redirect:/book/" + id.toString() + "/genres"; + } + + @PostMapping("/{id}/genres/{genreid}") + public String removeGenreFromBook(@PathVariable Long id, @PathVariable Long genreid){ + bookService.removeGenreFromBook(id, genreid); + return "redirect:/book/" + id.toString() + "/genres"; + } } diff --git a/src/main/java/ru/ip/labworks/labworks/bookshop/controller/GenreDto.java b/src/main/java/ru/ip/labworks/labworks/bookshop/controller/GenreDto.java index d4d4b7a..30f081e 100644 --- a/src/main/java/ru/ip/labworks/labworks/bookshop/controller/GenreDto.java +++ b/src/main/java/ru/ip/labworks/labworks/bookshop/controller/GenreDto.java @@ -14,4 +14,7 @@ public class GenreDto { public Long getId(){return id;} public String getName(){return name;} + + public void setId(Long id){this.id = id;} + public void setName(String name){this.name = name;} } diff --git a/src/main/java/ru/ip/labworks/labworks/bookshop/controller/GenreMvcController.java b/src/main/java/ru/ip/labworks/labworks/bookshop/controller/GenreMvcController.java index 57a649a..c303858 100644 --- a/src/main/java/ru/ip/labworks/labworks/bookshop/controller/GenreMvcController.java +++ b/src/main/java/ru/ip/labworks/labworks/bookshop/controller/GenreMvcController.java @@ -56,7 +56,7 @@ public class GenreMvcController { } @PostMapping("/delete/{id}") - public String deleteBook(@PathVariable Long id) { + public String deleteGenre(@PathVariable Long id) { genreService.deleteGenre(id); return "redirect:/genre"; } diff --git a/src/main/resources/templates/author-mtm.html b/src/main/resources/templates/author-mtm.html new file mode 100644 index 0000000..18af5d6 --- /dev/null +++ b/src/main/resources/templates/author-mtm.html @@ -0,0 +1,54 @@ + + + + + +
+
+ + + + + + + + + + + + + + + +
НазваниеДата релизаФотоРедактировать запись
+ + +
+ +
+
+ +
+
+
+
+
+ + +
+
+
+ Закрыть +
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/author-update.html b/src/main/resources/templates/author-update.html index e69de29..268327a 100644 --- a/src/main/resources/templates/author-update.html +++ b/src/main/resources/templates/author-update.html @@ -0,0 +1,40 @@ + + + + + +
+
+
+
+ + +
+
+ + +
+
+ + + +
+
+ + + Назад + +
+
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/authors.html b/src/main/resources/templates/authors.html index e69de29..0a3ad3f 100644 --- a/src/main/resources/templates/authors.html +++ b/src/main/resources/templates/authors.html @@ -0,0 +1,57 @@ + + + + + +
+
+ + Добавить + +
+
+ + + + + + + + + + + + + + + +
ИмяФамилияФотоРедактировать запись
+ + +
+ + Изменить + + + + Книга + +
+
+ +
+
+
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/book-mtm.html b/src/main/resources/templates/book-mtm.html new file mode 100644 index 0000000..70078bf --- /dev/null +++ b/src/main/resources/templates/book-mtm.html @@ -0,0 +1,50 @@ + + + + + +
+
+ + + + + + + + + + + + +
НазваниеРедактировать запись
+ +
+ +
+
+ +
+
+
+
+
+ + +
+
+
+ Закрыть +
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/book-update.html b/src/main/resources/templates/book-update.html index e69de29..ae3016c 100644 --- a/src/main/resources/templates/book-update.html +++ b/src/main/resources/templates/book-update.html @@ -0,0 +1,40 @@ + + + + + +
+
+
+
+ + +
+
+ + +
+
+ + + +
+
+ + + Назад + +
+
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/books.html b/src/main/resources/templates/books.html index e69de29..27b0e31 100644 --- a/src/main/resources/templates/books.html +++ b/src/main/resources/templates/books.html @@ -0,0 +1,57 @@ + + + + + +
+
+ + Добавить + +
+
+ + + + + + + + + + + + + + + +
НазваниеДата релизаФотоРедактировать запись
+ + +
+ + Изменить + + + + Жанры + +
+
+ +
+
+
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html index 68f7187..843e863 100644 --- a/src/main/resources/templates/default.html +++ b/src/main/resources/templates/default.html @@ -16,21 +16,25 @@ +
+
+
+