-
Notifications
You must be signed in to change notification settings - Fork 0
[feat] posts 조회수(view_count) 기능 추가 #87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -125,8 +125,10 @@ public PageResponseDto<PostResponseDto> getUserScrappedPosts(Long userId, PageRe | |
| ); | ||
| } | ||
|
|
||
| @Transactional | ||
| public PostDetailResponse getPostById(Long postId, User user) { | ||
| Post post = entityUtils.getEntity(postId, Post.class); | ||
| post.incrementViewCount(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incrementing the view count directly within the |
||
| Boolean isLiked = user != null ? postLikeRepository.existsByPostIdAndUserId(postId, user.getId()) : null; | ||
| Boolean isScrapped = user != null ? postScrapRepository.existsByPostIdAndUserId(postId, user.getId()) : null; | ||
| List<Comment> comments = commentRepository.findByPostIdAndIsBlockedFalseOrderByCreatedAtAsc(postId); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,6 +53,9 @@ public abstract class Post extends BaseEntity<Post> { | |
| @Column(name = "comment_count", nullable = false) | ||
| private int commentCount = 0; | ||
|
|
||
| @Column(name = "view_count", nullable = false) | ||
| private int viewCount = 0; | ||
|
Comment on lines
+56
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| @Column(name = "is_blocked", nullable = false) | ||
| private boolean isBlocked = false; | ||
|
|
||
|
|
@@ -133,6 +136,10 @@ public void incrementCommentCount(){ | |
| commentCount++; | ||
| } | ||
|
|
||
| public void incrementViewCount(){ | ||
| viewCount++; | ||
| } | ||
|
Comment on lines
+139
to
+141
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| public void softDelete() { | ||
| if (this.isDeleted) return; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ public record PostDetailResponse( | |
| PostStatus postStatus, | ||
| int likeCount, | ||
| int commentCount, | ||
| int viewCount, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| boolean isBlocked, | ||
| LocalDateTime createdAt, | ||
| LocalDateTime updatedAt, | ||
|
|
@@ -76,6 +77,7 @@ public static PostDetailResponse from(Post post, Boolean isLiked, Boolean isScra | |
| post.getPostStatus(), | ||
| post.getLikeCount(), | ||
| post.getCommentCount(), | ||
| post.getViewCount(), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| post.isBlocked(), | ||
| post.getCreatedAt(), | ||
| post.getUpdatedAt(), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ALTER TABLE posts ADD COLUMN view_count INT NOT NULL DEFAULT 0; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
@Transactionalannotation is correctly applied here to ensure that theincrementViewCount()operation, which modifies thePostentity, is part of a transaction. This is good practice for data consistency.