From b796a64fdf6734095edb0b25b6cc6f00b49c155c Mon Sep 17 00:00:00 2001 From: songhyeonpk Date: Mon, 21 Apr 2025 16:39:57 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EC=9C=A0=EC=A0=80=ED=94=BD=20?= =?UTF-8?q?=EA=B2=8C=EC=8B=9C=EA=B8=80=20=EC=A0=80=EC=9E=A5=20=EC=9D=91?= =?UTF-8?q?=EB=8B=B5=20=EB=8D=B0=EC=9D=B4=ED=84=B0=20=EC=B6=94=EA=B0=80=20?= =?UTF-8?q?=EB=B0=8F=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/docs/asciidoc/post-api.adoc | 3 ++ .../post/controller/SavePostController.java | 13 +++--- .../post/dto/response/SavePostResponse.java | 28 +++++++++++++ .../command/post/SavePostCommand.java | 2 +- .../port/in/post/SavePostUseCase.java | 3 +- .../service/post/SavePostService.java | 5 ++- .../application/vo/post/PostInfoVo.java | 41 +++++++++++++++++++ .../com/ftm/server/post/SavePostTest.java | 5 ++- 8 files changed, 91 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/ftm/server/adapter/in/web/post/dto/response/SavePostResponse.java create mode 100644 src/main/java/com/ftm/server/application/vo/post/PostInfoVo.java diff --git a/src/docs/asciidoc/post-api.adoc b/src/docs/asciidoc/post-api.adoc index c5efb7f..9ac0b97 100644 --- a/src/docs/asciidoc/post-api.adoc +++ b/src/docs/asciidoc/post-api.adoc @@ -19,6 +19,9 @@ include::{snippetsDir}/savePost/1/request-part-data-fields.adoc[] ==== 성공 Response include::{snippetsDir}/savePost/1/http-response.adoc[] +==== Response Body Fields +include::{snippetsDir}/savePost/1/response-fields.adoc[] + ==== 실패 Response 실패 1. 인증되지 않은 유저일 경우 diff --git a/src/main/java/com/ftm/server/adapter/in/web/post/controller/SavePostController.java b/src/main/java/com/ftm/server/adapter/in/web/post/controller/SavePostController.java index 53d5ab9..aaec8d1 100644 --- a/src/main/java/com/ftm/server/adapter/in/web/post/controller/SavePostController.java +++ b/src/main/java/com/ftm/server/adapter/in/web/post/controller/SavePostController.java @@ -1,8 +1,10 @@ package com.ftm.server.adapter.in.web.post.controller; import com.ftm.server.adapter.in.web.post.dto.request.SavePostRequest; +import com.ftm.server.adapter.in.web.post.dto.response.SavePostResponse; import com.ftm.server.application.command.post.SavePostCommand; import com.ftm.server.application.port.in.post.SavePostUseCase; +import com.ftm.server.application.vo.post.PostInfoVo; import com.ftm.server.common.response.ApiResponse; import com.ftm.server.common.response.enums.SuccessResponseCode; import com.ftm.server.infrastructure.security.UserPrincipal; @@ -24,17 +26,18 @@ public class SavePostController { private final SavePostUseCase savePostUseCase; @PostMapping("/api/posts") - public ResponseEntity> savePost( + public ResponseEntity> savePost( @RequestPart(value = "data") @Valid SavePostRequest request, @RequestPart(value = "postImageFiles", required = false) List postImageFiles, @RequestPart(value = "productImageFiles", required = false) List productImageFiles, @AuthenticationPrincipal UserPrincipal userPrincipal) { - savePostUseCase.execute( - SavePostCommand.from( - userPrincipal.getId(), request, postImageFiles, productImageFiles)); + PostInfoVo vo = + savePostUseCase.execute( + SavePostCommand.from( + userPrincipal.getId(), request, postImageFiles, productImageFiles)); return ResponseEntity.status(HttpStatus.CREATED) - .body(ApiResponse.success(SuccessResponseCode.CREATED)); + .body(ApiResponse.success(SuccessResponseCode.CREATED, SavePostResponse.from(vo))); } } diff --git a/src/main/java/com/ftm/server/adapter/in/web/post/dto/response/SavePostResponse.java b/src/main/java/com/ftm/server/adapter/in/web/post/dto/response/SavePostResponse.java new file mode 100644 index 0000000..5aadbfe --- /dev/null +++ b/src/main/java/com/ftm/server/adapter/in/web/post/dto/response/SavePostResponse.java @@ -0,0 +1,28 @@ +package com.ftm.server.adapter.in.web.post.dto.response; + +import com.fasterxml.jackson.annotation.JsonFormat; +import com.ftm.server.application.vo.post.PostInfoVo; +import java.time.LocalDateTime; +import lombok.Getter; + +@Getter +public class SavePostResponse { + + private final Long postId; + + @JsonFormat(pattern = "yyyy-MM-dd HH:mm", shape = JsonFormat.Shape.STRING) + private final LocalDateTime createdAt; + + @JsonFormat(pattern = "yyyy-MM-dd HH:mm", shape = JsonFormat.Shape.STRING) + private final LocalDateTime updatedAt; + + private SavePostResponse(PostInfoVo postInfoVo) { + this.postId = postInfoVo.getId(); + this.createdAt = postInfoVo.getCreatedAt(); + this.updatedAt = postInfoVo.getUpdatedAt(); + } + + public static SavePostResponse from(PostInfoVo postInfoVo) { + return new SavePostResponse(postInfoVo); + } +} diff --git a/src/main/java/com/ftm/server/application/command/post/SavePostCommand.java b/src/main/java/com/ftm/server/application/command/post/SavePostCommand.java index 63febe4..295d361 100644 --- a/src/main/java/com/ftm/server/application/command/post/SavePostCommand.java +++ b/src/main/java/com/ftm/server/application/command/post/SavePostCommand.java @@ -27,7 +27,7 @@ private SavePostCommand( List productImages) { this.userId = userId; this.title = request.getTitle(); - this.groomingCategory = getGroomingCategory(); + this.groomingCategory = request.getGroomingCategory(); this.hashTags = request.getHashtags().toArray(new HashTag[0]); this.content = request.getContent(); this.postImages = postImages; diff --git a/src/main/java/com/ftm/server/application/port/in/post/SavePostUseCase.java b/src/main/java/com/ftm/server/application/port/in/post/SavePostUseCase.java index ca59d35..644a615 100644 --- a/src/main/java/com/ftm/server/application/port/in/post/SavePostUseCase.java +++ b/src/main/java/com/ftm/server/application/port/in/post/SavePostUseCase.java @@ -1,10 +1,11 @@ package com.ftm.server.application.port.in.post; import com.ftm.server.application.command.post.SavePostCommand; +import com.ftm.server.application.vo.post.PostInfoVo; import com.ftm.server.common.annotation.UseCase; @UseCase public interface SavePostUseCase { - void execute(SavePostCommand command); + PostInfoVo execute(SavePostCommand command); } diff --git a/src/main/java/com/ftm/server/application/service/post/SavePostService.java b/src/main/java/com/ftm/server/application/service/post/SavePostService.java index b9eca41..2acfb7c 100644 --- a/src/main/java/com/ftm/server/application/service/post/SavePostService.java +++ b/src/main/java/com/ftm/server/application/service/post/SavePostService.java @@ -11,6 +11,7 @@ import com.ftm.server.application.port.out.s3.S3PostImageUploadPort; import com.ftm.server.application.port.out.s3.S3PostProductImageUploadPort; import com.ftm.server.application.port.out.transcation.AfterRollbackExecutorPort; +import com.ftm.server.application.vo.post.PostInfoVo; import com.ftm.server.common.exception.CustomException; import com.ftm.server.common.response.enums.ErrorResponseCode; import com.ftm.server.domain.entity.Post; @@ -44,7 +45,7 @@ public class SavePostService implements SavePostUseCase { @Override @Transactional - public void execute(SavePostCommand command) { + public PostInfoVo execute(SavePostCommand command) { // 상품, 상품이미지 검증 validateProductImages(command.getProducts(), command.getProductImages()); @@ -98,6 +99,8 @@ public void execute(SavePostCommand command) { postProductImages.add(PostProductImage.createDefault(postProduct.getId())); } savePostProductImagePort.savePostProductImages(postProductImages); + + return PostInfoVo.from(post); } private void validateProductImages( diff --git a/src/main/java/com/ftm/server/application/vo/post/PostInfoVo.java b/src/main/java/com/ftm/server/application/vo/post/PostInfoVo.java new file mode 100644 index 0000000..5ccea8c --- /dev/null +++ b/src/main/java/com/ftm/server/application/vo/post/PostInfoVo.java @@ -0,0 +1,41 @@ +package com.ftm.server.application.vo.post; + +import com.ftm.server.domain.entity.Post; +import com.ftm.server.domain.enums.GroomingCategory; +import com.ftm.server.domain.enums.HashTag; +import java.time.LocalDateTime; +import lombok.Getter; + +@Getter +public class PostInfoVo { + + private final Long id; + private final String title; + private final String content; + private final GroomingCategory groomingCategory; + private final HashTag[] hashtags; + private final Integer viewCount; + private final Integer likeCount; + private final Boolean isDeleted; + private final LocalDateTime deletedAt; + private final LocalDateTime createdAt; + private final LocalDateTime updatedAt; + + private PostInfoVo(Post post) { + this.id = post.getId(); + this.title = post.getTitle(); + this.content = post.getContent(); + this.groomingCategory = post.getGroomingCategory(); + this.hashtags = post.getHashtags(); + this.viewCount = post.getViewCount(); + this.likeCount = post.getLikeCount(); + this.isDeleted = post.getIsDeleted(); + this.deletedAt = post.getDeletedAt(); + this.createdAt = post.getCreatedAt(); + this.updatedAt = post.getUpdatedAt(); + } + + public static PostInfoVo from(Post post) { + return new PostInfoVo(post); + } +} diff --git a/src/test/java/com/ftm/server/post/SavePostTest.java b/src/test/java/com/ftm/server/post/SavePostTest.java index e0b0df8..204a5dd 100644 --- a/src/test/java/com/ftm/server/post/SavePostTest.java +++ b/src/test/java/com/ftm/server/post/SavePostTest.java @@ -89,7 +89,10 @@ public class SavePostTest extends BaseTest { fieldWithPath("status").type(NUMBER).description("응답 상태"), fieldWithPath("code").type(STRING).description("상태 코드"), fieldWithPath("message").type(STRING).description("메시지"), - fieldWithPath("data").type(OBJECT).optional().description("응답 데이터")); + fieldWithPath("data").type(OBJECT).optional().description("응답 데이터"), + fieldWithPath("data.postId").type(NUMBER).description("생성된 유저픽 게시글 ID"), + fieldWithPath("data.createdAt").type(STRING).description("게시글 생성 날짜"), + fieldWithPath("data.updatedAt").type(STRING).description("게시글 수정 날짜")); private ResultActions getResultActions( MockHttpSession session,