From f6e5c71aba8b59d0806d703e6d7995ab6050bdaf Mon Sep 17 00:00:00 2001 From: Zakharov_Rostislav Date: Sun, 9 Jun 2024 15:52:53 +0400 Subject: [PATCH] mvc add filtration in search page --- .../com/ip/library/LibraryApplication.java | 6 +++- .../controllers/users/UserBookController.java | 16 ++++++++- .../main/resources/templates/book-search.html | 34 +++++++++++-------- 3 files changed, 40 insertions(+), 16 deletions(-) diff --git a/SpringApp/library/src/main/java/com/ip/library/LibraryApplication.java b/SpringApp/library/src/main/java/com/ip/library/LibraryApplication.java index 23b262b..973939b 100644 --- a/SpringApp/library/src/main/java/com/ip/library/LibraryApplication.java +++ b/SpringApp/library/src/main/java/com/ip/library/LibraryApplication.java @@ -54,16 +54,20 @@ public class LibraryApplication implements CommandLineRunner { log.info("Create default authors values"); final var author1 = authorService.create(new AuthorEntity("author1")); final var author2 = authorService.create(new AuthorEntity("author2")); + final var author3 = authorService.create(new AuthorEntity("author3")); log.info("Create default books values"); final var book1 = bookService.create(new BookEntity("book1", type1), new ArrayList<>()); final var book2 = bookService.create(new BookEntity("book2", type1), new ArrayList<>()); final var book3 = bookService.create(new BookEntity("book3", type2), new ArrayList<>()); final var book4 = bookService.create(new BookEntity("book4", type2), new ArrayList<>()); + final var book5 = bookService.create(new BookEntity("book5", type2), new ArrayList<>()); bookService.addAuthor(author1.getId(), book1.getId()); - bookService.addAuthor(author2.getId(), book2.getId()); bookService.addAuthor(author1.getId(), book3.getId()); + bookService.addAuthor(author1.getId(), book4.getId()); + bookService.addAuthor(author1.getId(), book5.getId()); + bookService.addAuthor(author2.getId(), book2.getId()); bookService.addAuthor(author2.getId(), book3.getId()); log.info("Create default users values"); diff --git a/SpringApp/library/src/main/java/com/ip/library/controllers/users/UserBookController.java b/SpringApp/library/src/main/java/com/ip/library/controllers/users/UserBookController.java index 1b9b300..a407187 100644 --- a/SpringApp/library/src/main/java/com/ip/library/controllers/users/UserBookController.java +++ b/SpringApp/library/src/main/java/com/ip/library/controllers/users/UserBookController.java @@ -14,9 +14,11 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import com.ip.library.controllers.authors.AuthorEntity; +import com.ip.library.controllers.authors.AuthorService; import com.ip.library.controllers.books.BookDto; import com.ip.library.controllers.books.BookEntity; import com.ip.library.controllers.books.BookService; +import com.ip.library.controllers.types.TypeService; import com.ip.library.core.api.PageAttributesMapper; import com.ip.library.core.configuration.Constants; import com.ip.library.core.security.UserPrincipal; @@ -27,19 +29,27 @@ public class UserBookController { private static final String USER_FAVORITES_VIEW = "user-favorites"; private static final String PAGE_ATTRIBUTE = "page"; private static final String AUTHOR_ATTRIBUTE = "authorId"; + private static final String AUTHORS_ATTRIBUTE = "authors"; private static final String TYPE_ATTRIBUTE = "typeId"; + private static final String TYPES_ATTRIBUTE = "types"; private final UserService userService; private final BookService bookService; + private final TypeService typeService; + private final AuthorService authorService; private final ModelMapper modelMapper; public UserBookController( UserService userService, BookService bookService, - ModelMapper modelMapper) { + ModelMapper modelMapper, + TypeService typeService, + AuthorService authorService) { this.bookService = bookService; this.userService = userService; this.modelMapper = modelMapper; + this.typeService = typeService; + this.authorService = authorService; } private BookDto toBookDto (BookEntity entity) { @@ -93,6 +103,10 @@ public class UserBookController { bookService.getAll(typeId, authorId, page, Constants.DEFUALT_PAGE_SIZE), this::toBookDto); model.addAllAttributes(attributes); + model.addAttribute(TYPE_ATTRIBUTE, typeId); + model.addAttribute(AUTHOR_ATTRIBUTE, authorId); + model.addAttribute(TYPES_ATTRIBUTE, typeService.getAll()); + model.addAttribute(AUTHORS_ATTRIBUTE, authorService.getAll()); model.addAttribute(PAGE_ATTRIBUTE, page); return BOOK_SEARCH_VIEW; } diff --git a/SpringApp/library/src/main/resources/templates/book-search.html b/SpringApp/library/src/main/resources/templates/book-search.html index 80fca3c..cc2e47b 100644 --- a/SpringApp/library/src/main/resources/templates/book-search.html +++ b/SpringApp/library/src/main/resources/templates/book-search.html @@ -12,20 +12,26 @@

Поиск

- - +
+ +
+
+ +