1+ package org .devkor .apu .saerok_server .domain .collection .api ;
2+
3+ import io .swagger .v3 .oas .annotations .Operation ;
4+ import io .swagger .v3 .oas .annotations .media .Content ;
5+ import io .swagger .v3 .oas .annotations .media .Schema ;
6+ import io .swagger .v3 .oas .annotations .responses .ApiResponse ;
7+ import io .swagger .v3 .oas .annotations .security .SecurityRequirement ;
8+ import io .swagger .v3 .oas .annotations .tags .Tag ;
9+ import jakarta .annotation .security .PermitAll ;
10+ import lombok .RequiredArgsConstructor ;
11+ import org .devkor .apu .saerok_server .domain .collection .api .dto .request .CreateCollectionCommentRequest ;
12+ import org .devkor .apu .saerok_server .domain .collection .api .dto .request .UpdateCollectionCommentRequest ;
13+ import org .devkor .apu .saerok_server .domain .collection .api .dto .response .*;
14+ import org .devkor .apu .saerok_server .domain .collection .application .CollectionCommentCommandService ;
15+ import org .devkor .apu .saerok_server .domain .collection .application .CollectionCommentQueryService ;
16+ import org .devkor .apu .saerok_server .global .security .principal .UserPrincipal ;
17+ import org .springframework .http .HttpStatus ;
18+ import org .springframework .security .access .prepost .PreAuthorize ;
19+ import org .springframework .security .core .annotation .AuthenticationPrincipal ;
20+ import org .springframework .web .bind .annotation .*;
21+
22+ @ Tag (name = "Collection Comment API" , description = "컬렉션 댓글 관련 API" )
23+ @ RestController
24+ @ RequiredArgsConstructor
25+ @ RequestMapping ("${api_prefix}/collections" )
26+ public class CollectionCommentController {
27+
28+ private final CollectionCommentCommandService commentCommandService ;
29+ private final CollectionCommentQueryService commentQueryService ;
30+
31+ /* 댓글 작성 */
32+ @ PostMapping ("/{collectionId}/comments" )
33+ @ PreAuthorize ("hasRole('USER')" )
34+ @ Operation (
35+ summary = "컬렉션 댓글 작성" ,
36+ security = @ SecurityRequirement (name = "bearerAuth" ),
37+ responses = {
38+ @ ApiResponse (responseCode = "201" , description = "댓글 작성 성공" ,
39+ content = @ Content (schema = @ Schema (implementation = CreateCollectionCommentResponse .class ))),
40+ @ ApiResponse (responseCode = "400" , description = "잘못된 요청" , content = @ Content ),
41+ @ ApiResponse (responseCode = "401" , description = "사용자 인증 실패" , content = @ Content ),
42+ @ ApiResponse (responseCode = "404" , description = "컬렉션이 존재하지 않음" , content = @ Content )
43+ }
44+ )
45+ @ ResponseStatus (HttpStatus .CREATED )
46+ public CreateCollectionCommentResponse createComment (
47+ @ AuthenticationPrincipal UserPrincipal userPrincipal ,
48+ @ PathVariable Long collectionId ,
49+ @ RequestBody CreateCollectionCommentRequest request
50+ ) {
51+ return commentCommandService .createComment (userPrincipal .getId (), collectionId , request );
52+ }
53+
54+ /* 댓글 수정 */
55+ @ PatchMapping ("/{collectionId}/comments/{commentId}" )
56+ @ PreAuthorize ("hasRole('USER')" )
57+ @ Operation (
58+ summary = "컬렉션 댓글 수정" ,
59+ security = @ SecurityRequirement (name = "bearerAuth" ),
60+ responses = {
61+ @ ApiResponse (responseCode = "200" , description = "댓글 수정 성공" ,
62+ content = @ Content (schema = @ Schema (implementation = UpdateCollectionCommentResponse .class ))),
63+ @ ApiResponse (responseCode = "400" , description = "잘못된 요청" , content = @ Content ),
64+ @ ApiResponse (responseCode = "401" , description = "사용자 인증 실패" , content = @ Content ),
65+ @ ApiResponse (responseCode = "403" , description = "권한 없음" , content = @ Content ),
66+ @ ApiResponse (responseCode = "404" , description = "댓글 또는 컬렉션이 존재하지 않음" , content = @ Content )
67+ }
68+ )
69+ public UpdateCollectionCommentResponse updateComment (
70+ @ AuthenticationPrincipal UserPrincipal userPrincipal ,
71+ @ PathVariable Long collectionId ,
72+ @ PathVariable Long commentId ,
73+ @ RequestBody UpdateCollectionCommentRequest request
74+ ) {
75+ return commentCommandService .updateComment (userPrincipal .getId (), collectionId , commentId , request );
76+ }
77+
78+ /* 댓글 삭제 */
79+ @ DeleteMapping ("/{collectionId}/comments/{commentId}" )
80+ @ PreAuthorize ("hasRole('USER')" )
81+ @ Operation (
82+ summary = "컬렉션 댓글 삭제" ,
83+ security = @ SecurityRequirement (name = "bearerAuth" ),
84+ responses = {
85+ @ ApiResponse (responseCode = "204" , description = "댓글 삭제 성공" ),
86+ @ ApiResponse (responseCode = "401" , description = "사용자 인증 실패" , content = @ Content ),
87+ @ ApiResponse (responseCode = "403" , description = "권한 없음" , content = @ Content ),
88+ @ ApiResponse (responseCode = "404" , description = "댓글 또는 컬렉션이 존재하지 않음" , content = @ Content )
89+ }
90+ )
91+ @ ResponseStatus (HttpStatus .NO_CONTENT )
92+ public void deleteComment (
93+ @ AuthenticationPrincipal UserPrincipal userPrincipal ,
94+ @ PathVariable Long collectionId ,
95+ @ PathVariable Long commentId
96+ ) {
97+ commentCommandService .deleteComment (userPrincipal .getId (), collectionId , commentId );
98+ }
99+
100+ /* 댓글 목록 */
101+ @ GetMapping ("/{collectionId}/comments" )
102+ @ PermitAll
103+ @ Operation (
104+ summary = "컬렉션 댓글 목록 조회 (인증: optional)" ,
105+ security = @ SecurityRequirement (name = "bearerAuth" ),
106+ responses = {
107+ @ ApiResponse (responseCode = "200" , description = "댓글 목록 조회 성공" ,
108+ content = @ Content (schema = @ Schema (implementation = GetCollectionCommentsResponse .class ))),
109+ @ ApiResponse (responseCode = "404" , description = "컬렉션이 존재하지 않음" , content = @ Content )
110+ }
111+ )
112+ public GetCollectionCommentsResponse listComments (
113+ @ PathVariable Long collectionId ,
114+ @ AuthenticationPrincipal UserPrincipal userPrincipal
115+ ) {
116+ Long userId = userPrincipal == null ? null : userPrincipal .getId ();
117+ return commentQueryService .getComments (collectionId , userId );
118+ }
119+
120+ /* 댓글 개수 */
121+ @ GetMapping ("/{collectionId}/comments/count" )
122+ @ PermitAll
123+ @ Operation (
124+ summary = "컬렉션 댓글 수 조회" ,
125+ responses = {
126+ @ ApiResponse (responseCode = "200" , description = "댓글 수 조회 성공" ,
127+ content = @ Content (schema = @ Schema (implementation = GetCollectionCommentCountResponse .class ))),
128+ @ ApiResponse (responseCode = "404" , description = "컬렉션이 존재하지 않음" , content = @ Content )
129+ }
130+ )
131+ public GetCollectionCommentCountResponse getCommentCount (
132+ @ PathVariable Long collectionId
133+ ) {
134+ return commentQueryService .getCommentCount (collectionId );
135+ }
136+ }
0 commit comments