diff --git a/src/main/java/com/example/solidconnection/post/domain/Post.java b/src/main/java/com/example/solidconnection/post/domain/Post.java index 287b255a8..203feb5a9 100644 --- a/src/main/java/com/example/solidconnection/post/domain/Post.java +++ b/src/main/java/com/example/solidconnection/post/domain/Post.java @@ -97,9 +97,4 @@ public void update(PostUpdateRequest postUpdateRequest) { this.content = postUpdateRequest.content(); this.category = PostCategory.valueOf(postUpdateRequest.postCategory()); } - - public void increaseViewCount(Long updateViewCount) { - this.viewCount += updateViewCount; - } - } diff --git a/src/main/java/com/example/solidconnection/post/repository/PostRepository.java b/src/main/java/com/example/solidconnection/post/repository/PostRepository.java index f5c10875c..b819cc45a 100644 --- a/src/main/java/com/example/solidconnection/post/repository/PostRepository.java +++ b/src/main/java/com/example/solidconnection/post/repository/PostRepository.java @@ -38,4 +38,9 @@ default Post getById(Long id) { @Query("UPDATE Post p SET p.likeCount = p.likeCount + 1 " + "WHERE p.id = :postId") void increaseLikeCount(@Param("postId") Long postId); + + @Modifying(clearAutomatically = true, flushAutomatically = true) + @Query("UPDATE Post p SET p.viewCount = p.viewCount + :count " + + "WHERE p.id = :postId") + void increaseViewCount(@Param("postId") Long postId, @Param("count") Long count); } diff --git a/src/main/java/com/example/solidconnection/service/UpdateViewCountService.java b/src/main/java/com/example/solidconnection/service/UpdateViewCountService.java index 46954fff6..55d4d9eba 100644 --- a/src/main/java/com/example/solidconnection/service/UpdateViewCountService.java +++ b/src/main/java/com/example/solidconnection/service/UpdateViewCountService.java @@ -26,7 +26,7 @@ public void updateViewCount(String key) { log.info("updateViewCount Processing key: {} in thread: {}", key, Thread.currentThread().getName()); Long postId = redisUtils.getPostIdFromPostViewCountRedisKey(key); Post post = postRepository.getById(postId); - post.increaseViewCount(redisService.getAndDelete(key)); + postRepository.increaseViewCount(postId, redisService.getAndDelete(key)); log.info("updateViewCount Updated post id: {} with view count from key: {}", postId, key); } }