From 517882d304f3d580a43d99590d6b754fb2860732 Mon Sep 17 00:00:00 2001 From: B-efore Date: Sat, 19 Jul 2025 21:34:19 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EA=B2=8C=EC=8B=9C=EA=B8=80=20=EB=8B=A8?= =?UTF-8?q?=EA=B1=B4=20=EC=A1=B0=ED=9A=8C=20=EC=BA=90=EC=8B=B1=20=EB=AF=B8?= =?UTF-8?q?=EC=A0=81=EC=9A=A9=20=EB=AC=B8=EC=A0=9C=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mylog/domain/post/controller/PostController.java | 6 +++++- .../jiwon/mylog/domain/post/service/PostService.java | 10 +--------- .../mylog/domain/post/service/PostViewService.java | 6 ++++-- .../domain/post/service/PostServiceCacheTest.java | 12 ++++++------ 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/jiwon/mylog/domain/post/controller/PostController.java b/src/main/java/com/jiwon/mylog/domain/post/controller/PostController.java index db9e3d2..de0ec24 100644 --- a/src/main/java/com/jiwon/mylog/domain/post/controller/PostController.java +++ b/src/main/java/com/jiwon/mylog/domain/post/controller/PostController.java @@ -1,6 +1,7 @@ package com.jiwon.mylog.domain.post.controller; import com.jiwon.mylog.domain.post.service.PostService; +import com.jiwon.mylog.domain.post.service.PostViewService; import com.jiwon.mylog.global.security.auth.annotation.AllUser; import com.jiwon.mylog.global.security.auth.annotation.LoginUser; import com.jiwon.mylog.domain.post.dto.request.PostRequest; @@ -37,6 +38,7 @@ public class PostController { private final PostService postService; + private final PostViewService postViewService; @PostMapping("/posts") @Operation( @@ -106,7 +108,9 @@ public ResponseEntity getAllNotices( public ResponseEntity getPost( @AllUser String userKey, @PathVariable("postId") Long postId) { - PostDetailResponse response = postService.getPost(postId, userKey); + PostDetailResponse response = postService.getPost(postId); + int view = postViewService.incrementPostView(response.getPostId(), response.getViews(), userKey); + response.setViews(view); return new ResponseEntity<>(response, HttpStatus.OK); } diff --git a/src/main/java/com/jiwon/mylog/domain/post/service/PostService.java b/src/main/java/com/jiwon/mylog/domain/post/service/PostService.java index f2ceb4f..fd7c024 100644 --- a/src/main/java/com/jiwon/mylog/domain/post/service/PostService.java +++ b/src/main/java/com/jiwon/mylog/domain/post/service/PostService.java @@ -39,7 +39,6 @@ public class PostService { private final ApplicationEventPublisher eventPublisher; - private final PostViewService postViewService; private final TagService tagService; private final PostRepository postRepository; private final UserRepository userRepository; @@ -168,18 +167,11 @@ public PageResponse getAllNotices(Pageable pageable) { condition = "#postId != null && #postId > 0" ) @Transactional(readOnly = true) - public PostDetailResponse getPostContent(Long postId) { + public PostDetailResponse getPost(Long postId) { return postRepository.findPostDetail(postId) .orElseThrow(() -> new NotFoundException(ErrorCode.NOT_FOUND_POST)); } - public PostDetailResponse getPost(Long postId, String userKey) { - PostDetailResponse post = getPostContent(postId); - postViewService.incrementPostView(post.getPostId(), post.getViews(), userKey); - post.setViews(postViewService.getPostView(post.getPostId(), post.getViews())); - return post; - } - @Cacheable(value = "post::main", key = "'page:' + #pageable.pageNumber + ':size:' + #pageable.pageSize", condition = "#pageable != null") diff --git a/src/main/java/com/jiwon/mylog/domain/post/service/PostViewService.java b/src/main/java/com/jiwon/mylog/domain/post/service/PostViewService.java index c07e368..0f18923 100644 --- a/src/main/java/com/jiwon/mylog/domain/post/service/PostViewService.java +++ b/src/main/java/com/jiwon/mylog/domain/post/service/PostViewService.java @@ -12,7 +12,7 @@ public class PostViewService { private static final String VIEW_KEY_PREFIX = "post:view:"; private static final String VIEW_COUNT_KEY_PREFIX = "post:view:count"; - public void incrementPostView(Long postId, int view, String userKey) { + public int incrementPostView(Long postId, int view, String userKey) { String existKey = VIEW_KEY_PREFIX + postId; String countKey = VIEW_COUNT_KEY_PREFIX; @@ -21,9 +21,11 @@ public void incrementPostView(Long postId, int view, String userKey) { redisUtil.addPostViewUser(existKey, userKey); redisUtil.increasePostView(countKey, postId.toString(), String.valueOf(view)); } + + return getPostView(postId, view); } - public int getPostView(Long postId, int view) { + private int getPostView(Long postId, int view) { return redisUtil.getPostView(VIEW_COUNT_KEY_PREFIX, postId.toString(), view); } } diff --git a/src/test/java/com/jiwon/mylog/domain/post/service/PostServiceCacheTest.java b/src/test/java/com/jiwon/mylog/domain/post/service/PostServiceCacheTest.java index 214de3e..bf151ad 100644 --- a/src/test/java/com/jiwon/mylog/domain/post/service/PostServiceCacheTest.java +++ b/src/test/java/com/jiwon/mylog/domain/post/service/PostServiceCacheTest.java @@ -60,7 +60,7 @@ void createPost() { PostDetailResponse post = postService.createPost(userId, postRequest, false); Long postId = post.getPostId(); - postService.getPost(postId, "dummy"); + postService.getPost(postId); postService.getUserPosts(userId, pageable); // 캐시 다시 생성 postService.getPostsByCategoryAndTags(userId, categoryId, List.of(), pageable); // 캐시 다시 생성 @@ -88,7 +88,7 @@ void updatePost() { // when PostDetailResponse post = postService.updatePost(userId, postId, postRequest, false); - postService.getPost(postId, "dummy"); + postService.getPost(postId); postService.getUserPosts(userId, pageable); // 캐시 다시 생성 postService.getPostsByCategoryAndTags(userId, categoryId, List.of(), pageable); // 캐시 다시 생성 @@ -113,11 +113,11 @@ void deletePost() { .willReturn(Optional.empty()); // when - postService.getPost(postId, "dummy"); + postService.getPost(postId); postService.deletePost(userId, postId); // then - assertThrows(NotFoundException.class, () -> postService.getPost(postId, "dummy")); + assertThrows(NotFoundException.class, () -> postService.getPost(postId); verify(postRepository, times(2)).findPostDetail(postId); } @@ -131,10 +131,10 @@ void getPost() { .willReturn(Optional.of(post)); // when & then - PostDetailResponse firstPost = postService.getPost(postId, "dummy"); + PostDetailResponse firstPost = postService.getPost(postId); verify(postRepository, times(1)).findPostDetail(postId); - PostDetailResponse secondPost = postService.getPost(postId, "dummy"); + PostDetailResponse secondPost = postService.getPost(postId); verify(postRepository, times(1)).findPostDetail(postId); assertThat(firstPost).isEqualTo(secondPost);