From cb2828e0a933c3df6ca7915bdbcb94993539d9ee Mon Sep 17 00:00:00 2001 From: maxnes3 <112558334+maxnes3@users.noreply.github.com> Date: Sun, 14 May 2023 19:58:26 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=B0=20=D0=B3=D0=BE=D1=82?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bookshop/controller/AuthorDto.java | 5 ++ .../controller/AuthorMvcController.java | 42 ++++++++++++-- .../labworks/bookshop/controller/BookDto.java | 5 ++ .../controller/BookMvcController.java | 42 ++++++++++++-- .../bookshop/controller/GenreDto.java | 3 + .../controller/GenreMvcController.java | 2 +- src/main/resources/templates/author-mtm.html | 54 ++++++++++++++++++ .../resources/templates/author-update.html | 40 +++++++++++++ src/main/resources/templates/authors.html | 57 +++++++++++++++++++ src/main/resources/templates/book-mtm.html | 50 ++++++++++++++++ src/main/resources/templates/book-update.html | 40 +++++++++++++ src/main/resources/templates/books.html | 57 +++++++++++++++++++ src/main/resources/templates/default.html | 14 +++-- .../resources/templates/genre-update.html | 31 ++++++++++ src/main/resources/templates/genres.html | 49 ++++++++++++++++ 15 files changed, 477 insertions(+), 14 deletions(-) create mode 100644 src/main/resources/templates/author-mtm.html create mode 100644 src/main/resources/templates/book-mtm.html 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 @@ + + +
+ + +Название | +Дата релиза | +Фото | +Редактировать запись | +
---|---|---|---|
+ | + |
+
+
+
+
+ |
+
Имя | +Фамилия | +Фото | +Редактировать запись | +
---|---|---|---|
+ | + |
+
+
+ Изменить
+
+
+
+ Книга
+
+
+
+ |
+
Название | +Редактировать запись | +
---|---|
+ |
+
+
+
+
+ |
+
Название | +Дата релиза | +Фото | +Редактировать запись | +
---|---|---|---|
+ | + |
+
+
+ Изменить
+
+
+
+ Жанры
+
+
+
+ |
+