Skip to content

Commit

Permalink
#4 Feat: delete Comment
Browse files Browse the repository at this point in the history
  • Loading branch information
5jisoo committed Jan 9, 2024
1 parent 4562de8 commit d0049a1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,15 @@ public BaseResponse<?> getChildCommentsWithPaging(@RequestHeader("Authorization"
return new BaseResponse<>(e.getStatus());
}
}

@DeleteMapping("/{commentId}")
public BaseResponse<?> deleteComment(@RequestHeader("Authorization") Long loginId,
@PathVariable String postId,
@PathVariable("commentId") Long commentId) {
try {
return new BaseResponse<>(commentService.deleteComment(commentId));
} catch (BaseException e) {
return new BaseResponse<>(e.getStatus());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,14 @@ Integer getCommentCountByPost(@Param("post") Post post,
@Query("update Comment c set c.status = 'DELETED' " +
"where c.member.id = :memberId ")
void bulkDeleteByMember(@Param("memberId") Long memberId);

@Modifying
@Query("update Comment c set c.status = 'DELETED' " +
"where c.post = :post ")
void bulkDeleteByPost(@Param("post") Post post);

@Modifying
@Query("update Comment c set c.status = 'DELETED' " +
"where c.parentComment = :comment ")
void bulkDeleteByParentComment(@Param("comment") Comment comment);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.springframework.stereotype.Service;

import static com.unibond.unibond.common.BaseEntityStatus.ACTIVE;
import static com.unibond.unibond.common.BaseEntityStatus.DELETED;
import static com.unibond.unibond.common.BaseResponseStatus.*;

@Service
Expand Down Expand Up @@ -74,6 +75,28 @@ public ChildCommentsPagingResDto getChildCommentsWithPaging(Long postId, Long co
}
}

@Transactional
public BaseResponseStatus deleteComment(Long commentId) throws BaseException{
try {
Member loginMember = loginInfoService.getLoginMember();
Comment comment = findCommentById(commentId);
if (!comment.getMember().getId().equals(loginMember.getId())) {
throw new BaseException(NOT_YOUR_COMMENT);
}
comment.setStatus(DELETED);
deleteChildComments(comment);
return SUCCESS;
} catch (BaseException e) {
throw e;
} catch (Exception e) {
throw new BaseException(DATABASE_ERROR);
}
}

private void deleteChildComments(Comment comment) {
commentRepository.bulkDeleteByParentComment(comment);
}

private Comment findCommentById(Long id) throws BaseException {
return commentRepository.findCommentByIdAndStatus(id, ACTIVE)
.orElseThrow(() -> new BaseException(INVALID_COMMENT_ID));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public enum BaseResponseStatus {
// alarm (3000 ~ 3099)

// comment (3100 ~ 3199)
NOT_YOUR_COMMENT(false, 3100, "댓글 삭제 권한이 없습니다."),

// disease (3200 ~ 3299)

Expand Down

0 comments on commit d0049a1

Please sign in to comment.