diff --git a/src/main/java/space/space_spring/response/status/BaseExceptionResponseStatus.java b/src/main/java/space/space_spring/response/status/BaseExceptionResponseStatus.java index a6af3f78..64174408 100644 --- a/src/main/java/space/space_spring/response/status/BaseExceptionResponseStatus.java +++ b/src/main/java/space/space_spring/response/status/BaseExceptionResponseStatus.java @@ -109,7 +109,9 @@ public enum BaseExceptionResponseStatus implements ResponseStatus { NOT_LIKED_THE_POST_YET(11003, HttpStatus.BAD_REQUEST, "유저가 해당 게시글에 좋아요를 누르지 않았습니다."), COMMENT_NOT_EXIST(11004, HttpStatus.NOT_FOUND, "존재하지 않는 댓글 id입니다."), ALREADY_LIKED_THE_COMMENT(11005, HttpStatus.BAD_REQUEST, "해당 댓글에 이미 좋아요를 눌렀습니다."), - NOT_LIKED_THE_COMMENT_YET(11006, HttpStatus.BAD_REQUEST, "유저가 해당 댓글에 좋아요를 누르지 않았습니다."); + NOT_LIKED_THE_COMMENT_YET(11006, HttpStatus.BAD_REQUEST, "유저가 해당 댓글에 좋아요를 누르지 않았습니다."), + INVALID_REPLY_TARGET(11007, HttpStatus.BAD_REQUEST, "대댓글 일 때는 targetId는 null 일 수 없습니다."), + INVALID_COMMENT_TARGET(11008, HttpStatus.BAD_REQUEST, "댓글일 때는 targetId가 null이어야 합니다."); private final int code; private final HttpStatus status; diff --git a/src/main/java/space/space_spring/service/CommentService.java b/src/main/java/space/space_spring/service/CommentService.java index 2f28fefa..7b9a5071 100644 --- a/src/main/java/space/space_spring/service/CommentService.java +++ b/src/main/java/space/space_spring/service/CommentService.java @@ -32,7 +32,6 @@ public class CommentService { private final UserSpaceDao userSpaceDao; private final UserUtils userUtils; private final UserSpaceUtils userSpaceUtils; - private final LikeService likeService; // TODO 1: 유저가 스페이스에 속하는지 검증 public void validateUserInSpace(Long userId, Long spaceId) { @@ -64,6 +63,14 @@ public List getCommentsByPost(Long postId, Long userId) { @Transactional public Long createComment(Long userId, Long postId, CreateCommentRequest.Request createCommentRequest) { + // TODO 1: isReply와 targetId 유효성 검사 + if(createCommentRequest.isReply() && createCommentRequest.getTargetId() == null) { + throw new CustomException(INVALID_REPLY_TARGET); + } + if(!createCommentRequest.isReply() && createCommentRequest.getTargetId() != null) { + throw new CustomException(INVALID_COMMENT_TARGET); + } + // TODO 1: userId에 해당하는 user find User user = userUtils.findUserByUserId(userId); @@ -71,6 +78,7 @@ public Long createComment(Long userId, Long postId, CreateCommentRequest.Request Post post = postDao.findById(postId) .orElseThrow(() -> new CustomException(POST_NOT_EXIST)); + // TODO 3: 댓글 또는 대댓글 생성 Comment comment = createCommentRequest.toEntity(user, post); commentDao.save(comment);