diff --git a/src/main/java/zoo/insightnote/domain/comment/controller/CommentController.java b/src/main/java/zoo/insightnote/domain/comment/controller/CommentController.java index bb37a337..26de22c5 100644 --- a/src/main/java/zoo/insightnote/domain/comment/controller/CommentController.java +++ b/src/main/java/zoo/insightnote/domain/comment/controller/CommentController.java @@ -9,27 +9,27 @@ import org.springframework.security.core.userdetails.UserDetails; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; -import zoo.insightnote.domain.comment.dto.req.CommentCreateReqDto; -import zoo.insightnote.domain.comment.dto.req.CommentUpdateReqDto; -import zoo.insightnote.domain.comment.dto.res.CommentIdResDto; -import zoo.insightnote.domain.comment.dto.res.CommentListResDto; +import zoo.insightnote.domain.comment.dto.response.CommentCreateRequest; +import zoo.insightnote.domain.comment.dto.response.CommentUpdateRequest; +import zoo.insightnote.domain.comment.dto.request.CommentIdResponse; +import zoo.insightnote.domain.comment.dto.request.CommentListResponse; @Tag(name = "Comment API", description = "댓글 관련 API") public interface CommentController { @Operation(summary = "댓글 작성", description = "사용자가 특정 인사이트에 댓글을 작성합니다.") - ResponseEntity writeComment( + ResponseEntity writeComment( @Parameter(description = "인사이트 ID") @PathVariable Long insightId, @Parameter(hidden = true) @AuthenticationPrincipal UserDetails userDetails, - @RequestBody CommentCreateReqDto request); + @RequestBody CommentCreateRequest request); @Operation(summary = "댓글 수정", description = "작성자가 본인의 댓글을 수정합니다.") - ResponseEntity updateComment( + ResponseEntity updateComment( @Parameter(description = "인사이트 ID") @PathVariable Long insightId, @Parameter(description = "댓글 ID") @PathVariable Long commentId, @Parameter(hidden = true) @AuthenticationPrincipal UserDetails userDetails, - @RequestBody CommentUpdateReqDto request); + @RequestBody CommentUpdateRequest request); @Operation(summary = "댓글 삭제", description = "작성자가 본인의 댓글을 삭제합니다.") ResponseEntity deleteComment( @@ -38,6 +38,6 @@ ResponseEntity deleteComment( @Parameter(hidden = true) @AuthenticationPrincipal UserDetails userDetails); @Operation(summary = "댓글 목록 조회", description = "특정 인사이트의 댓글 목록을 조회합니다.") - ResponseEntity> getListComments( + ResponseEntity> getListComments( @Parameter(description = "인사이트 ID") @PathVariable Long insightId); } \ No newline at end of file diff --git a/src/main/java/zoo/insightnote/domain/comment/controller/CommentControllerImpl.java b/src/main/java/zoo/insightnote/domain/comment/controller/CommentControllerImpl.java index 29a9f3fb..5f1bbbb1 100644 --- a/src/main/java/zoo/insightnote/domain/comment/controller/CommentControllerImpl.java +++ b/src/main/java/zoo/insightnote/domain/comment/controller/CommentControllerImpl.java @@ -1,11 +1,9 @@ package zoo.insightnote.domain.comment.controller; -import java.util.Collections; import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; -import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; @@ -15,11 +13,11 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import zoo.insightnote.domain.comment.dto.res.CommentListResDto; +import zoo.insightnote.domain.comment.dto.request.CommentListResponse; -import zoo.insightnote.domain.comment.dto.req.CommentCreateReqDto; -import zoo.insightnote.domain.comment.dto.req.CommentUpdateReqDto; -import zoo.insightnote.domain.comment.dto.res.CommentIdResDto; +import zoo.insightnote.domain.comment.dto.response.CommentCreateRequest; +import zoo.insightnote.domain.comment.dto.response.CommentUpdateRequest; +import zoo.insightnote.domain.comment.dto.request.CommentIdResponse; import zoo.insightnote.domain.comment.service.CommentService; @@ -32,28 +30,24 @@ public class CommentControllerImpl implements CommentController { @Override @PostMapping("/{insightId}/comments") - public ResponseEntity writeComment( + public ResponseEntity writeComment( @PathVariable Long insightId, @AuthenticationPrincipal UserDetails userDetails, - @RequestBody CommentCreateReqDto request) + @RequestBody CommentCreateRequest request) { - CommentIdResDto commentId = commentService.createComment(insightId, userDetails.getUsername(), request); + CommentIdResponse commentId = commentService.createComment(insightId, userDetails.getUsername(), request); return ResponseEntity.ok().body(commentId); } -// @Override -// @GetMapping("/{insightId}/comments") -// public ResponseEntity> listComments(@PathVariable Long insightId) { -// return ResponseEntity.ok().body(commentService.findCommentsByInsightId(insightId)); -// } - @Override @PutMapping("/{insightId}/comments/{commentId}") - public ResponseEntity updateComment(@PathVariable Long insightId, - @PathVariable Long commentId, - @AuthenticationPrincipal UserDetails userDetails, - @RequestBody CommentUpdateReqDto request) { - CommentIdResDto response = commentService.updateComment(insightId, userDetails.getUsername(), commentId, request); + public ResponseEntity updateComment( + @PathVariable Long insightId, + @PathVariable Long commentId, + @AuthenticationPrincipal UserDetails userDetails, + @RequestBody CommentUpdateRequest request + ) { + CommentIdResponse response = commentService.updateComment(insightId, userDetails.getUsername(), commentId, request); return ResponseEntity.ok().body(response); } @@ -68,20 +62,10 @@ public ResponseEntity deleteComment( return ResponseEntity.noContent().build(); } - // 임시 로직 - private UserDetails validateUser(UserDetails userDetails) { - if (userDetails != null) { - return userDetails; - } - return new User("001", "password", Collections.emptyList()); - } - - - // 댓글 리스트 출력 @Override @GetMapping("/comments/{insightId}") - public ResponseEntity> getListComments(@PathVariable Long insightId) { - List comments = commentService.getCommentsByInsight(insightId); + public ResponseEntity> getListComments(@PathVariable Long insightId) { + List comments = commentService.getCommentsByInsight(insightId); return ResponseEntity.ok(comments); } } diff --git a/src/main/java/zoo/insightnote/domain/comment/dto/request/CommentDefaultResponse.java b/src/main/java/zoo/insightnote/domain/comment/dto/request/CommentDefaultResponse.java new file mode 100644 index 00000000..570db15c --- /dev/null +++ b/src/main/java/zoo/insightnote/domain/comment/dto/request/CommentDefaultResponse.java @@ -0,0 +1,15 @@ +package zoo.insightnote.domain.comment.dto.request; +import java.time.LocalDateTime; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.AccessLevel; + +public record CommentDefaultResponse ( + Long commentId, + String content, + LocalDateTime createAt, + String author +){ +} \ No newline at end of file diff --git a/src/main/java/zoo/insightnote/domain/comment/dto/request/CommentIdResponse.java b/src/main/java/zoo/insightnote/domain/comment/dto/request/CommentIdResponse.java new file mode 100644 index 00000000..11c6ed2b --- /dev/null +++ b/src/main/java/zoo/insightnote/domain/comment/dto/request/CommentIdResponse.java @@ -0,0 +1,5 @@ +package zoo.insightnote.domain.comment.dto.request; + + +public record CommentIdResponse(Long id) { +} \ No newline at end of file diff --git a/src/main/java/zoo/insightnote/domain/comment/dto/res/CommentListResDto.java b/src/main/java/zoo/insightnote/domain/comment/dto/request/CommentListResponse.java similarity index 89% rename from src/main/java/zoo/insightnote/domain/comment/dto/res/CommentListResDto.java rename to src/main/java/zoo/insightnote/domain/comment/dto/request/CommentListResponse.java index 7bc921ff..3015c600 100644 --- a/src/main/java/zoo/insightnote/domain/comment/dto/res/CommentListResDto.java +++ b/src/main/java/zoo/insightnote/domain/comment/dto/request/CommentListResponse.java @@ -1,4 +1,4 @@ -package zoo.insightnote.domain.comment.dto.res; +package zoo.insightnote.domain.comment.dto.request; import lombok.AllArgsConstructor; import lombok.Getter; @@ -10,7 +10,7 @@ @Getter @AllArgsConstructor -public class CommentListResDto { +public class CommentListResponse { private Long id; private String name; private String createdAt; @@ -19,7 +19,7 @@ public class CommentListResDto { private String content; private boolean isSpeaker; - public CommentListResDto(Comment comment) { + public CommentListResponse(Comment comment) { this.id = comment.getId(); this.name = comment.getUser().getName(); // User 엔티티에 name 필드가 있다고 가정 this.createdAt = formatDate(comment.getCreateAt()); diff --git a/src/main/java/zoo/insightnote/domain/comment/dto/res/CommentDefaultResDto.java b/src/main/java/zoo/insightnote/domain/comment/dto/res/CommentDefaultResDto.java deleted file mode 100644 index dcba7c6c..00000000 --- a/src/main/java/zoo/insightnote/domain/comment/dto/res/CommentDefaultResDto.java +++ /dev/null @@ -1,17 +0,0 @@ -package zoo.insightnote.domain.comment.dto.res; -import java.time.LocalDateTime; - -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.AccessLevel; - -@Getter -@AllArgsConstructor -@NoArgsConstructor(access = AccessLevel.PROTECTED) -public class CommentDefaultResDto { - private Long commentId; - private String content; - private LocalDateTime createAt; - private String author; -} \ No newline at end of file diff --git a/src/main/java/zoo/insightnote/domain/comment/dto/res/CommentIdResDto.java b/src/main/java/zoo/insightnote/domain/comment/dto/res/CommentIdResDto.java deleted file mode 100644 index 685ae1b7..00000000 --- a/src/main/java/zoo/insightnote/domain/comment/dto/res/CommentIdResDto.java +++ /dev/null @@ -1,10 +0,0 @@ -package zoo.insightnote.domain.comment.dto.res; - -import lombok.AllArgsConstructor; -import lombok.Getter; - -@Getter -@AllArgsConstructor -public class CommentIdResDto { - private Long id; -} \ No newline at end of file diff --git a/src/main/java/zoo/insightnote/domain/comment/dto/req/CommentCreateReqDto.java b/src/main/java/zoo/insightnote/domain/comment/dto/response/CommentCreateRequest.java similarity index 77% rename from src/main/java/zoo/insightnote/domain/comment/dto/req/CommentCreateReqDto.java rename to src/main/java/zoo/insightnote/domain/comment/dto/response/CommentCreateRequest.java index 821406e3..64a369cf 100644 --- a/src/main/java/zoo/insightnote/domain/comment/dto/req/CommentCreateReqDto.java +++ b/src/main/java/zoo/insightnote/domain/comment/dto/response/CommentCreateRequest.java @@ -1,9 +1,9 @@ -package zoo.insightnote.domain.comment.dto.req; +package zoo.insightnote.domain.comment.dto.response; import io.swagger.v3.oas.annotations.media.Schema; import jakarta.validation.constraints.NotBlank; -public record CommentCreateReqDto( +public record CommentCreateRequest( @Schema(description = "댓글 내용", example = "작성하신 노트 잘보았습니다!") @NotBlank(message = "댓글 내용은 비어 있을 수 없습니다.") String content diff --git a/src/main/java/zoo/insightnote/domain/comment/dto/req/CommentUpdateReqDto.java b/src/main/java/zoo/insightnote/domain/comment/dto/response/CommentUpdateRequest.java similarity index 79% rename from src/main/java/zoo/insightnote/domain/comment/dto/req/CommentUpdateReqDto.java rename to src/main/java/zoo/insightnote/domain/comment/dto/response/CommentUpdateRequest.java index 9bec4edb..48c930fd 100644 --- a/src/main/java/zoo/insightnote/domain/comment/dto/req/CommentUpdateReqDto.java +++ b/src/main/java/zoo/insightnote/domain/comment/dto/response/CommentUpdateRequest.java @@ -1,9 +1,9 @@ -package zoo.insightnote.domain.comment.dto.req; +package zoo.insightnote.domain.comment.dto.response; import io.swagger.v3.oas.annotations.media.Schema; import jakarta.validation.constraints.NotBlank; -public record CommentUpdateReqDto( +public record CommentUpdateRequest( @Schema(description = "댓글 내용", example = "작성하신 노트에 잘못된 부분이 있는 것 같습니다!") @NotBlank(message = "댓글 내용은 비어 있을 수 없습니다.") String content diff --git a/src/main/java/zoo/insightnote/domain/comment/entity/Comment.java b/src/main/java/zoo/insightnote/domain/comment/entity/Comment.java index e0d16350..ac88920d 100644 --- a/src/main/java/zoo/insightnote/domain/comment/entity/Comment.java +++ b/src/main/java/zoo/insightnote/domain/comment/entity/Comment.java @@ -27,14 +27,12 @@ public class Comment extends BaseTimeEntity { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; - //TODO : user Entity 개발 완료시 nullable = false로 수정 @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "user_id", nullable = true) + @JoinColumn(name = "user_id", nullable = false) private User user; - //TODO : insight Entity 개발 완료시 nullable = false로 수정 @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "insight_id", nullable = true) + @JoinColumn(name = "insight_id", nullable = false) private Insight insight; private String content; diff --git a/src/main/java/zoo/insightnote/domain/comment/mapper/CommentMapper.java b/src/main/java/zoo/insightnote/domain/comment/mapper/CommentMapper.java index 1398b76d..d6157036 100644 --- a/src/main/java/zoo/insightnote/domain/comment/mapper/CommentMapper.java +++ b/src/main/java/zoo/insightnote/domain/comment/mapper/CommentMapper.java @@ -1,7 +1,7 @@ package zoo.insightnote.domain.comment.mapper; -import zoo.insightnote.domain.comment.dto.req.CommentCreateReqDto; -import zoo.insightnote.domain.comment.dto.res.CommentDefaultResDto; +import zoo.insightnote.domain.comment.dto.response.CommentCreateRequest; +import zoo.insightnote.domain.comment.dto.request.CommentDefaultResponse; import zoo.insightnote.domain.comment.entity.Comment; import zoo.insightnote.domain.insight.entity.Insight; @@ -9,7 +9,7 @@ public class CommentMapper { - public static Comment toEntity(Insight insight, User user, CommentCreateReqDto request) { + public static Comment toEntity(Insight insight, User user, CommentCreateRequest request) { return Comment.builder() .insight(insight) .user(user) @@ -17,8 +17,8 @@ public static Comment toEntity(Insight insight, User user, CommentCreateReqDto r .build(); } - public static CommentDefaultResDto toResponse(Comment comment) { - return new CommentDefaultResDto ( + public static CommentDefaultResponse toResponse(Comment comment) { + return new CommentDefaultResponse( comment.getId(), comment.getContent(), comment.getCreateAt(), diff --git a/src/main/java/zoo/insightnote/domain/comment/service/CommentService.java b/src/main/java/zoo/insightnote/domain/comment/service/CommentService.java index 46a71425..91042794 100644 --- a/src/main/java/zoo/insightnote/domain/comment/service/CommentService.java +++ b/src/main/java/zoo/insightnote/domain/comment/service/CommentService.java @@ -6,12 +6,11 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import zoo.insightnote.domain.comment.dto.res.CommentIdResDto; -import zoo.insightnote.domain.comment.dto.res.CommentListResDto; +import zoo.insightnote.domain.comment.dto.request.CommentIdResponse; +import zoo.insightnote.domain.comment.dto.request.CommentListResponse; -import zoo.insightnote.domain.comment.dto.req.CommentCreateReqDto; -import zoo.insightnote.domain.comment.dto.req.CommentUpdateReqDto; -import zoo.insightnote.domain.comment.dto.res.CommentDefaultResDto; +import zoo.insightnote.domain.comment.dto.response.CommentCreateRequest; +import zoo.insightnote.domain.comment.dto.response.CommentUpdateRequest; import zoo.insightnote.domain.comment.entity.Comment; import zoo.insightnote.domain.comment.mapper.CommentMapper; @@ -19,7 +18,6 @@ import zoo.insightnote.domain.insight.entity.Insight; import zoo.insightnote.domain.insight.repository.InsightRepository; import zoo.insightnote.domain.user.entity.User; -import zoo.insightnote.domain.user.repository.UserRepository; import zoo.insightnote.domain.user.service.UserService; import zoo.insightnote.global.exception.CustomException; import zoo.insightnote.global.exception.ErrorCode; @@ -32,10 +30,10 @@ public class CommentService { private final UserService userService; private final InsightRepository insightRepository; - public CommentIdResDto createComment(Long insightId, String userName, CommentCreateReqDto request) { + public CommentIdResponse createComment(Long insightId, String userName, CommentCreateRequest request) { Insight insight = insightRepository.findById(insightId) - .orElseThrow(() -> new CustomException(null, "인사이트 노트를 찾을 수 없음")); + .orElseThrow(() -> new CustomException(ErrorCode.INSIGHT_NOT_FOUND)); User user = userService.findByUsername(userName); @@ -43,23 +41,11 @@ public CommentIdResDto createComment(Long insightId, String userName, CommentCre commentRepository.save(comment); - return new CommentIdResDto(comment.getId()); + return new CommentIdResponse(comment.getId()); } -// public List findCommentsByInsightId(Long insightId) { -// -// List comments = commentRepository.findAllByInsightId(insightId); -// -// List responses = new ArrayList<>(); -// for (Comment comment : comments) { -// responses.add(CommentMapper.toResponse(comment)); -// } -// -// return responses; -// } - @Transactional - public CommentIdResDto updateComment(Long insightId, String userName, Long commentId, CommentUpdateReqDto request) { + public CommentIdResponse updateComment(Long insightId, String userName, Long commentId, CommentUpdateRequest request) { Comment comment = findCommentById(commentId); User user = userService.findByUsername(userName); @@ -68,7 +54,7 @@ public CommentIdResDto updateComment(Long insightId, String userName, Long comme comment.update(request.content()); - return new CommentIdResDto(comment.getId()); + return new CommentIdResponse(comment.getId()); } public void deleteComment(Long insightId, String userName, Long commentId) { @@ -82,14 +68,11 @@ public void deleteComment(Long insightId, String userName, Long commentId) { } - public List getCommentsByInsight(Long insightId) { - - // 예외처리 로직 필요함 - + public List getCommentsByInsight(Long insightId) { List comments = commentRepository.findByInsightIdWithUser(insightId); return comments.stream() - .map(CommentListResDto::new) + .map(CommentListResponse::new) .collect(Collectors.toList()); }