From 357aa113b6673fc0e610eba143b11f1daf1ec324 Mon Sep 17 00:00:00 2001 From: moonyaeyoon Date: Thu, 12 Sep 2024 10:21:35 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat=20:=20=EA=B8=80=20=EB=82=B4=EC=9A=A9?= =?UTF-8?q?=EC=97=90=20userId=20=EB=B0=98=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/meltingpot/server/post/dto/PostDetailResponse.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/meltingpot/server/post/dto/PostDetailResponse.java b/src/main/java/meltingpot/server/post/dto/PostDetailResponse.java index f904d48..1dbbe2d 100644 --- a/src/main/java/meltingpot/server/post/dto/PostDetailResponse.java +++ b/src/main/java/meltingpot/server/post/dto/PostDetailResponse.java @@ -13,6 +13,7 @@ @Builder public class PostDetailResponse { private Long postId; + private Long userId; private String name; private String title; private String content; @@ -41,6 +42,7 @@ 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()) From 2a746b39fc9971c4e07b2fefa3a90ea1521493e1 Mon Sep 17 00:00:00 2001 From: moonyaeyoon Date: Thu, 12 Sep 2024 10:33:38 +0900 Subject: [PATCH 2/3] =?UTF-8?q?fix=20:=20=EC=BB=A4=EB=AE=A4=EB=8B=88?= =?UTF-8?q?=ED=8B=B0=20=EA=B8=80=20=EB=AA=A9=EB=A1=9D=EC=97=90=20=ED=94=84?= =?UTF-8?q?=EB=A1=9C=ED=95=84=20=EC=82=AC=EC=A7=84=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../meltingpot/server/post/dto/PostsListResponse.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/meltingpot/server/post/dto/PostsListResponse.java b/src/main/java/meltingpot/server/post/dto/PostsListResponse.java index edcaccc..ef7aa88 100644 --- a/src/main/java/meltingpot/server/post/dto/PostsListResponse.java +++ b/src/main/java/meltingpot/server/post/dto/PostsListResponse.java @@ -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; @@ -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; @@ -36,9 +38,15 @@ public static PostsList from(Post post) { List 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()) From 28b12713d96b735158b9b04558169820261e8236 Mon Sep 17 00:00:00 2001 From: moonyaeyoon Date: Thu, 12 Sep 2024 12:02:54 +0900 Subject: [PATCH 3/3] =?UTF-8?q?fix=20:=20=EC=BB=A4=EB=AE=A4=EB=8B=88?= =?UTF-8?q?=ED=8B=B0=20=ED=8F=AC=EC=8A=A4=ED=8A=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/repository/PostRepository.java | 2 +- .../post/controller/PostController.java | 4 +- .../server/post/dto/PostDetailResponse.java | 5 +- .../server/post/service/PostService.java | 79 ++----------------- .../meltingpot/server/util/ResponseCode.java | 1 + 5 files changed, 12 insertions(+), 79 deletions(-) diff --git a/src/main/java/meltingpot/server/domain/repository/PostRepository.java b/src/main/java/meltingpot/server/domain/repository/PostRepository.java index 65f36e0..e4d4c7a 100644 --- a/src/main/java/meltingpot/server/domain/repository/PostRepository.java +++ b/src/main/java/meltingpot/server/domain/repository/PostRepository.java @@ -16,7 +16,7 @@ public interface PostRepository extends JpaRepository { - @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 findByPostTypeAndCursor(@Param("postType") PostType postType, @Param("cursor") Long cursor, Pageable pageable); Slice findAllByAccountAndDeletedAtIsNullOrderByIdDesc(Account account, Pageable page); diff --git a/src/main/java/meltingpot/server/post/controller/PostController.java b/src/main/java/meltingpot/server/post/controller/PostController.java index a48f3b1..cbf458d 100644 --- a/src/main/java/meltingpot/server/post/controller/PostController.java +++ b/src/main/java/meltingpot/server/post/controller/PostController.java @@ -64,9 +64,9 @@ public ResponseEntity> getPostList(@CurrentUser @GetMapping("/{postId}") @Operation(summary = "커뮤니티 글 내용 조회", description = "postId로 커뮤니티 글 내용을 조회합니다.") - public ResponseEntity> getPostDetail(@CurrentUser Account account, @PathVariable Long postId, @RequestParam(required = false, name = "cursor") Long cursor, @RequestParam(name = "pageSize") Integer pageSize) { + public ResponseEntity> 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); } diff --git a/src/main/java/meltingpot/server/post/dto/PostDetailResponse.java b/src/main/java/meltingpot/server/post/dto/PostDetailResponse.java index 1dbbe2d..cde2713 100644 --- a/src/main/java/meltingpot/server/post/dto/PostDetailResponse.java +++ b/src/main/java/meltingpot/server/post/dto/PostDetailResponse.java @@ -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; @@ -19,7 +18,6 @@ public class PostDetailResponse { private String content; private List imgData; // Image data including ID and URL private Integer commentCount; - private CommentsListResponse commentsList; private LocalDateTime updatedAt; // Static nested class to hold image data @@ -31,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 imgData = post.getPostImages().stream() .map(postImage -> new ImageData(postImage.getId(), postImage.getImageUrl())) .collect(Collectors.toList()); @@ -48,7 +46,6 @@ public static PostDetailResponse of(Post post, CommentsListResponse commentsList .content(post.getContent()) .imgData(imgData) .commentCount(commentCount) - .commentsList(commentsList) .updatedAt(post.getUpdatedAt()) .build(); } diff --git a/src/main/java/meltingpot/server/post/service/PostService.java b/src/main/java/meltingpot/server/post/service/PostService.java index 0b68194..d572b24 100644 --- a/src/main/java/meltingpot/server/post/service/PostService.java +++ b/src/main/java/meltingpot/server/post/service/PostService.java @@ -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; @@ -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; @@ -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 목록 불러오기*/ @@ -171,65 +163,6 @@ private List createPostImages(List imageKeys, Post post, Acco .collect(Collectors.toList()); } - private CommentsListResponse fetchCommentsList(Long postId, Long cursor, int pageSize) { - List 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 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 parentComments = commentRepository.findParentCommentsByPostId(postId, parentCursor, pageable); - for (Comment parent : parentComments) { - if (count >= pageSize) break; - commentDetailDTOs.add(CommentsListResponse.CommentDetail.from(parent)); - count++; - - List 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 getDraftPost(Account account) { return postRepository.findByAccountAndIsDraftTrue(account); } @@ -238,5 +171,7 @@ private void setPostImages(Post post, Account account,PostCreateRequest postRequ List postImages = createPostImages(postRequest.getImageKeys(), post, account); post.setPostImages(postImages); } + + } diff --git a/src/main/java/meltingpot/server/util/ResponseCode.java b/src/main/java/meltingpot/server/util/ResponseCode.java index 13b750f..b502ae4 100644 --- a/src/main/java/meltingpot/server/util/ResponseCode.java +++ b/src/main/java/meltingpot/server/util/ResponseCode.java @@ -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, "현재 좌표 조회는 국내에서만 사용 가능합니다"),