From 88342653287fa4734d440b8b49766a68191f8d4a Mon Sep 17 00:00:00 2001 From: sewon Date: Sat, 7 Sep 2024 13:43:33 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EC=A1=B0=ED=9A=8C=EC=88=98=20=EA=B0=B1?= =?UTF-8?q?=EC=8B=A0=20=EC=8B=9C=20=EB=B3=80=EA=B2=BD=EA=B0=90=EC=A7=80=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9=ED=95=98=EC=A7=80=20=EC=95=8A=EA=B3=A0=20?= =?UTF-8?q?=EC=A7=81=EC=A0=91=20update=20=EC=BF=BC=EB=A6=AC=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/example/solidconnection/post/domain/Post.java | 5 ----- .../solidconnection/post/repository/PostRepository.java | 5 +++++ .../solidconnection/service/UpdateViewCountService.java | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) 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); } }