From 8de620e82710ed4e3cb2146de6d199210236766a Mon Sep 17 00:00:00 2001 From: linirini <101927543+linirini@users.noreply.github.com> Date: Wed, 13 Nov 2024 19:01:55 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20comments=20api=20uri=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20#544=20(#549)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: comment uri 변경 및 버저닝 * refactor: 불필요한 코드 변경을 최소화하기 위해 uri 상에서 버저닝 위치 변경 --- .../comment/controller/CommentController.java | 24 ++++++++++--- .../docs/CommentControllerDocs.java | 35 +++++++++++++++++-- .../controller/CommentControllerTest.java | 11 ++++++ 3 files changed, 63 insertions(+), 7 deletions(-) diff --git a/backend/src/main/java/com/staccato/comment/controller/CommentController.java b/backend/src/main/java/com/staccato/comment/controller/CommentController.java index 86ee389e7..a1ba6ffed 100644 --- a/backend/src/main/java/com/staccato/comment/controller/CommentController.java +++ b/backend/src/main/java/com/staccato/comment/controller/CommentController.java @@ -1,21 +1,19 @@ package com.staccato.comment.controller; import java.net.URI; - import jakarta.validation.Valid; import jakarta.validation.constraints.Min; - import org.springframework.http.ResponseEntity; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; - import com.staccato.comment.controller.docs.CommentControllerDocs; import com.staccato.comment.service.CommentService; import com.staccato.comment.service.dto.request.CommentRequest; @@ -24,7 +22,6 @@ import com.staccato.config.auth.LoginMember; import com.staccato.config.log.annotation.Trace; import com.staccato.member.domain.Member; - import lombok.RequiredArgsConstructor; @Trace @@ -64,6 +61,16 @@ public ResponseEntity updateComment( return ResponseEntity.ok().build(); } + @PutMapping("/v2/{commentId}") + public ResponseEntity updateCommentV2( + @LoginMember Member member, + @PathVariable @Min(value = 1L, message = "댓글 식별자는 양수로 이루어져야 합니다.") long commentId, + @Valid @RequestBody CommentUpdateRequest commentUpdateRequest + ) { + commentService.updateComment(member, commentId, commentUpdateRequest); + return ResponseEntity.ok().build(); + } + @DeleteMapping public ResponseEntity deleteComment( @RequestParam @Min(value = 1L, message = "댓글 식별자는 양수로 이루어져야 합니다.") long commentId, @@ -72,4 +79,13 @@ public ResponseEntity deleteComment( commentService.deleteComment(commentId, member); return ResponseEntity.ok().build(); } + + @DeleteMapping("/v2/{commentId}") + public ResponseEntity deleteCommentV2( + @PathVariable @Min(value = 1L, message = "댓글 식별자는 양수로 이루어져야 합니다.") long commentId, + @LoginMember Member member + ) { + commentService.deleteComment(commentId, member); + return ResponseEntity.ok().build(); + } } diff --git a/backend/src/main/java/com/staccato/comment/controller/docs/CommentControllerDocs.java b/backend/src/main/java/com/staccato/comment/controller/docs/CommentControllerDocs.java index 3aa7bf733..76ace9027 100644 --- a/backend/src/main/java/com/staccato/comment/controller/docs/CommentControllerDocs.java +++ b/backend/src/main/java/com/staccato/comment/controller/docs/CommentControllerDocs.java @@ -2,14 +2,11 @@ import jakarta.validation.Valid; import jakarta.validation.constraints.Min; - import org.springframework.http.ResponseEntity; - import com.staccato.comment.service.dto.request.CommentRequest; import com.staccato.comment.service.dto.request.CommentUpdateRequest; import com.staccato.comment.service.dto.response.CommentResponses; import com.staccato.member.domain.Member; - import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.responses.ApiResponse; @@ -70,6 +67,28 @@ ResponseEntity updateComment( @Parameter(description = "댓글 식별자", example = "1") @Min(value = 1L, message = "댓글 식별자는 양수로 이루어져야 합니다.") long commentId, @Parameter(description = "댓글 수정 시 요구 형식") @Valid CommentUpdateRequest commentUpdateRequest); + @Operation(summary = "댓글 수정", description = "댓글을 수정합니다.") + @ApiResponses(value = { + @ApiResponse(description = "댓글 수정 성공", responseCode = "200"), + @ApiResponse(description = """ + <발생 가능한 케이스> + + (1) 댓글 식별자가 양수가 아닐 때 + + (2) 요청한 댓글을 찾을 수 없을 때 + + (3) 댓글 내용이 공백 뿐이거나 없을 때 + + (4) 댓글이 공백 포함 500자 초과일 때 + """, + responseCode = "400") + }) + public ResponseEntity updateCommentV2( + @Parameter(hidden = true) Member member, + @Parameter(description = "댓글 식별자", example = "1") @Min(value = 1L, message = "댓글 식별자는 양수로 이루어져야 합니다.") long commentId, + @Parameter(description = "댓글 수정 시 요구 형식") @Valid CommentUpdateRequest commentUpdateRequest + ); + @Operation(summary = "댓글 삭제", description = "댓글을 삭제합니다.") @ApiResponses(value = { @ApiResponse(description = "댓글 삭제 성공", responseCode = "200"), @@ -78,4 +97,14 @@ ResponseEntity updateComment( ResponseEntity deleteComment( @Parameter(description = "댓글 식별자", example = "1") @Min(value = 1L, message = "댓글 식별자는 양수로 이루어져야 합니다.") long commentId, @Parameter(hidden = true) Member member); + + @Operation(summary = "댓글 삭제", description = "댓글을 삭제합니다.") + @ApiResponses(value = { + @ApiResponse(description = "댓글 삭제 성공", responseCode = "200"), + @ApiResponse(description = "댓글 식별자가 양수가 아닐 시 댓글 삭제 실패", responseCode = "400") + }) + public ResponseEntity deleteCommentV2( + @Parameter(description = "댓글 식별자", example = "1") @Min(value = 1L, message = "댓글 식별자는 양수로 이루어져야 합니다.") long commentId, + @Parameter(hidden = true) Member member + ); } diff --git a/backend/src/test/java/com/staccato/comment/controller/CommentControllerTest.java b/backend/src/test/java/com/staccato/comment/controller/CommentControllerTest.java index fa6a089cc..95927da8b 100644 --- a/backend/src/test/java/com/staccato/comment/controller/CommentControllerTest.java +++ b/backend/src/test/java/com/staccato/comment/controller/CommentControllerTest.java @@ -184,6 +184,12 @@ void updateComment() throws Exception { .contentType(MediaType.APPLICATION_JSON) .header(HttpHeaders.AUTHORIZATION, "token")) .andExpect(status().isOk()); + + mockMvc.perform(put("/comments/v2/{commentId}", 1) + .content(commentUpdateRequest) + .contentType(MediaType.APPLICATION_JSON) + .header(HttpHeaders.AUTHORIZATION, "token")) + .andExpect(status().isOk()); } @DisplayName("댓글 식별자가 양수가 아닐 경우 댓글 수정에 실패한다.") @@ -236,6 +242,11 @@ void deleteComment() throws Exception { .contentType(MediaType.APPLICATION_JSON) .header(HttpHeaders.AUTHORIZATION, "token")) .andExpect(status().isOk()); + + mockMvc.perform(delete("/comments/v2/{commentId}", 1) + .contentType(MediaType.APPLICATION_JSON) + .header(HttpHeaders.AUTHORIZATION, "token")) + .andExpect(status().isOk()); } @DisplayName("댓글 식별자가 양수가 아닐 경우 댓글 삭제에 실패한다.")