728x90
320x100
🎯 오늘의 목표
- 댓글(Comment) 도메인 API 구현
- 댓글(Comment) 도메인 API 테스트
⚙️ 진행한 작업
- 댓글(Comment) 도메인 API 구현
- 댓글(Comment) 도메인 API 테스트
🛠️ 개발내용
📌 CommentController 구현
package ddururi.bookbookclub.domain.comment.controller;
/**
* 댓글(Comment) 관련 API 컨트롤러
* - 댓글 작성, 조회, 삭제
*/
@RestController
@RequestMapping("/api/comments")
@RequiredArgsConstructor
public class CommentController {
private final CommentService commentService;
/**
* 댓글 작성
* @param userDetails 현재 로그인한 사용자 정보
* @param request 댓글 작성 요청 데이터
* @return 생성된 댓글 정보
*/
@PostMapping
public ResponseEntity<ApiResponse<CommentResponse>> createComment(
@AuthenticationPrincipal CustomUserDetails userDetails,
@RequestBody @Valid CommentRequest request
) {
Comment comment = commentService.createComment(request.getContent(), userDetails.getUser(), request.getFeedId());
return ResponseEntity.ok(ApiResponse.success(CommentResponse.from(comment)));
}
/**
* 댓글 목록 조회
* @param feedId 댓글을 조회할 피드 ID
* @return 댓글 리스트
*/
@GetMapping("/feed/{feedId}")
public ResponseEntity<ApiResponse<List<CommentResponse>>> getComments(@PathVariable Long feedId) {
List<Comment> comments = commentService.getCommentsByFeed(feedId);
List<CommentResponse> response = comments.stream()
.map(CommentResponse::from)
.collect(Collectors.toList());
return ResponseEntity.ok(ApiResponse.success(response));
}
/**
* 댓글 삭제
* @param commentId 삭제할 댓글 ID
* @param userDetails 현재 로그인한 사용자 정보
*/
@DeleteMapping("/{commentId}")
public ResponseEntity<ApiResponse<Void>> deleteComment(
@PathVariable Long commentId,
@AuthenticationPrincipal CustomUserDetails userDetails
) {
commentService.deleteComment(commentId, userDetails.getUser());
return ResponseEntity.ok(ApiResponse.success(null, "댓글이 삭제되었습니다."));
}
}
[포스트맨으로 테스트하기]
📌 댓글 등록
METHOD | POST |
URL | http://localhost:8080/api/comments |
Headers | Authorization: Bearer {JWT} |
Body (JSON) |
{
"content": "이 책 너무 좋아요!",
"feedId": 1
}
[테스트 결과 : 성공]
📌 댓글 목록 조회 (특정 피드에 달린)
METHOD | GET |
URL | http://localhost:8080/api/comments/feed/{feedId} |
예시 | http://localhost:8080/api/comments/feed/1 |
Headers | Authorization: Bearer {JWT} |
[테스트 결과 : 성공]
📌 댓글 삭제
METHOD | DELETE |
URL | http://localhost:8080/api/comments/{commentId} |
예시 | http://localhost:8080/api/comments/12 |
Headers | Authorization: Bearer {JWT} |
[테스트 결과 : 성공]
728x90
320x100
'💻 뚝딱뚝딱 > 북북클럽' 카테고리의 다른 글
[개발일지 #025] 좋아요(Like) 토글 기능으로 변경하기 (0) | 2025.05.01 |
---|---|
[개발일지 #024] JWT 토큰이 없는데도 200 OK 가 뜬다? (해결방법) (0) | 2025.04.30 |
[개발일지 #022] 댓글(Comment) 도메인 구현 및 단위테스트 (0) | 2025.04.30 |
[개발일지 #021] 좋아요(Like) 도메인 API 구현 및 테스트 (0) | 2025.04.30 |
[개발일지 #020] 좋아요(Like) 도메인 개발 및 단위테스트 (0) | 2025.04.29 |