Skip to content

Commit

Permalink
fix: 조회 수 post도 증가하도록 수정 (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
toychip committed Jan 15, 2024
1 parent efa60f2 commit a8d9f94
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,8 @@ public String getCreatedTime() {
return TimeUtil.formatCreatedDate(createdDate);
}

public void addCount() {
viewCount++;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,13 @@ public void registerTest(final PostCreateRequest request, final Member member) {

/* --------------------------------- READ Single --------------------------------- */

@Transactional
public PostResponse getSinglePost(final Long postId) {
Post post = getPostFetchJoin(postId);
PostTier postTier = post.getPostTier();

validateAuthority(postTier);
addViewCount(postId);
addViewCount(post);

CommentListResponse commentResponses = commentService.findComments(postId);
return postResponse(post, commentResponses);
Expand All @@ -128,8 +129,12 @@ private Post getPostFetchJoin(final Long postId) {
.orElseThrow(() -> new ApiException(ErrorType._POST_NOT_FOUND));
}

private void addViewCount(Long postId) {
viewService.checkAndAddViewHistory(postId);
private void addViewCount(Post post) {
Long postId = post.getId();
boolean isAlreadyView = viewService.checkAndAddViewHistory(postId);
if (!isAlreadyView) {
post.addCount();
}
}

/* --------------------------------- READ Paging --------------------------------- */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public class ViewHistory {
private Long id;

private Long postId;
private Long userId;
private Long memberId;
private LocalDateTime viewTime;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
import org.springframework.data.jpa.repository.JpaRepository;

public interface ViewHistoryRepository extends JpaRepository<ViewHistory, Long> {
boolean existsByPostIdAndUserId(Long postId, Long userId);
boolean existsByPostIdAndMemberId(Long postId, Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,35 @@
import java.time.LocalDateTime;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class ViewService {

private final ViewHistoryRepository viewHistoryRepository;

@Transactional
public void checkAndAddViewHistory(final Long postId) {
public boolean checkAndAddViewHistory(final Long postId) {
Long currentMemberId = getCurrentMember().getId();
boolean isAlreadyViewed = isAlreadyViewed(postId, currentMemberId);
if (!isAlreadyViewed) {
register(postId, currentMemberId);
return false;
}
return true;
}

private Member getCurrentMember() {
return SecurityUtil.getCurrentMember();
}

private boolean isAlreadyViewed(final Long postId, final Long userId) {
return viewHistoryRepository.existsByPostIdAndUserId(postId, userId);
return viewHistoryRepository.existsByPostIdAndMemberId(postId, userId);
}

public void register(final Long postId, final Long memberId) {
ViewHistory viewHistory = ViewHistory.builder()
.postId(postId)
.userId(memberId)
.memberId(memberId)
.viewTime(LocalDateTime.now())
.build();

Expand Down

0 comments on commit a8d9f94

Please sign in to comment.