Skip to content

Commit 80cd4ee

Browse files
committed
[Feat]: 댓글 조회 기능 구현
1 parent c19f687 commit 80cd4ee

File tree

4 files changed

+93
-3
lines changed

4 files changed

+93
-3
lines changed

back/src/main/java/com/back/domain/comment/controller/CommentController.java

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22

33
import com.back.domain.comment.dto.CommentRequest;
44
import com.back.domain.comment.dto.CommentResponse;
5+
import com.back.domain.comment.enums.CommentSortType;
56
import com.back.domain.comment.service.CommentService;
6-
import com.back.domain.post.dto.PostDetailResponse;
7-
import com.back.domain.post.dto.PostRequest;
8-
import com.back.domain.post.service.PostService;
7+
import com.back.domain.post.dto.PostSummaryResponse;
98
import com.back.global.common.ApiResponse;
9+
import com.back.global.common.PageResponse;
1010
import io.swagger.v3.oas.annotations.Operation;
1111
import io.swagger.v3.oas.annotations.Parameter;
1212
import io.swagger.v3.oas.annotations.tags.Tag;
1313
import jakarta.validation.Valid;
1414
import lombok.RequiredArgsConstructor;
15+
import org.springframework.data.domain.Page;
16+
import org.springframework.data.domain.PageRequest;
17+
import org.springframework.data.domain.Pageable;
18+
import org.springframework.data.domain.Sort;
1519
import org.springframework.http.HttpStatus;
1620
import org.springframework.web.bind.annotation.*;
1721

@@ -41,4 +45,56 @@ public ApiResponse<CommentResponse> createPost(
4145
return ApiResponse.success(response, "성공적으로 생성되었습니다.", HttpStatus.OK);
4246
}
4347

48+
// 게시글 목록 조회
49+
@GetMapping
50+
@Operation(summary = "댓글 목록 조회", description = "게시글 목록을 조회합니다.")
51+
public ApiResponse<PageResponse<CommentResponse>> getPosts(
52+
@Parameter(description = "페이지 정보") Pageable pageable,
53+
@Parameter(description = "조회할 게시글 ID", required = true) @PathVariable("postId") Long postId,
54+
@Parameter(description = "정렬 조건 LATEST or LIKES") @RequestParam(defaultValue = "LATEST") CommentSortType sortType,
55+
@RequestParam Long userId) {
56+
57+
Sort sort = Sort.by(Sort.Direction.DESC, sortType.getProperty());
58+
59+
Pageable sortedPageable = PageRequest.of(
60+
pageable.getPageNumber(),
61+
pageable.getPageSize(),
62+
sort
63+
);
64+
65+
Page<CommentResponse> responses = commentService.getComments(userId, postId, sortedPageable);
66+
return ApiResponse.success(PageResponse.of(responses), "성공적으로 조회되었습니다.", HttpStatus.OK);
67+
}
68+
69+
// // 게시글 단건 조회
70+
// @GetMapping("/{postId}")
71+
// @Operation(summary = "게시글 상세 조회", description = "게시글 ID로 게시글을 조회합니다.")
72+
// public ApiResponse<PostDetailResponse> getPost(
73+
// @Parameter(description = "조회할 게시글 ID", required = true) @PathVariable Long postId,
74+
// @RequestParam Long userId) {
75+
// return ApiResponse.success(postService.getPost(userId, postId), "성공적으로 조회되었습니다.", HttpStatus.OK);
76+
// }
77+
//
78+
// @PutMapping("/{postId}")
79+
// @Operation(summary = "게시글 수정", description = "게시글 ID로 게시글을 수정합니다.")
80+
// public ApiResponse<Long> updatePost(
81+
// @Parameter(description = "수정할 게시글 ID", required = true) @PathVariable Long postId,
82+
// @io.swagger.v3.oas.annotations.parameters.RequestBody(
83+
// description = "수정할 게시글 정보",
84+
// required = true
85+
// )
86+
// @RequestBody @Valid PostRequest request,
87+
// @RequestParam Long userId) {
88+
// return ApiResponse.success(postService.updatePost(userId, postId, request), "성공적으로 수정되었습니다.", HttpStatus.OK);
89+
// }
90+
//
91+
// @DeleteMapping("/{postId}")
92+
// @Operation(summary = "게시글 삭제", description = "게시글 ID로 게시글을 삭제합니다.")
93+
// public ApiResponse<Void> deletePost(
94+
// @Parameter(description = "삭제할 게시글 ID", required = true) @PathVariable Long postId,
95+
// @RequestParam Long userId) {
96+
// postService.deletePost(userId, postId);
97+
// return ApiResponse.success(null, "성공적으로 삭제되었습니다.", HttpStatus.OK);
98+
// }
99+
44100
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.back.domain.comment.enums;
2+
3+
import lombok.Getter;
4+
5+
/**
6+
* 최신순, 좋아요순
7+
*/
8+
@Getter
9+
public enum CommentSortType {
10+
LATEST("createdDate"),
11+
LIKES("likeCount");
12+
13+
private final String property;
14+
15+
CommentSortType(String property) {
16+
this.property = property;
17+
}
18+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package com.back.domain.comment.repository;
22

33
import com.back.domain.comment.entity.Comment;
4+
import org.springframework.data.domain.Page;
5+
import org.springframework.data.domain.Pageable;
46
import org.springframework.data.jpa.repository.JpaRepository;
7+
import org.springframework.data.jpa.repository.Query;
8+
import org.springframework.data.repository.query.Param;
59
import org.springframework.stereotype.Repository;
610

711
/**
812
* 댓글 엔티티에 대한 데이터베이스 접근을 담당하는 JpaRepository.
913
*/
1014
@Repository
1115
public interface CommentRepository extends JpaRepository<Comment, Long> {
16+
@Query("SELECT c FROM Comment c WHERE c.post.id = :postId")
17+
Page<Comment> findCommentsByPostId(@Param("postId") Long postId, Pageable pageable);
1218
}

back/src/main/java/com/back/domain/comment/service/CommentService.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.back.domain.comment.dto.CommentRequest;
44
import com.back.domain.comment.dto.CommentResponse;
55
import com.back.domain.comment.entity.Comment;
6+
import com.back.domain.comment.enums.CommentSortType;
67
import com.back.domain.comment.mapper.CommentMappers;
78
import com.back.domain.comment.repository.CommentRepository;
89
import com.back.domain.post.entity.Post;
@@ -13,6 +14,10 @@
1314
import com.back.global.exception.ErrorCode;
1415
import jakarta.validation.Valid;
1516
import lombok.RequiredArgsConstructor;
17+
import org.springframework.data.domain.Page;
18+
import org.springframework.data.domain.PageRequest;
19+
import org.springframework.data.domain.Pageable;
20+
import org.springframework.data.domain.Sort;
1621
import org.springframework.stereotype.Service;
1722

1823
/**
@@ -36,4 +41,9 @@ public CommentResponse createComment(Long userId, Long postId, CommentRequest re
3641
Comment savedComment = commentRepository.save(ctxMapper.toEntity(request));
3742
return ctxMapper.toResponse(savedComment);
3843
}
44+
45+
public Page<CommentResponse> getComments(Long userId, Long postId, Pageable pageable) {
46+
Page<Comment> commentsPage = commentRepository.findCommentsByPostId(postId, pageable);
47+
return commentsPage.map(CommentMappers.COMMENT_READ::map);
48+
}
3949
}

0 commit comments

Comments
 (0)