mvc add filtration in search page

This commit is contained in:
Zakharov_Rostislav 2024-06-09 15:52:53 +04:00
parent e2e27510da
commit f6e5c71aba
3 changed files with 40 additions and 16 deletions

View File

@ -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");

View File

@ -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;
}

View File

@ -12,20 +12,26 @@
<th:block th:case="*">
<h2>Поиск</h2>
<form method="get">
<select id="typeId" name="typeId" th:value="*{typeId}">
<div class="mb-3">
<select class="form-control" id="typeId" name="typeId">
<option value="-1">Выберите жанр</option>
<option th:each="type : ${types}"
th:value="${type.Id}"
th:text="${type.name}">
th:value="${type.id}"
th:text="${type.name}"
th:selected="${type.id==typeId}">
</option>
</select>
<select id="authorId" name="authorId" th:value="*{authorId}">
</div>
<div class="mb-3">
<select class="form-control" id="authorId" name="authorId">
<option value="-1">Выберите автора</option>
<option th:each="author : ${authors}"
th:value="${author.id}"
th:text="${author.name}">
th:text="${author.name}"
th:selected="${author.id==authorId}">
</option>
</select>
</div>
<button class="btn btn-primary me-2 button-fixed-width" type="submit">Обновить</button>
</form>
<table class="table">