Skip to content

Commit

Permalink
feat: 대댓글 유효성 검사
Browse files Browse the repository at this point in the history
  • Loading branch information
arkchive committed Aug 17, 2024
1 parent df4c27a commit 22a44a6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/space/space_spring/service/CommentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -64,13 +63,22 @@ public List<ReadCommentsResponse> 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);

// TODO 2: postId에 해당하는 post find
Post post = postDao.findById(postId)
.orElseThrow(() -> new CustomException(POST_NOT_EXIST));

// TODO 3: 댓글 또는 대댓글 생성
Comment comment = createCommentRequest.toEntity(user, post);
commentDao.save(comment);

Expand Down

0 comments on commit 22a44a6

Please sign in to comment.