From 46732e4a2af0da5a75d2bc0e823a865c902cc19f Mon Sep 17 00:00:00 2001 From: maxnes3 <112558334+maxnes3@users.noreply.github.com> Date: Sun, 14 May 2023 15:25:43 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D1=87=D0=B0=D0=BB=20=D0=B2=D0=B5?= =?UTF-8?q?=D1=82=D0=BA=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 5 ++ .../bookshop/controller/AuthorController.java | 4 +- .../controller/AuthorMvcController.java | 63 +++++++++++++++++++ .../bookshop/controller/BookController.java | 4 +- .../controller/BookMvcController.java | 62 ++++++++++++++++++ .../bookshop/controller/GenreController.java | 4 +- .../controller/GenreMvcController.java | 63 +++++++++++++++++++ .../configuration/WebConfiguration.java | 8 +++ .../labworks/util/error/AdviceController.java | 3 +- .../resources/templates/author-update.html | 0 src/main/resources/templates/authors.html | 0 src/main/resources/templates/book-update.html | 0 src/main/resources/templates/books.html | 0 src/main/resources/templates/default.html | 44 +++++++++++++ src/main/resources/templates/error.html | 10 +++ .../resources/templates/genre-update.html | 0 src/main/resources/templates/genres.html | 0 17 files changed, 263 insertions(+), 7 deletions(-) create mode 100644 src/main/java/ru/ip/labworks/labworks/bookshop/controller/AuthorMvcController.java create mode 100644 src/main/java/ru/ip/labworks/labworks/bookshop/controller/BookMvcController.java create mode 100644 src/main/java/ru/ip/labworks/labworks/bookshop/controller/GenreMvcController.java create mode 100644 src/main/resources/templates/author-update.html create mode 100644 src/main/resources/templates/authors.html create mode 100644 src/main/resources/templates/book-update.html create mode 100644 src/main/resources/templates/books.html create mode 100644 src/main/resources/templates/default.html create mode 100644 src/main/resources/templates/error.html create mode 100644 src/main/resources/templates/genre-update.html create mode 100644 src/main/resources/templates/genres.html diff --git a/build.gradle b/build.gradle index 87c4075..43bcafb 100644 --- a/build.gradle +++ b/build.gradle @@ -15,6 +15,11 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' developmentOnly 'org.springframework.boot:spring-boot-devtools' + implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' + implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect' + implementation 'org.webjars:bootstrap:5.1.3' + implementation 'org.webjars:jquery:3.6.0' + implementation 'org.webjars:font-awesome:6.1.0' implementation 'com.h2database:h2:2.1.210' implementation 'org.hibernate.validator:hibernate-validator:6.0.17.Final' implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5' diff --git a/src/main/java/ru/ip/labworks/labworks/bookshop/controller/AuthorController.java b/src/main/java/ru/ip/labworks/labworks/bookshop/controller/AuthorController.java index 269f188..e7e7a6a 100644 --- a/src/main/java/ru/ip/labworks/labworks/bookshop/controller/AuthorController.java +++ b/src/main/java/ru/ip/labworks/labworks/bookshop/controller/AuthorController.java @@ -1,14 +1,14 @@ package ru.ip.labworks.labworks.bookshop.controller; - import org.springframework.web.bind.annotation.*; import ru.ip.labworks.labworks.bookshop.service.AuthorService; +import ru.ip.labworks.labworks.configuration.WebConfiguration; import javax.validation.Valid; import java.io.IOException; import java.util.List; @RestController -@RequestMapping("/author") +@RequestMapping(WebConfiguration.REST_API + "/author") public class AuthorController { private final AuthorService authorService; 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 new file mode 100644 index 0000000..28bb0f3 --- /dev/null +++ b/src/main/java/ru/ip/labworks/labworks/bookshop/controller/AuthorMvcController.java @@ -0,0 +1,63 @@ +package ru.ip.labworks.labworks.bookshop.controller; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.*; +import ru.ip.labworks.labworks.bookshop.service.AuthorService; + +import java.io.IOException; + +@Controller +@RequestMapping("/author") +public class AuthorMvcController { + private final AuthorService authorService; + public AuthorMvcController(AuthorService authorService) + { + this.authorService = authorService; + } + + @GetMapping + public String getAuthors(Model model) { + model.addAttribute("authors", + authorService.findAllAuthors().stream() + .map(AuthorDto::new) + .toList()); + return "authors"; + } + + @GetMapping(value = {"/update", "/update/{id}"}) + public String updateAuthor(@PathVariable(required = false) Long id, + Model model) { + if (id == null || id <= 0) { + model.addAttribute("authorDto", new AuthorDto()); + } else { + model.addAttribute("authorDto", id); + model.addAttribute("authorDto", new AuthorDto(authorService.findAuthor(id))); + } + return "author-update"; + } + + @PostMapping(value = {"/", "/{id}"}) + public String saveAuthor(@PathVariable(required = false) Long id, + @ModelAttribute("authorDto") AuthorDto authorDto, + BindingResult bindingResult, + Model model) throws IOException { + if (bindingResult.hasErrors()) { + model.addAttribute("errors", + bindingResult.getAllErrors()); + return "author-update"; + } + if (id == null || id <= 0) { + authorService.addAuthor(authorDto); + } else { + authorService.updateAuthor(id, authorDto); + } + return "redirect:/author"; + } + + @PostMapping("/delete/{id}") + public String deleteAuthor(@PathVariable Long id) { + authorService.deleteAuthor(id); + return "redirect:/author"; + } +} diff --git a/src/main/java/ru/ip/labworks/labworks/bookshop/controller/BookController.java b/src/main/java/ru/ip/labworks/labworks/bookshop/controller/BookController.java index 10ed1c3..b60e01c 100644 --- a/src/main/java/ru/ip/labworks/labworks/bookshop/controller/BookController.java +++ b/src/main/java/ru/ip/labworks/labworks/bookshop/controller/BookController.java @@ -1,14 +1,14 @@ package ru.ip.labworks.labworks.bookshop.controller; - import org.springframework.web.bind.annotation.*; import ru.ip.labworks.labworks.bookshop.service.BookService; +import ru.ip.labworks.labworks.configuration.WebConfiguration; import javax.validation.Valid; import java.io.IOException; import java.util.List; @RestController -@RequestMapping("/book") +@RequestMapping(WebConfiguration.REST_API + "/book") public class BookController { private final BookService bookService; 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 new file mode 100644 index 0000000..c9d72b0 --- /dev/null +++ b/src/main/java/ru/ip/labworks/labworks/bookshop/controller/BookMvcController.java @@ -0,0 +1,62 @@ +package ru.ip.labworks.labworks.bookshop.controller; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.*; +import ru.ip.labworks.labworks.bookshop.service.BookService; + +import java.io.IOException; + +@Controller +@RequestMapping("/book") +public class BookMvcController { + private final BookService bookService; + public BookMvcController(BookService bookService){ + this.bookService = bookService; + } + + @GetMapping + public String getBooks(Model model) { + model.addAttribute("books", + bookService.findAllBooks().stream() + .map(BookDto::new) + .toList()); + return "books"; + } + + @GetMapping(value = {"/update", "/update/{id}"}) + public String updateBook(@PathVariable(required = false) Long id, + Model model) { + if (id == null || id <= 0) { + model.addAttribute("bookDto", new BookDto()); + } else { + model.addAttribute("bookDto", id); + model.addAttribute("bookDto", new BookDto(bookService.findBook(id))); + } + return "book-update"; + } + + @PostMapping(value = {"/", "/{id}"}) + public String saveBook(@PathVariable(required = false) Long id, + @ModelAttribute("bookDto") BookDto bookDto, + BindingResult bindingResult, + Model model) throws IOException { + if (bindingResult.hasErrors()) { + model.addAttribute("errors", + bindingResult.getAllErrors()); + return "book-update"; + } + if (id == null || id <= 0) { + bookService.addBook(bookDto); + } else { + bookService.updateBook(id, bookDto); + } + return "redirect:/book"; + } + + @PostMapping("/delete/{id}") + public String deleteBook(@PathVariable Long id) { + bookService.deleteBook(id); + return "redirect:/book"; + } +} diff --git a/src/main/java/ru/ip/labworks/labworks/bookshop/controller/GenreController.java b/src/main/java/ru/ip/labworks/labworks/bookshop/controller/GenreController.java index 67525d3..8912319 100644 --- a/src/main/java/ru/ip/labworks/labworks/bookshop/controller/GenreController.java +++ b/src/main/java/ru/ip/labworks/labworks/bookshop/controller/GenreController.java @@ -1,14 +1,14 @@ package ru.ip.labworks.labworks.bookshop.controller; - import org.springframework.web.bind.annotation.*; import ru.ip.labworks.labworks.bookshop.service.GenreService; +import ru.ip.labworks.labworks.configuration.WebConfiguration; import javax.validation.Valid; import java.io.IOException; import java.util.List; @RestController -@RequestMapping("/genre") +@RequestMapping(WebConfiguration.REST_API + "/genre") public class GenreController { private final GenreService genreService; 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 new file mode 100644 index 0000000..57a649a --- /dev/null +++ b/src/main/java/ru/ip/labworks/labworks/bookshop/controller/GenreMvcController.java @@ -0,0 +1,63 @@ +package ru.ip.labworks.labworks.bookshop.controller; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.*; +import ru.ip.labworks.labworks.bookshop.service.BookService; +import ru.ip.labworks.labworks.bookshop.service.GenreService; + +import java.io.IOException; + +@Controller +@RequestMapping("/genre") +public class GenreMvcController { + private final GenreService genreService; + public GenreMvcController(GenreService genreService){ + this.genreService = genreService; + } + + @GetMapping + public String getBooks(Model model) { + model.addAttribute("genres", + genreService.findAllGenres().stream() + .map(GenreDto::new) + .toList()); + return "genres"; + } + + @GetMapping(value = {"/update", "/update/{id}"}) + public String editBook(@PathVariable(required = false) Long id, + Model model) { + if (id == null || id <= 0) { + model.addAttribute("genreDto", new GenreDto()); + } else { + model.addAttribute("genreDto", id); + model.addAttribute("genreDto", new GenreDto(genreService.findGenre(id))); + } + return "genre-update"; + } + + @PostMapping(value = {"/", "/{id}"}) + public String saveBook(@PathVariable(required = false) Long id, + @ModelAttribute("genreDto") GenreDto genreDto, + BindingResult bindingResult, + Model model) throws IOException { + if (bindingResult.hasErrors()) { + model.addAttribute("errors", + bindingResult.getAllErrors()); + return "genre-update"; + } + if (id == null || id <= 0) { + genreService.addGenre(genreDto); + } else { + genreService.updateGenre(id, genreDto); + } + return "redirect:/genre"; + } + + @PostMapping("/delete/{id}") + public String deleteBook(@PathVariable Long id) { + genreService.deleteGenre(id); + return "redirect:/genre"; + } +} diff --git a/src/main/java/ru/ip/labworks/labworks/configuration/WebConfiguration.java b/src/main/java/ru/ip/labworks/labworks/configuration/WebConfiguration.java index 29a172c..065df0f 100644 --- a/src/main/java/ru/ip/labworks/labworks/configuration/WebConfiguration.java +++ b/src/main/java/ru/ip/labworks/labworks/configuration/WebConfiguration.java @@ -2,10 +2,18 @@ package ru.ip.labworks.labworks.configuration; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class WebConfiguration implements WebMvcConfigurer { + public static final String REST_API = "/api"; + @Override + public void addViewControllers(ViewControllerRegistry registry) { + WebMvcConfigurer.super.addViewControllers(registry); + registry.addViewController("author"); + } + @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**").allowedMethods("*"); diff --git a/src/main/java/ru/ip/labworks/labworks/util/error/AdviceController.java b/src/main/java/ru/ip/labworks/labworks/util/error/AdviceController.java index 4949027..c53c98b 100644 --- a/src/main/java/ru/ip/labworks/labworks/util/error/AdviceController.java +++ b/src/main/java/ru/ip/labworks/labworks/util/error/AdviceController.java @@ -6,11 +6,12 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestController; import ru.ip.labworks.labworks.util.validation.ValidationException; import java.util.stream.Collectors; -@ControllerAdvice +@ControllerAdvice(annotations = RestController.class) public class AdviceController { @ExceptionHandler({ ValidationException.class diff --git a/src/main/resources/templates/author-update.html b/src/main/resources/templates/author-update.html new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/templates/authors.html b/src/main/resources/templates/authors.html new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/templates/book-update.html b/src/main/resources/templates/book-update.html new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/templates/books.html b/src/main/resources/templates/books.html new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html new file mode 100644 index 0000000..68f7187 --- /dev/null +++ b/src/main/resources/templates/default.html @@ -0,0 +1,44 @@ + + +
+ +