У меня есть следующий запрос, который подсчитывает, нравится или не нравится из таблицы OpinionProduct
@Query(value = "SELECT new org.computerShop.dto.CommentsDto(m,count(ml), (sum(case when ml = :user then 1 else 0 end) > 0), count(ds), (sum (case when ds =:user then 1 else 0 end)>0)) " +
"FROM OpinionProduct m left join m.likes ml left join m.dislikes ds where m.commentToProduct.id = :productId group by m order by m.id DESC ")
Page<CommentsDto> findAllForProduct(@Param("productId") long productId, Pageable pageable,@Param("user") User user);
Соединения в сущности OpinionProduct:
@ManyToMany
@JoinTable(
name = "opinions_likes",
joinColumns = @JoinColumn(name = "opinion_id"),
inverseJoinColumns = @JoinColumn(name = "user_id")
)
private Set<User> likes = new HashSet<>();
@ManyToMany
@JoinTable(name = "opinions_dislikes",
joinColumns = @JoinColumn(name = "opinion_id"),
inverseJoinColumns = @JoinColumn(name = "user_id")
)
private Set<User> dislikes = new HashSet<>();
Мои комментарииDTO, которые содержат подробности о комментарии:
private Long id;
private String text;
private Product commentToProduct;
private String advantages;
private String disadvantages;
private Long likes;
private Long dislikes;
private boolean meLiked;
private boolean meDisliked;
private LocalDateTime dateTime;
private short rating;
public CommentsDto(OpinionProduct opinionProduct, Long likes, boolean meLiked, Long dislikes, boolean meDisliked) {
this.id = opinionProduct.getId();
this.text = opinionProduct.getText();
this.advantages = opinionProduct.getAdvantages();
this.disadvantages = opinionProduct.getDisadvantages();
this.likes = likes;
this.meLiked = meLiked;
this.dateTime = opinionProduct.getDateTime();
this.rating = opinionProduct.getRating();
this.commentToProduct = opinionProduct.getCommentToProduct();
this.dislikes = dislikes;
this.meDisliked = meDisliked;
}
Получить мнения:
public OpinionsDto getOpinionForProduct(long productId, int page, User user) {
Pageable pageable = PageRequest.of(page, 5);
Page<CommentsDto> products = opinionProductRepo.findAllForProduct(productId,pageable,user);
return new OpinionsDto(products.toList(), pageable.getPageNumber(), products.getTotalPages(), opinionProductRepo.findAllForProduct(productId).size());
}
Мнения DTO:
private List<CommentsDto> comments;
private int currentPage;
private int totalPages;
private int commentsCounter;
public OpinionsDto(List<CommentsDto> comments, int currentPage, int totalPages, int commentsCounter) {
this.comments = comments;
this.currentPage = currentPage;
this.totalPages = totalPages;
this.commentsCounter = commentsCounter;
}
Установить лайки:
public ResponseEntity<String> setLike(User user, OpinionProduct opinionProduct) {
Set<User> likes = opinionProduct.getLikes();
Set<User> dislikes = opinionProduct.getDislikes();
if(dislikes.contains(user)){
dislikes.remove(user);
}
if(likes.contains(user)){
likes.remove(user);
}else{
likes.add(user);
}
opinionProductRepo.save(opinionProduct);
return new ResponseEntity<>("Ok", HttpStatus.OK);
}
Установить антипатии:
public ResponseEntity<String> setDislike(User user, OpinionProduct opinionProduct) {
Set<User> dislikes = opinionProduct.getDislikes();
Set<User> likes = opinionProduct.getLikes();
if(likes.contains(user)){
likes.remove(user);
}
if(dislikes.contains(user)){
dislikes.remove(user);
}else{
dislikes.add(user);
}
opinionProductRepo.save(opinionProduct);
return new ResponseEntity<>("Ok", HttpStatus.OK);
}
Но когда я нажимаю на "кнопка, как", а затем из другой учетной записи на "неприязнь", счет работает неправильно.
Живой пример: https://youtu.be/Lx8bNUCvXG4