Skip to content

Commit 397b339

Browse files
committed
Feat : 적절한 예외를 반환하도록 변경
1 parent 7886c87 commit 397b339

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

back/src/main/java/com/back/domain/news/comment/controller/CommentController.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
import com.back.domain.news.news.service.NewsService;
1111
import com.back.global.rq.Rq;
1212
import com.back.global.rsData.RsData;
13+
import io.swagger.v3.oas.annotations.Operation;
1314
import lombok.RequiredArgsConstructor;
15+
import org.springframework.security.access.AccessDeniedException;
1416
import org.springframework.web.bind.annotation.*;
1517

1618
import java.util.List;
@@ -25,6 +27,7 @@ public class CommentController {
2527
private final Rq rq;
2628

2729
@GetMapping
30+
@Operation(summary = "댓글 목록 조회", description = "특정 뉴스의 댓글 목록을 불러옵니다.")
2831
public RsData<List<CommentResponse>> getComments(@PathVariable Long newsId) {
2932
News news = newsService.getNewsById(newsId);
3033
List<Comment> comments = commentService.getComments(news);
@@ -35,6 +38,7 @@ public RsData<List<CommentResponse>> getComments(@PathVariable Long newsId) {
3538
}
3639

3740
@PostMapping
41+
@Operation(summary = "댓글 생성", description = "특정 뉴스에 댓글을 생성합니다. 로그인한 사용자만 접근할 수 있습니다.")
3842
public RsData<CommentResponse> createComment(@PathVariable Long newsId, @RequestBody CommentCreateRequest request) {
3943
Member member = rq.getActor();
4044
if (member == null) {
@@ -47,25 +51,39 @@ public RsData<CommentResponse> createComment(@PathVariable Long newsId, @Request
4751
}
4852

4953
@PutMapping("/{commentId}")
54+
@Operation(summary = "댓글 수정", description = "특정 뉴스의 댓글을 수정합니다. 댓글 작성자만 접근할 수 있습니다.")
5055
public RsData<CommentResponse> updateComment(@PathVariable Long newsId, @PathVariable Long commentId, @RequestBody CommentUpdateRequest request) {
5156
Member member = rq.getActor();
5257
if (member == null) {
5358
return new RsData<>("401", "로그인이 필요합니다.");
5459
}
55-
News news = newsService.getNewsById(newsId);
56-
Comment updatedComment = commentService.updateComment(member, news, commentId, request.content());
57-
CommentResponse commentResponse = new CommentResponse(updatedComment);
58-
return new RsData<>("200", "댓글 수정 완료", commentResponse);
60+
try {
61+
News news = newsService.getNewsById(newsId);
62+
Comment updatedComment = commentService.updateComment(member, news, commentId, request.content());
63+
CommentResponse commentResponse = new CommentResponse(updatedComment);
64+
return new RsData<>("200", "댓글 수정 완료", commentResponse);
65+
} catch (AccessDeniedException e) {
66+
return new RsData<>("403", e.getMessage());
67+
} catch (IllegalArgumentException e) {
68+
return new RsData<>("400", e.getMessage());
69+
}
5970
}
6071

6172
@DeleteMapping("/{commentId}")
73+
@Operation(summary = "댓글 삭제", description = "특정 뉴스의 댓글을 삭제합니다. 댓글 작성자만 접근할 수 있습니다.")
6274
public RsData<Void> deleteComment(@PathVariable Long newsId, @PathVariable Long commentId) {
6375
Member member = rq.getActor();
6476
if (member == null) {
6577
return new RsData<>("401", "로그인이 필요합니다.");
6678
}
67-
News news = newsService.getNewsById(newsId);
68-
commentService.deleteComment(member, news, commentId);
69-
return new RsData<>("200", "댓글 삭제 완료");
79+
try {
80+
News news = newsService.getNewsById(newsId);
81+
commentService.deleteComment(member, news, commentId);
82+
return new RsData<>("200", "댓글 삭제 완료");
83+
} catch (AccessDeniedException e) {
84+
return new RsData<>("403", e.getMessage());
85+
} catch (IllegalArgumentException e) {
86+
return new RsData<>("400", e.getMessage());
87+
}
7088
}
7189
}

0 commit comments

Comments
 (0)