Skip to content

Commit 5ccb2fe

Browse files
authored
Merge pull request #77 from Hsu-Connect/feat/#76
[#76] 댓글 삭제 API
2 parents 2b90859 + 0ee7546 commit 5ccb2fe

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

src/main/java/hansung/hansung_connect/common/exception/code/status/ErrorStatus.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ public enum ErrorStatus implements BaseErrorCode {
2828
POST_QUERY_TYPE_EXCEPTION(HttpStatus.BAD_REQUEST, "POST4002", "게시글 조회 타입이 잘못되었습니다."),
2929
POST_LIST_NOT_FOUND(HttpStatus.INTERNAL_SERVER_ERROR, "POST4003", "게시글 리스트가 비어있습니다."),
3030
INVALID_PAGE_FOR_POPULAR(HttpStatus.BAD_REQUEST, "POST4004", "인기 게시글은 0번 페이지만 조회 가능합니다."),
31-
POST_FORBIDDEN(HttpStatus.INTERNAL_SERVER_ERROR, "POST4005", "해당 게시글의 수정이 허용된 사용자가 아닙니다."),
31+
POST_FORBIDDEN(HttpStatus.BAD_REQUEST, "POST4005", "해당 게시글의 권한이 없습니다."),
32+
33+
//댓글 관련 응답
34+
COMMENT_NOT_FOUND(HttpStatus.BAD_REQUEST,"COMMENT4001","댓글을 찾을 수 없습니다."),
35+
COMMENT_FORBIDDEN(HttpStatus.BAD_REQUEST,"COMMENT4002","해당 댓글의 권한이 없습니다."),
3236

3337
// 커리어 관련 응답
3438
CAREER_COMPANY_REQUIRED(HttpStatus.BAD_REQUEST, "CAREER4001", "회사명은 필수입니다."),

src/main/java/hansung/hansung_connect/domain/commnet/controller/CommentController.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.swagger.v3.oas.annotations.Parameter;
1010
import io.swagger.v3.oas.annotations.tags.Tag;
1111
import lombok.RequiredArgsConstructor;
12+
import org.springframework.web.bind.annotation.DeleteMapping;
1213
import org.springframework.web.bind.annotation.GetMapping;
1314
import org.springframework.web.bind.annotation.PathVariable;
1415
import org.springframework.web.bind.annotation.PostMapping;
@@ -55,6 +56,23 @@ public ApiResponse<CommentResponseDto.CommentListResponse> getMyComments(
5556
return ApiResponse.onSuccess(commentQueryService.getCommentsByUser(userId, page));
5657
}
5758

59+
@Operation(
60+
summary = "댓글 삭제",
61+
description = """
62+
댓글을 삭제하는 API입니다.
63+
Path Variable로 댓글 아이디를 입력해주세요.
64+
- 게시글의 작성자인 경우 모든 댓글 삭제 가능
65+
- 게시글의 작성자가 아닌 경우 자신의 댓글만 삭제 가능
66+
"""
67+
)
68+
@DeleteMapping
69+
public ApiResponse<Void> deleteComment(
70+
@PathVariable("commentId") Long commentId
71+
) {
5872

73+
Long userId = 1L;
74+
commentCommandService.deleteComment(userId, commentId);
75+
return ApiResponse.onSuccess(null);
76+
}
5977

6078
}

src/main/java/hansung/hansung_connect/domain/commnet/service/CommentCommandService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
public interface CommentCommandService {
77

88
CommentResponseDto.CommentCreateResponse createComment(Long userId, Long postId, CommentRequestDto.CommentCreateRequest request);
9+
void deleteComment(Long userId, Long commentId);
910
}

src/main/java/hansung/hansung_connect/domain/commnet/service/CommentCommandServiceImpl.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,20 @@ public CommentCreateResponse createComment(Long userId, Long postId, CommentCrea
3939

4040
return commentConverter.toCommentCreateResponse(comment);
4141
}
42+
43+
@Override
44+
public void deleteComment(Long userId, Long commentId) {
45+
46+
Comment comment = commentRepository.findById(commentId)
47+
.orElseThrow(() -> new GeneralException(ErrorStatus.COMMENT_NOT_FOUND));
48+
49+
User commentAuthor = comment.getUser();
50+
User postAuthor = comment.getPost().getUser();
51+
52+
if(!commentAuthor.getId().equals(userId) && !postAuthor.getId().equals(userId)) {
53+
throw new GeneralException(ErrorStatus.COMMENT_FORBIDDEN);
54+
}
55+
56+
commentRepository.delete(comment);
57+
}
4258
}

0 commit comments

Comments
 (0)