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
@@ -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;
Expand Down Expand Up @@ -37,6 +38,7 @@
public class PostController {

private final PostService postService;
private final PostViewService postViewService;

@PostMapping("/posts")
@Operation(
Expand Down Expand Up @@ -106,7 +108,9 @@ public ResponseEntity<PageResponse> getAllNotices(
public ResponseEntity<PostDetailResponse> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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); // 캐시 다시 생성

Expand Down Expand Up @@ -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); // 캐시 다시 생성

Expand All @@ -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);
}

Expand All @@ -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);
Expand Down