Skip to content

Commit

Permalink
Merge pull request #158 from Lixuhuilll/comment-dislike
Browse files Browse the repository at this point in the history
评论响应点踩数量
  • Loading branch information
LuoRenMu authored Jan 9, 2024
2 parents 7b7963d + 0504e3e commit 022e48b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@
@Mapper(componentModel = "spring")
public interface CommentConverter {

@Mapping(target = "like", source = "likeCount")
@Mapping(target = "like", source = "commentsArea.likeCount")
@Mapping(target = "dislike", source = "commentsArea.dislikeCount")
@Mapping(target = "uploader", source = "maaUser.userName")
@Mapping(target = "commentId", source = "id")
@Mapping(target = "commentId", source = "commentsArea.id")
@Mapping(target = "subCommentsInfos", ignore = true)
CommentsInfo toCommentsInfo(CommentsArea commentsArea, String id, int likeCount, MaaUser maaUser);
CommentsInfo toCommentsInfo(CommentsArea commentsArea, MaaUser maaUser);


@Mapping(target = "like", source = "likeCount")
@Mapping(target = "like", source = "commentsArea.likeCount")
@Mapping(target = "dislike", source = "commentsArea.dislikeCount")
@Mapping(target = "uploader", source = "maaUser.userName")
@Mapping(target = "commentId", source = "id")
@Mapping(target = "deleted", source = "delete")
SubCommentsInfo toSubCommentsInfo(CommentsArea commentsArea, String id, int likeCount, MaaUser maaUser, boolean delete);
@Mapping(target = "commentId", source = "commentsArea.id")
@Mapping(target = "deleted", source = "commentsArea.delete")
SubCommentsInfo toSubCommentsInfo(CommentsArea commentsArea, MaaUser maaUser);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public class CommentsInfo {
//评论内容
private String message;
private LocalDateTime uploadTime;
private int like;
private long like;
private long dislike;
private boolean topping;
private List<SubCommentsInfo> subCommentsInfos = new ArrayList<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public class SubCommentsInfo {
//评论内容
private String message;
private LocalDateTime uploadTime;
private int like;
private long like;
private long dislike;
private String fromCommentId;
private String mainCommentId;
private boolean deleted;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class CommentsArea implements Serializable {

private long likeCount;

private long dislikeCount;

private LocalDateTime uploadTime = LocalDateTime.now();

// 是否将该评论置顶
Expand Down
25 changes: 16 additions & 9 deletions src/main/java/plus/maa/backend/service/CommentsAreaService.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,9 @@ public void rates(String userId, CommentsRatingDTO commentsRatingDTO) {

CommentsArea commentsArea = findCommentsById(commentsRatingDTO.getCommentId());

long change;
long likeCountChange;
long dislikeCountChange;

Optional<Rating> ratingOptional = ratingRepository.findByTypeAndKeyAndUserId(Rating.KeyType.COMMENT, commentsArea.getId(), userId);
// 判断该用户是否存在评分
if (ratingOptional.isPresent()) {
Expand All @@ -204,8 +206,10 @@ public void rates(String userId, CommentsRatingDTO commentsRatingDTO) {
ratingOptional.get().setRateTime(LocalDateTime.now());
RatingType newRatingType = ratingRepository.save(ratingOptional.get()).getRating();
// 更新评分后更新评论的点赞数
change = newRatingType == RatingType.LIKE ? 1 :
likeCountChange = newRatingType == RatingType.LIKE ? 1 :
(oldRatingType != RatingType.LIKE ? 0 : -1);
dislikeCountChange = newRatingType == RatingType.DISLIKE ? 1 :
(oldRatingType != RatingType.DISLIKE ? 0 : -1);
} else {
// 如果评分未发生变化则结束
return;
Expand All @@ -220,15 +224,23 @@ public void rates(String userId, CommentsRatingDTO commentsRatingDTO) {
.setRateTime(LocalDateTime.now());

ratingRepository.insert(newRating);
change = newRating.getRating() == RatingType.LIKE ? 1 : 0;
likeCountChange = newRating.getRating() == RatingType.LIKE ? 1 : 0;
dislikeCountChange = newRating.getRating() == RatingType.DISLIKE ? 1 : 0;
}

// 点赞数不需要在高并发下特别精准,大概就行,但是也得避免特别离谱的数字
long likeCount = commentsArea.getLikeCount() + change;
long likeCount = commentsArea.getLikeCount() + likeCountChange;
if (likeCount < 0) {
likeCount = 0;
}

long dislikeCount = commentsArea.getDislikeCount() + dislikeCountChange;
if (dislikeCount < 0) {
dislikeCount = 0;
}

commentsArea.setLikeCount(likeCount);
commentsArea.setDislikeCount(dislikeCount);

commentsAreaRepository.save(commentsArea);
}
Expand Down Expand Up @@ -328,8 +340,6 @@ public CommentsAreaInfo queriesCommentsArea(CommentsQueriesDTO request) {
commentConverter
.toCommentsInfo(
mainComment
, mainComment.getId()
, (int) mainComment.getLikeCount()
, maaUserMap.getOrDefault(
mainComment.getUploaderId()
, UNKNOWN_USER
Expand All @@ -343,14 +353,11 @@ public CommentsAreaInfo queriesCommentsArea(CommentsQueriesDTO request) {
commentConverter
.toSubCommentsInfo(
subComment
, subComment.getId()
, (int) subComment.getLikeCount()
//填充评论用户名
, maaUserMap.getOrDefault(
subComment.getUploaderId(),
UNKNOWN_USER
)
, subComment.isDelete()
)
).toList();

Expand Down

0 comments on commit 022e48b

Please sign in to comment.