Skip to content

Commit

Permalink
Merge pull request #144 from Me1tingPot/feature/#143
Browse files Browse the repository at this point in the history
[FIX] ์ปค๋ฎค๋‹ˆํ‹ฐ ๋ฒ„๊ทธ ์ˆ˜์ •
  • Loading branch information
moonyaeyoon committed Sep 20, 2024
2 parents 8fb9cf4 + 28b1271 commit 70d1465
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

public interface PostRepository extends JpaRepository<Post, Long> {

@Query("SELECT p FROM Post p WHERE p.postType = :postType AND (:cursor IS NULL OR p.id > :cursor) ORDER BY p.id ASC")
@Query("SELECT p FROM Post p WHERE p.postType = :postType AND p.isDraft = false AND (:cursor IS NULL OR p.id > :cursor) ORDER BY p.id ASC")
List<Post> findByPostTypeAndCursor(@Param("postType") PostType postType, @Param("cursor") Long cursor, Pageable pageable);

Slice<Post> findAllByAccountAndDeletedAtIsNullOrderByIdDesc(Account account, Pageable page);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ public ResponseEntity<ResponseData<PostsListResponse>> getPostList(@CurrentUser

@GetMapping("/{postId}")
@Operation(summary = "์ปค๋ฎค๋‹ˆํ‹ฐ ๊ธ€ ๋‚ด์šฉ ์กฐํšŒ", description = "postId๋กœ ์ปค๋ฎค๋‹ˆํ‹ฐ ๊ธ€ ๋‚ด์šฉ์„ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.")
public ResponseEntity<ResponseData<PostDetailResponse>> getPostDetail(@CurrentUser Account account, @PathVariable Long postId, @RequestParam(required = false, name = "cursor") Long cursor, @RequestParam(name = "pageSize") Integer pageSize) {
public ResponseEntity<ResponseData<PostDetailResponse>> getPostDetail(@CurrentUser Account account, @PathVariable Long postId) {
try{
return ResponseData.toResponseEntity(ResponseCode.POST_DETAIL_FETCH_SUCCEESS,postService.getPostDetail(postId,cursor, pageSize));
return ResponseData.toResponseEntity(ResponseCode.POST_DETAIL_FETCH_SUCCEESS,postService.getPostDetail(postId));
}catch (NoSuchElementException e) {
return ResponseData.toResponseEntity(ResponseCode.POST_NOT_FOUND, null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package meltingpot.server.post.dto;

import lombok.*;
import meltingpot.server.comment.dto.CommentsListResponse;
import meltingpot.server.domain.entity.post.Post;
import java.time.LocalDateTime;
import java.util.List;
Expand All @@ -13,12 +12,12 @@
@Builder
public class PostDetailResponse {
private Long postId;
private Long userId;
private String name;
private String title;
private String content;
private List<ImageData> imgData; // Image data including ID and URL
private Integer commentCount;
private CommentsListResponse commentsList;
private LocalDateTime updatedAt;

// Static nested class to hold image data
Expand All @@ -30,7 +29,7 @@ public static class ImageData {
private String imageUrl;
}

public static PostDetailResponse of(Post post, CommentsListResponse commentsList) {
public static PostDetailResponse of(Post post) {
List<ImageData> imgData = post.getPostImages().stream()
.map(postImage -> new ImageData(postImage.getId(), postImage.getImageUrl()))
.collect(Collectors.toList());
Expand All @@ -41,12 +40,12 @@ public static PostDetailResponse of(Post post, CommentsListResponse commentsList

return PostDetailResponse.builder()
.postId(post.getId())
.userId(post.getAccount().getId())
.name(post.getAccount().getName())
.title(post.getTitle())
.content(post.getContent())
.imgData(imgData)
.commentCount(commentCount)
.commentsList(commentsList)
.updatedAt(post.getUpdatedAt())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.*;
import meltingpot.server.domain.entity.post.Post;
import meltingpot.server.domain.entity.post.PostImage;
import meltingpot.server.domain.entity.AccountProfileImage;

import java.time.LocalDateTime;
import java.util.List;
Expand All @@ -25,6 +26,7 @@ public class PostsListResponse {
public static class PostsList {
private Long postId;
private Long userId;
private String profileImg;
private String name;
private String title;
private String content;
Expand All @@ -36,9 +38,15 @@ public static PostsList from(Post post) {
List<String> imgUrls = post.getPostImages().stream()
.map(PostImage::getImageUrl)
.collect(Collectors.toList());
String profileImgKey = post.getAccount().getProfileImages().stream()
.filter(AccountProfileImage::isThumbnail)
.map(AccountProfileImage::getImageKey)
.findFirst()
.orElse(null);
return PostsList.builder()
.postId(post.getId())
.userId(post.getAccount().getId())
.profileImg(profileImgKey)
.name(post.getAccount().getName())
.title(post.getTitle())
.content(post.getContent())
Expand Down
79 changes: 7 additions & 72 deletions src/main/java/meltingpot/server/post/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@


import lombok.*;

import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.stream.Collectors;

import meltingpot.server.comment.dto.CommentsListResponse;
import meltingpot.server.domain.entity.Account;
import meltingpot.server.domain.entity.comment.Comment;
import meltingpot.server.domain.entity.post.Post;
import meltingpot.server.domain.entity.enums.PostType;
import meltingpot.server.domain.entity.post.PostImage;
Expand Down Expand Up @@ -52,13 +48,7 @@ public ResponseCode createPost(PostCreateRequest postCreateRequest, Account acco
updatePostContent(post, account, postCreateRequest);
}
setPostImages(post, account, postCreateRequest);
// isDraft ๊ฐ’์„ ์„ค์ •ํ•˜๊ธฐ ์ „์— ํ˜„์žฌ ์ƒํƒœ ์ถœ๋ ฅ
System.out.println("Setting isDraft for post with ID " + post.getId() + " to " + isDraft);
post.setIsDraft(isDraft);

// isDraft ๊ฐ’ ์„ค์ • ํ›„ ์ถœ๋ ฅ
System.out.println("Post isDraft status: " + post.getIsDraft());

postRepository.save(post);

return isDraft ? ResponseCode.DRAFT_SAVE_SUCCESS : ResponseCode.CREATE_POST_SUCCESS;
Expand All @@ -79,10 +69,12 @@ public ResponseCode updatePost(PostCreateRequest updateRequest,Long postId, Acco

/*post ๋‚ด์šฉ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ*/
@Transactional(readOnly = true)
public PostDetailResponse getPostDetail(Long postId, Long cursor, int pageSize){
public PostDetailResponse getPostDetail(Long postId){
Post post = findPostById(postId);
CommentsListResponse commentsList = fetchCommentsList(postId, cursor, pageSize);
return PostDetailResponse.of(post,commentsList);
if(post.getIsDraft()){
throw new IllegalStateException("๊ฒŒ์‹œ๋ฌผ ์กฐํšŒ ์‹คํŒจ");
}
return PostDetailResponse.of(post);
}

/*post ๋ชฉ๋ก ๋ถˆ๋Ÿฌ์˜ค๊ธฐ*/
Expand Down Expand Up @@ -171,65 +163,6 @@ private List<PostImage> createPostImages(List<String> imageKeys, Post post, Acco
.collect(Collectors.toList());
}

private CommentsListResponse fetchCommentsList(Long postId, Long cursor, int pageSize) {
List<CommentsListResponse.CommentDetail> commentDetailDTOs = new ArrayList<>();
int count = 0;
Long parentCursor = null;

// Cursor๊ฐ€ ์ž์‹ ๋Œ“๊ธ€์— ํ•ด๋‹นํ•˜๋Š” ๊ฒฝ์šฐ ์ฒ˜๋ฆฌ
if (cursor != null) {
Comment cursorComment = commentRepository.findById(cursor).orElse(null);
if (cursorComment != null) {
Comment parentComment;
if (cursorComment.getParent() != null) {
// Cursor๊ฐ€ ์ž์‹ ๋Œ“๊ธ€์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒฝ์šฐ
parentComment = cursorComment.getParent();
} else {
// Cursor๊ฐ€ ๋ถ€๋ชจ ๋Œ“๊ธ€์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒฝ์šฐ
parentComment = cursorComment;
}

List<Comment> remainingChildren = commentRepository.findChildrenCommentsByParentId(parentComment.getId(), cursor);
for (Comment child : remainingChildren) {
if (count >= pageSize) break;
commentDetailDTOs.add(CommentsListResponse.CommentDetail.from(child));
count++;
}
parentCursor = parentComment.getId();

// ๋งŒ์•ฝ ์ž์‹ ๋Œ“๊ธ€์„ ๋ชจ๋‘ ๊ฐ€์ ธ์™”๊ณ , ํŽ˜์ด์ง€๊ฐ€ ๊ฝ‰ ์ฐจ์ง€ ์•Š์•˜๋‹ค๋ฉด ๋‹ค์Œ ๋ถ€๋ชจ ๋Œ“๊ธ€๋กœ ๋„˜์–ด๊ฐ
if (count < pageSize && remainingChildren.size() < pageSize) {
parentCursor = parentComment.getId();
}
} else {
parentCursor = cursor; // cursor๊ฐ€ ๋ถ€๋ชจ ๋Œ“๊ธ€์ธ ๊ฒฝ์šฐ
}
}

// ๋ถ€๋ชจ ๋Œ“๊ธ€๊ณผ ์ž์‹ ๋Œ“๊ธ€์„ ๊ฐ€์ ธ์˜ค๋Š” ์ฒ˜๋ฆฌ
if (count < pageSize) {
Pageable pageable = PageRequest.of(0, pageSize - count);
List<Comment> parentComments = commentRepository.findParentCommentsByPostId(postId, parentCursor, pageable);
for (Comment parent : parentComments) {
if (count >= pageSize) break;
commentDetailDTOs.add(CommentsListResponse.CommentDetail.from(parent));
count++;

List<Comment> children = commentRepository.findChildrenCommentsByParentId(parent.getId(), null);
for (Comment child : children) {
if (count >= pageSize) break;
commentDetailDTOs.add(CommentsListResponse.CommentDetail.from(child));
count++;
}
}
}

Long nextCursor = (count < pageSize) ? null : commentDetailDTOs.get(commentDetailDTOs.size() - 1).getCommentId();
boolean isLast = (count < pageSize);

return CommentsListResponse.from(commentDetailDTOs, nextCursor, isLast);
}

private Optional<Post> getDraftPost(Account account) {
return postRepository.findByAccountAndIsDraftTrue(account);
}
Expand All @@ -238,5 +171,7 @@ private void setPostImages(Post post, Account account,PostCreateRequest postRequ
List<PostImage> postImages = createPostImages(postRequest.getImageKeys(), post, account);
post.setPostImages(postImages);
}


}

1 change: 1 addition & 0 deletions src/main/java/meltingpot/server/util/ResponseCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public enum ResponseCode {
POST_CREATE_FAIL(BAD_REQUEST, "๊ฒŒ์‹œ๊ธ€ ์ž‘์„ฑ ์‹คํŒจ"),
POST_UPDATE_FAIL(BAD_REQUEST,"๊ฒŒ์‹œ๊ธ€ ์ˆ˜์ • ์‹คํŒจ"),
POST_DELETE_FAIL(BAD_REQUEST,"๊ฒŒ์‹œ๊ธ€ ์‚ญ์ œ ์‹คํŒจ"),
POST_FETCH_FAIL(BAD_REQUEST,"๊ฒŒ์‹œ๊ธ€ ์กฐํšŒ ์‹คํŒจ"),
REPORT_CREATE_FAIL(BAD_REQUEST, "์‹ ๊ณ  ์ž‘์„ฑ ์‹คํŒจ"),
AREA_FETCH_FAILED(BAD_REQUEST, "์ง€์—ญ ์กฐํšŒ ์‹คํŒจ"),
AREA_FETCH_FAILED_NOT_SERVICE_AREA(BAD_REQUEST, "ํ˜„์žฌ ์ขŒํ‘œ ์กฐํšŒ๋Š” ๊ตญ๋‚ด์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค"),
Expand Down

0 comments on commit 70d1465

Please sign in to comment.