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
Expand Up @@ -125,8 +125,10 @@ public PageResponseDto<PostResponseDto> getUserScrappedPosts(Long userId, PageRe
);
}

@Transactional

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The @Transactional annotation is correctly applied here to ensure that the incrementViewCount() operation, which modifies the Post entity, is part of a transaction. This is good practice for data consistency.

public PostDetailResponse getPostById(Long postId, User user) {
Post post = entityUtils.getEntity(postId, Post.class);
post.incrementViewCount();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Incrementing the view count directly within the getPostById method is a straightforward approach. However, consider if this operation should be asynchronous or debounced to prevent excessive database writes on rapid page refreshes, especially for high-traffic posts. For now, this is acceptable, but it's a potential area for optimization if performance becomes an issue.

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);
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/daramg/server/post/domain/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The addition of view_count with nullable = false and DEFAULT 0 is correctly implemented, ensuring data integrity and a sensible default value for new posts.


@Column(name = "is_blocked", nullable = false)
private boolean isBlocked = false;

Expand Down Expand Up @@ -133,6 +136,10 @@ public void incrementCommentCount(){
commentCount++;
}

public void incrementViewCount(){
viewCount++;
}
Comment on lines +139 to +141

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The incrementViewCount() method is a simple and effective way to encapsulate the logic for increasing the view count within the Post entity itself, adhering to the principles of object-oriented design.


public void softDelete() {
if (this.isDeleted) return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public record PostDetailResponse(
PostStatus postStatus,
int likeCount,
int commentCount,
int viewCount,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Adding viewCount to the PostDetailResponse record ensures that the client receives the updated view count when requesting post details. This is consistent with the feature's requirements.

boolean isBlocked,
LocalDateTime createdAt,
LocalDateTime updatedAt,
Expand Down Expand Up @@ -76,6 +77,7 @@ public static PostDetailResponse from(Post post, Boolean isLiked, Boolean isScra
post.getPostStatus(),
post.getLikeCount(),
post.getCommentCount(),
post.getViewCount(),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The post.getViewCount() is correctly mapped to the viewCount field in the PostDetailResponse.from method, ensuring the data is properly transferred to the DTO.

post.isBlocked(),
post.getCreatedAt(),
post.getUpdatedAt(),
Expand Down
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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The SQL migration script correctly adds the view_count column with INT NOT NULL DEFAULT 0, which aligns with the Post entity definition and ensures that existing posts will have a default view count of 0.

Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,7 @@ public class PostQueryControllerTest extends ControllerTestSupport {
PostStatus.PUBLISHED,
10,
5,
0,
false,
createdAt,
updatedAt,
Expand Down Expand Up @@ -704,6 +705,7 @@ public class PostQueryControllerTest extends ControllerTestSupport {
fieldWithPath("postStatus").type(JsonFieldType.STRING).description("포스트 상태 (PUBLISHED, DRAFT)"),
fieldWithPath("likeCount").type(JsonFieldType.NUMBER).description("좋아요 개수"),
fieldWithPath("commentCount").type(JsonFieldType.NUMBER).description("댓글 개수"),
fieldWithPath("viewCount").type(JsonFieldType.NUMBER).description("조회수"),
fieldWithPath("isBlocked").type(JsonFieldType.BOOLEAN).description("포스트 블락 여부"),
fieldWithPath("createdAt").type(JsonFieldType.STRING).description("생성일시 (ISO 8601 형식)"),
fieldWithPath("updatedAt").type(JsonFieldType.STRING).description("수정일시 (ISO 8601 형식)"),
Expand Down
Loading