Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,14 @@ public List<Post> findAllByIds(List<Long> postIds) {
public void incrementViewCount(Long postId, int delta){
postJpaRepository.incrementViewCount(postId,delta);
}

@Override
public void incrementLikeCount(Long postId){
postJpaRepository.incrementLikeCount(postId);
}

@Override
public void decrementLikeCount(Long postId){
postJpaRepository.decrementLikeCount(postId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,12 @@ Page<Post> findAllByHashtagIdAndVisibilityPublic(
@Modifying
@Query("UPDATE Post p SET p.viewCount = p.viewCount + :delta WHERE p.postId = :postId")
void incrementViewCount(@Param("postId") Long postId, @Param("delta") int delta);

@Modifying
@Query("UPDATE Post p Set p.likeCount = p.likeCount + 1 WHERE p.postId = :postId")
void incrementLikeCount(@Param("postId") Long postId);

@Modifying
@Query("UPDATE Post p SET p.likeCount = p.likeCount - 1 WHERE p.postId = :postId AND p.likeCount > 0")
void decrementLikeCount(@Param("postId") Long postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ public interface PostRepository {
List<Post> findAllByIds(List<Long> postIds);

void incrementViewCount(Long postId, int delta);

void incrementLikeCount(Long postId);
void decrementLikeCount(Long postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,18 @@ public LikeResponseDTO toggleLike(Long postId, Long userId) {

if (existingLike.isPresent()) {
postLikeRepository.delete(existingLike.get());
post.decrementLikeCount();
postRepository.save(post);
return LikeResponseDTO.of(postId, false, post.getLikeCount());
postRepository.decrementLikeCount(postId);
return LikeResponseDTO.of(postId, false, post.getLikeCount() - 1);
} else {
blockService.validateNotBlocked(userId, post.getUserId());
PostLike postLike = PostLike.create(userId, postId);
postLikeRepository.save(postLike);
post.incrementLikeCount();
postRepository.save(post);
postRepository.incrementLikeCount(postId);

notificationService.send(
NotificationType.LIKE,userId,post.getUserId(),postId, ReferenceType.POST
NotificationType.LIKE, userId, post.getUserId(), postId, ReferenceType.POST
);
return LikeResponseDTO.of(postId, true, post.getLikeCount());
return LikeResponseDTO.of(postId, true, post.getLikeCount() + 1);
}
}

Expand Down
Loading