|
| 1 | +package org.devkor.apu.saerok_server.domain.freeboard.api; |
| 2 | + |
| 3 | +import io.swagger.v3.oas.annotations.Operation; |
| 4 | +import io.swagger.v3.oas.annotations.Parameter; |
| 5 | +import io.swagger.v3.oas.annotations.media.Content; |
| 6 | +import io.swagger.v3.oas.annotations.media.Schema; |
| 7 | +import io.swagger.v3.oas.annotations.responses.ApiResponse; |
| 8 | +import io.swagger.v3.oas.annotations.security.SecurityRequirement; |
| 9 | +import io.swagger.v3.oas.annotations.tags.Tag; |
| 10 | +import jakarta.annotation.security.PermitAll; |
| 11 | +import jakarta.validation.Valid; |
| 12 | +import lombok.RequiredArgsConstructor; |
| 13 | +import org.devkor.apu.saerok_server.domain.freeboard.api.dto.request.CreateFreeBoardPostCommentRequest; |
| 14 | +import org.devkor.apu.saerok_server.domain.freeboard.api.dto.request.UpdateFreeBoardPostCommentRequest; |
| 15 | +import org.devkor.apu.saerok_server.domain.freeboard.api.dto.response.CreateFreeBoardPostCommentResponse; |
| 16 | +import org.devkor.apu.saerok_server.domain.freeboard.api.dto.response.GetFreeBoardPostCommentCountResponse; |
| 17 | +import org.devkor.apu.saerok_server.domain.freeboard.api.dto.response.GetFreeBoardPostCommentsResponse; |
| 18 | +import org.devkor.apu.saerok_server.domain.freeboard.api.dto.response.UpdateFreeBoardPostCommentResponse; |
| 19 | +import org.devkor.apu.saerok_server.domain.freeboard.application.FreeBoardPostCommentCommandService; |
| 20 | +import org.devkor.apu.saerok_server.domain.freeboard.application.FreeBoardPostCommentQueryService; |
| 21 | +import org.devkor.apu.saerok_server.domain.freeboard.application.dto.FreeBoardCommentQueryCommand; |
| 22 | +import org.devkor.apu.saerok_server.global.security.principal.UserPrincipal; |
| 23 | +import org.devkor.apu.saerok_server.global.shared.exception.BadRequestException; |
| 24 | +import org.springframework.http.HttpStatus; |
| 25 | +import org.springframework.security.access.prepost.PreAuthorize; |
| 26 | +import org.springframework.security.core.annotation.AuthenticationPrincipal; |
| 27 | +import org.springframework.web.bind.annotation.*; |
| 28 | + |
| 29 | +@Tag(name = "Free Board API", description = "자유게시판 관련 API") |
| 30 | +@RestController |
| 31 | +@RequiredArgsConstructor |
| 32 | +@RequestMapping("${api_prefix}/community/freeboard/posts") |
| 33 | +public class FreeBoardPostCommentController { |
| 34 | + |
| 35 | + private final FreeBoardPostCommentCommandService commentCommandService; |
| 36 | + private final FreeBoardPostCommentQueryService commentQueryService; |
| 37 | + |
| 38 | + /* 댓글 작성 */ |
| 39 | + @PostMapping("/{postId}/comments") |
| 40 | + @PreAuthorize("isAuthenticated()") |
| 41 | + @Operation( |
| 42 | + summary = "자유게시판 댓글 작성", |
| 43 | + security = @SecurityRequirement(name = "bearerAuth"), |
| 44 | + responses = { |
| 45 | + @ApiResponse(responseCode = "201", description = "댓글 작성 성공", |
| 46 | + content = @Content(schema = @Schema(implementation = CreateFreeBoardPostCommentResponse.class))), |
| 47 | + @ApiResponse(responseCode = "400", description = "잘못된 요청", content = @Content), |
| 48 | + @ApiResponse(responseCode = "401", description = "사용자 인증 실패", content = @Content), |
| 49 | + @ApiResponse(responseCode = "404", description = "게시글이 존재하지 않음", content = @Content) |
| 50 | + } |
| 51 | + ) |
| 52 | + @ResponseStatus(HttpStatus.CREATED) |
| 53 | + public CreateFreeBoardPostCommentResponse createComment( |
| 54 | + @AuthenticationPrincipal UserPrincipal userPrincipal, |
| 55 | + @PathVariable Long postId, |
| 56 | + @Valid @RequestBody CreateFreeBoardPostCommentRequest request |
| 57 | + ) { |
| 58 | + return commentCommandService.createComment(userPrincipal.getId(), postId, request); |
| 59 | + } |
| 60 | + |
| 61 | + /* 댓글 목록 조회 */ |
| 62 | + @GetMapping("/{postId}/comments") |
| 63 | + @PermitAll |
| 64 | + @Operation( |
| 65 | + summary = "자유게시판 댓글 목록 조회 (인증: optional)", |
| 66 | + description = """ |
| 67 | + 게시글의 댓글 목록을 조회합니다. |
| 68 | +
|
| 69 | + 📄 **페이징 (선택)** |
| 70 | + - `page`와 `size`는 둘 다 제공해야 하며, 하나만 제공 시 Bad Request가 발생합니다. |
| 71 | + - 생략하면 전체 결과를 반환합니다. |
| 72 | + """, |
| 73 | + security = @SecurityRequirement(name = "bearerAuth"), |
| 74 | + responses = { |
| 75 | + @ApiResponse(responseCode = "200", description = "댓글 목록 조회 성공", |
| 76 | + content = @Content(schema = @Schema(implementation = GetFreeBoardPostCommentsResponse.class))), |
| 77 | + @ApiResponse(responseCode = "400", description = "잘못된 요청", content = @Content), |
| 78 | + @ApiResponse(responseCode = "404", description = "게시글이 존재하지 않음", content = @Content) |
| 79 | + } |
| 80 | + ) |
| 81 | + public GetFreeBoardPostCommentsResponse listComments( |
| 82 | + @PathVariable Long postId, |
| 83 | + @AuthenticationPrincipal UserPrincipal userPrincipal, |
| 84 | + @Parameter(description = "페이지 번호 (1부터 시작)", example = "1") @RequestParam(required = false) Integer page, |
| 85 | + @Parameter(description = "페이지 크기", example = "20") @RequestParam(required = false) Integer size |
| 86 | + ) { |
| 87 | + FreeBoardCommentQueryCommand command = new FreeBoardCommentQueryCommand(page, size); |
| 88 | + if (!command.hasValidPagination()) { |
| 89 | + throw new BadRequestException("page와 size 값이 유효하지 않아요."); |
| 90 | + } |
| 91 | + |
| 92 | + Long userId = userPrincipal == null ? null : userPrincipal.getId(); |
| 93 | + return commentQueryService.getComments(postId, userId, command); |
| 94 | + } |
| 95 | + |
| 96 | + /* 댓글 수 조회 */ |
| 97 | + @GetMapping("/{postId}/comments/count") |
| 98 | + @PermitAll |
| 99 | + @Operation( |
| 100 | + summary = "자유게시판 댓글 수 조회", |
| 101 | + responses = { |
| 102 | + @ApiResponse(responseCode = "200", description = "댓글 수 조회 성공", |
| 103 | + content = @Content(schema = @Schema(implementation = GetFreeBoardPostCommentCountResponse.class))), |
| 104 | + @ApiResponse(responseCode = "404", description = "게시글이 존재하지 않음", content = @Content) |
| 105 | + } |
| 106 | + ) |
| 107 | + public GetFreeBoardPostCommentCountResponse getCommentCount( |
| 108 | + @PathVariable Long postId |
| 109 | + ) { |
| 110 | + return commentQueryService.getCommentCount(postId); |
| 111 | + } |
| 112 | + |
| 113 | + /* 댓글 수정 */ |
| 114 | + @PatchMapping("/{postId}/comments/{commentId}") |
| 115 | + @PreAuthorize("isAuthenticated()") |
| 116 | + @Operation( |
| 117 | + summary = "자유게시판 댓글 수정", |
| 118 | + security = @SecurityRequirement(name = "bearerAuth"), |
| 119 | + responses = { |
| 120 | + @ApiResponse(responseCode = "200", description = "댓글 수정 성공", |
| 121 | + content = @Content(schema = @Schema(implementation = UpdateFreeBoardPostCommentResponse.class))), |
| 122 | + @ApiResponse(responseCode = "400", description = "잘못된 요청", content = @Content), |
| 123 | + @ApiResponse(responseCode = "401", description = "사용자 인증 실패", content = @Content), |
| 124 | + @ApiResponse(responseCode = "403", description = "권한 없음", content = @Content), |
| 125 | + @ApiResponse(responseCode = "404", description = "댓글 또는 게시글이 존재하지 않음", content = @Content) |
| 126 | + } |
| 127 | + ) |
| 128 | + public UpdateFreeBoardPostCommentResponse updateComment( |
| 129 | + @AuthenticationPrincipal UserPrincipal userPrincipal, |
| 130 | + @PathVariable Long postId, |
| 131 | + @PathVariable Long commentId, |
| 132 | + @Valid @RequestBody UpdateFreeBoardPostCommentRequest request |
| 133 | + ) { |
| 134 | + return commentCommandService.updateComment(userPrincipal.getId(), postId, commentId, request); |
| 135 | + } |
| 136 | + |
| 137 | + /* 댓글 삭제 */ |
| 138 | + @DeleteMapping("/{postId}/comments/{commentId}") |
| 139 | + @PreAuthorize("isAuthenticated()") |
| 140 | + @Operation( |
| 141 | + summary = "자유게시판 댓글 삭제", |
| 142 | + security = @SecurityRequirement(name = "bearerAuth"), |
| 143 | + responses = { |
| 144 | + @ApiResponse(responseCode = "204", description = "댓글 삭제 성공"), |
| 145 | + @ApiResponse(responseCode = "401", description = "사용자 인증 실패", content = @Content), |
| 146 | + @ApiResponse(responseCode = "403", description = "권한 없음", content = @Content), |
| 147 | + @ApiResponse(responseCode = "404", description = "댓글 또는 게시글이 존재하지 않음", content = @Content) |
| 148 | + } |
| 149 | + ) |
| 150 | + @ResponseStatus(HttpStatus.NO_CONTENT) |
| 151 | + public void deleteComment( |
| 152 | + @AuthenticationPrincipal UserPrincipal userPrincipal, |
| 153 | + @PathVariable Long postId, |
| 154 | + @PathVariable Long commentId |
| 155 | + ) { |
| 156 | + commentCommandService.deleteComment(userPrincipal.getId(), postId, commentId); |
| 157 | + } |
| 158 | +} |
0 commit comments