it is working
This commit is contained in:
parent
dffe1aeae2
commit
aa9798b68a
@ -22,6 +22,10 @@ public class BookDto {
|
|||||||
private Long typeId;
|
private Long typeId;
|
||||||
@NotNull
|
@NotNull
|
||||||
private List<Long> authorsId;
|
private List<Long> authorsId;
|
||||||
|
@NotNull
|
||||||
|
private boolean isChosen = false;
|
||||||
|
@Min(0)
|
||||||
|
private int fansNumber = 0;
|
||||||
|
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
return id;
|
return id;
|
||||||
@ -70,4 +74,20 @@ public class BookDto {
|
|||||||
public void setAuthorsId(List<Long> authorsId) {
|
public void setAuthorsId(List<Long> authorsId) {
|
||||||
this.authorsId = authorsId;
|
this.authorsId = authorsId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean getIsChosen() {
|
||||||
|
return isChosen;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIsChosen(boolean isChosen) {
|
||||||
|
this.isChosen = isChosen;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getFansNumber() {
|
||||||
|
return fansNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFansNumber(int fansNumber) {
|
||||||
|
this.fansNumber = fansNumber;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,9 +15,11 @@ import jakarta.persistence.ManyToOne;
|
|||||||
import jakarta.persistence.OneToMany;
|
import jakarta.persistence.OneToMany;
|
||||||
import jakarta.persistence.OrderBy;
|
import jakarta.persistence.OrderBy;
|
||||||
import jakarta.persistence.Table;
|
import jakarta.persistence.Table;
|
||||||
|
import jakarta.persistence.Transient;
|
||||||
|
|
||||||
import com.ip.library.controllers.authors.AuthorEntity;
|
import com.ip.library.controllers.authors.AuthorEntity;
|
||||||
import com.ip.library.controllers.authors_books.AuthorsBooksEntity;
|
import com.ip.library.controllers.authors_books.AuthorsBooksEntity;
|
||||||
|
import com.ip.library.controllers.favorites.FavoriteEntity;
|
||||||
import com.ip.library.controllers.types.TypeEntity;
|
import com.ip.library.controllers.types.TypeEntity;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@ -32,6 +34,13 @@ public class BookEntity extends BaseEntity {
|
|||||||
@OneToMany(mappedBy = "book", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
@OneToMany(mappedBy = "book", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
||||||
@OrderBy("id ASC")
|
@OrderBy("id ASC")
|
||||||
private Set<AuthorsBooksEntity> authorsBooks = new HashSet<>();
|
private Set<AuthorsBooksEntity> authorsBooks = new HashSet<>();
|
||||||
|
@OneToMany(mappedBy = "book", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
||||||
|
@OrderBy("id ASC")
|
||||||
|
private Set<FavoriteEntity> fans = new HashSet<>();
|
||||||
|
@Transient
|
||||||
|
private boolean isChosen = false;
|
||||||
|
@Transient
|
||||||
|
private int fansNumber = 0;
|
||||||
|
|
||||||
public BookEntity() {
|
public BookEntity() {
|
||||||
super();
|
super();
|
||||||
@ -42,14 +51,6 @@ public class BookEntity extends BaseEntity {
|
|||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TypeEntity getType() {
|
public TypeEntity getType() {
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
@ -66,6 +67,38 @@ public class BookEntity extends BaseEntity {
|
|||||||
this.authorsBooks = authorsBooks;
|
this.authorsBooks = authorsBooks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean getIsChosen() {
|
||||||
|
return isChosen;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIsChosen(boolean isChosen) {
|
||||||
|
this.isChosen = isChosen;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getFansNumber() {
|
||||||
|
return fansNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFansNumber(int fansNumber) {
|
||||||
|
this.fansNumber = fansNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<FavoriteEntity> getFans() {
|
||||||
|
return fans;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFans(Set<FavoriteEntity> fans) {
|
||||||
|
this.fans = fans;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean addAuthor(AuthorEntity author) {
|
public boolean addAuthor(AuthorEntity author) {
|
||||||
AuthorsBooksEntity entity = new AuthorsBooksEntity(author, this);
|
AuthorsBooksEntity entity = new AuthorsBooksEntity(author, this);
|
||||||
return authorsBooks.add(entity);
|
return authorsBooks.add(entity);
|
||||||
|
@ -13,6 +13,7 @@ import com.ip.library.controllers.authors.AuthorService;
|
|||||||
import com.ip.library.controllers.authors_books.AuthorsBooksEntity;
|
import com.ip.library.controllers.authors_books.AuthorsBooksEntity;
|
||||||
import com.ip.library.controllers.authors_books.AuthorsBooksId;
|
import com.ip.library.controllers.authors_books.AuthorsBooksId;
|
||||||
import com.ip.library.controllers.authors_books.AuthorsBooksRepository;
|
import com.ip.library.controllers.authors_books.AuthorsBooksRepository;
|
||||||
|
import com.ip.library.controllers.users.UserRepository;
|
||||||
import com.ip.library.core.error.NotFoundException;
|
import com.ip.library.core.error.NotFoundException;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@ -20,14 +21,17 @@ public class BookService {
|
|||||||
private final AuthorsBooksRepository authorsBooksRepository;
|
private final AuthorsBooksRepository authorsBooksRepository;
|
||||||
private final BookRepository bookRepository;
|
private final BookRepository bookRepository;
|
||||||
private final AuthorService authorService;
|
private final AuthorService authorService;
|
||||||
|
private final UserRepository userRepository;
|
||||||
|
|
||||||
public BookService(
|
public BookService(
|
||||||
BookRepository bookRepository,
|
BookRepository bookRepository,
|
||||||
AuthorService authorService,
|
AuthorService authorService,
|
||||||
AuthorsBooksRepository authorsBooksRepository) {
|
AuthorsBooksRepository authorsBooksRepository,
|
||||||
|
UserRepository userRepository) {
|
||||||
this.bookRepository = bookRepository;
|
this.bookRepository = bookRepository;
|
||||||
this.authorService = authorService;
|
this.authorService = authorService;
|
||||||
this.authorsBooksRepository = authorsBooksRepository;
|
this.authorsBooksRepository = authorsBooksRepository;
|
||||||
|
this.userRepository = userRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkNameUniqueness(String name){
|
private void checkNameUniqueness(String name){
|
||||||
@ -55,6 +59,32 @@ public class BookService {
|
|||||||
return bookRepository.findByAuthorIdAndTypeId(authorId, typeId, pageRequest);
|
return bookRepository.findByAuthorIdAndTypeId(authorId, typeId, pageRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public Page<BookEntity> getAll(long typeId, long authorId, long userId, int page, int size) {
|
||||||
|
PageRequest pageRequest = PageRequest.of(page, size);
|
||||||
|
Page<BookEntity> result = null;
|
||||||
|
if (typeId <= 0L && authorId <= 0L) {
|
||||||
|
result = bookRepository.findAll(pageRequest);
|
||||||
|
}
|
||||||
|
else if (authorId <= 0L){
|
||||||
|
result = bookRepository.findByTypeId(typeId, pageRequest);
|
||||||
|
}
|
||||||
|
else if (typeId <= 0L) {
|
||||||
|
result = bookRepository.findByAuthorId(authorId, pageRequest);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
result = bookRepository.findByAuthorIdAndTypeId(authorId, typeId, pageRequest);
|
||||||
|
}
|
||||||
|
List<BookEntity> favorites = getUserFavorities(userId);
|
||||||
|
for (BookEntity book : result.getContent()) {
|
||||||
|
if (favorites.contains(book)) {
|
||||||
|
book.setIsChosen(true);
|
||||||
|
}
|
||||||
|
book.setFansNumber(book.getFans().size());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public List<BookEntity> getAll(long typeId, long authorId) {
|
public List<BookEntity> getAll(long typeId, long authorId) {
|
||||||
if (typeId <= 0L && authorId <= 0L)
|
if (typeId <= 0L && authorId <= 0L)
|
||||||
@ -122,6 +152,11 @@ public class BookService {
|
|||||||
return bookRepository.getBookSubscribersNumber(bookId);
|
return bookRepository.getBookSubscribersNumber(bookId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<BookEntity> getUserFavorities (long userId) {
|
||||||
|
return userRepository.getUserFavorities(userId);
|
||||||
|
}
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public List<AuthorEntity> getAuthors(long bookId) {
|
public List<AuthorEntity> getAuthors(long bookId) {
|
||||||
return bookRepository.getAuthors(bookId);
|
return bookRepository.getAuthors(bookId);
|
||||||
|
@ -99,9 +99,10 @@ public class UserBookController {
|
|||||||
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
|
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
|
||||||
@RequestParam(name = TYPE_ATTRIBUTE, defaultValue = "-1") Long typeId,
|
@RequestParam(name = TYPE_ATTRIBUTE, defaultValue = "-1") Long typeId,
|
||||||
@RequestParam(name = AUTHOR_ATTRIBUTE, defaultValue = "-1") Long authorId,
|
@RequestParam(name = AUTHOR_ATTRIBUTE, defaultValue = "-1") Long authorId,
|
||||||
Model model) {
|
Model model,
|
||||||
|
@AuthenticationPrincipal UserPrincipal principal) {
|
||||||
final Map<String, Object> attributes = PageAttributesMapper.toAttributes(
|
final Map<String, Object> attributes = PageAttributesMapper.toAttributes(
|
||||||
bookService.getAll(typeId, authorId, page, Constants.DEFUALT_PAGE_SIZE),
|
bookService.getAll(typeId, authorId, principal.getId(), page, Constants.DEFUALT_PAGE_SIZE),
|
||||||
this::toBookDto);
|
this::toBookDto);
|
||||||
model.addAllAttributes(attributes);
|
model.addAllAttributes(attributes);
|
||||||
model.addAttribute(TYPE_ATTRIBUTE, typeId);
|
model.addAttribute(TYPE_ATTRIBUTE, typeId);
|
||||||
|
@ -42,6 +42,7 @@
|
|||||||
<th scope="col" class="w-auto">Название</th>
|
<th scope="col" class="w-auto">Название</th>
|
||||||
<th scope="col" class="w-auto">Жанр</th>
|
<th scope="col" class="w-auto">Жанр</th>
|
||||||
<th scope="col" class="w-auto">Автор</th>
|
<th scope="col" class="w-auto">Автор</th>
|
||||||
|
<th scope="col" class="w-auto">Добавили в избранное</th>
|
||||||
<th scope="col" class="w-10"></th>
|
<th scope="col" class="w-10"></th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
@ -51,10 +52,12 @@
|
|||||||
<td th:text="${book.name}"></td>
|
<td th:text="${book.name}"></td>
|
||||||
<td th:text="${book.typeName}"></td>
|
<td th:text="${book.typeName}"></td>
|
||||||
<td th:text="${book.authorName}"></td>
|
<td th:text="${book.authorName}"></td>
|
||||||
|
<td th:text="${book.fansNumber}"></td>
|
||||||
<td>
|
<td>
|
||||||
<form th:action="@{/api/1.0/addFavorite/{id}(id=${book.id})}" method="post">
|
<form th:action="@{/api/1.0/addFavorite/{id}(id=${book.id})}" method="post">
|
||||||
<input type="hidden" th:name="page" th:value="${page}">
|
<input type="hidden" th:name="page" th:value="${page}">
|
||||||
<button type="submit" class="btn btn-link button-link">В избранное</button>
|
<button type="submit" class="btn btn-link button-link"
|
||||||
|
th:hidden="${book.isChosen}">В избранное</button>
|
||||||
</form>
|
</form>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user