From 7e61bd6fb5ed90addaea65f7d19ae680afc2d2c2 Mon Sep 17 00:00:00 2001 From: yerimkoko Date: Thu, 27 Jun 2024 23:00:16 +0900 Subject: [PATCH] =?UTF-8?q?[TDC-112]=20fix:=20=EC=9D=BC=EB=8B=A8=20workspa?= =?UTF-8?q?ceId=20=EB=8A=94=20=EB=B9=BC=EA=B3=A0,=20WIP:=20=EB=8B=A8?= =?UTF-8?q?=EA=B1=B4=EC=A1=B0=ED=9A=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- threedollar-application/http/post.http | 12 ++++-- .../controller/post/PostController.java | 34 +++++++++++---- .../service/post/PostFacadeService.java | 13 +++++- .../threedollar/service/post/PostService.java | 26 ++++++++++-- .../service/post/request/GetPostRequest.java | 5 +-- .../service/post/request/PostAddRequest.java | 5 +-- .../post/request/PostAndCursorRequest.java | 6 +-- .../post/request/PostUpdateRequest.java | 41 +++++++++++++++++++ .../service/post/response/PostResponse.java | 4 +- .../service/post/PostServiceTest.java | 4 +- .../com/threedollar/domain/post/Post.java | 7 ++++ .../post/repository/PostRepositoryCustom.java | 2 +- .../repository/PostRepositoryCustomImpl.java | 9 +++- 13 files changed, 132 insertions(+), 36 deletions(-) create mode 100644 threedollar-application/src/main/java/com/threedollar/service/post/request/PostUpdateRequest.java diff --git a/threedollar-application/http/post.http b/threedollar-application/http/post.http index 15b9429..34123a7 100644 --- a/threedollar-application/http/post.http +++ b/threedollar-application/http/post.http @@ -22,20 +22,26 @@ Content-Type: application/json ### Post 조회 (다건 조회) -GET {{host_api}}/community/v1/post-group/NEWS_POST?cursor=8&size=3&workspaceId=threedollar-dev&targetId=3&postGroup=NEWS_POST&accountId=USER222 +GET {{host_api}}/community/v1/post-group/NEWS_POST?cursor=8&size=3&targetId=3&postGroup=NEWS_POST&accountId=USER222 X-Community-Api-Key: {{api_key}} Content-Type: application/json ### Post 단건 조회 -GET {{host_api}}/community/v1/post-group/NEWS_POST/post/2?accountId=USER222&workspaceId=threedollar-dev&targetId=3 +GET {{host_api}}/community/v1/post-group/NEWS_POST/post/2?targetId=3 X-Community-Api-Key: {{api_key}} Content-Type: application/json ### Post targetId 에 해당하는 post 개수 조회 -GET {{host_api}}/community/v1/post-group/NEWS_POST/count?workspaceId=threedollar-dev&targetId=3 +GET {{host_api}}/community/v1/post-group/NEWS_POST/count?targetId=3 X-Community-Api-Key: {{api_key}} Content-Type: application/json + + +### Post 수정 +GET {{host_api}}/community/v1/post-group/NEWS_POST/post/2?targetId=3 +X-Community-Api-Key: {{api_key}} + diff --git a/threedollar-application/src/main/java/com/threedollar/controller/post/PostController.java b/threedollar-application/src/main/java/com/threedollar/controller/post/PostController.java index 8256255..3aecc6b 100644 --- a/threedollar-application/src/main/java/com/threedollar/controller/post/PostController.java +++ b/threedollar-application/src/main/java/com/threedollar/controller/post/PostController.java @@ -8,12 +8,14 @@ import com.threedollar.service.post.request.GetPostRequest; import com.threedollar.service.post.request.PostAddRequest; import com.threedollar.service.post.request.PostAndCursorRequest; +import com.threedollar.service.post.request.PostUpdateRequest; import com.threedollar.service.post.response.PostAndCursorResponse; import com.threedollar.service.post.response.PostResponse; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PatchMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -37,32 +39,48 @@ public ApiResponse addPost(@RequestApiKey ApiKeyContext workspaceId, @DeleteMapping("/v1/post-group/{postGroup}/post/{postId}") public ApiResponse deletePost(@PathVariable Long postId, + @RequestApiKey ApiKeyContext workspaceId, @PathVariable PostGroup postGroup, @Valid GetPostRequest request) { - postFacadeService.deletePost(request.getWorkspaceId(), request.getAccountId(), postId, postGroup, request.getTargetId()); + postFacadeService.deletePost(workspaceId.getWorkspaceId(), request.getAccountId(), postId, postGroup, request.getTargetId()); return ApiResponse.OK; } @GetMapping("/v1/post-group/{postGroup}") public ApiResponse getPosts(@Valid PostAndCursorRequest request, + @RequestApiKey ApiKeyContext workspaceId, @PathVariable PostGroup postGroup) { - return ApiResponse.success(postFacadeService.getPostAndCursor(request, postGroup)); + return ApiResponse.success(postFacadeService.getPostAndCursor(request, workspaceId.getWorkspaceId(), postGroup)); } @GetMapping("/v1/post-group/{postGroup}/post/{postId}") - public ApiResponse getPost(@Valid GetPostRequest postRequest, + public ApiResponse getPost(@RequestParam(required = false) String accountId, + @RequestParam String targetId, + @RequestApiKey ApiKeyContext workspaceId, @PathVariable PostGroup postGroup, @PathVariable Long postId) { - return ApiResponse.success(postFacadeService.getPostById(postRequest.getWorkspaceId(), - postRequest.getAccountId(), postId, postGroup, postRequest.getTargetId())); + return ApiResponse.success(postFacadeService.getPostById(workspaceId.getWorkspaceId(), + accountId, postId, postGroup, targetId)); } @GetMapping("/v1/post-group/{postGroup}/count") public ApiResponse getPostCount(@PathVariable PostGroup postGroup, - @RequestParam String workspaceId, - @RequestParam String targetId) { - return ApiResponse.success(postFacadeService.getPostCountByTargetId(workspaceId, postGroup, targetId)); + @RequestApiKey ApiKeyContext workspaceId, + @RequestParam String targetId) { + return ApiResponse.success(postFacadeService.getPostCountByTargetId(workspaceId.getWorkspaceId(), postGroup, targetId)); + + } + + @PatchMapping("/v1/post-group/{postGroup}/post/{postId}") + public ApiResponse updatePost(@Valid PostUpdateRequest request, + @RequestApiKey ApiKeyContext workspaceId, + @PathVariable PostGroup postGroup, + @PathVariable Long postId, + @RequestParam(required = false) String accountId, + @RequestParam String targetId) { + postFacadeService.updatePost(workspaceId.getWorkspaceId(), accountId, postGroup, postId, targetId, request); + return ApiResponse.OK; } diff --git a/threedollar-application/src/main/java/com/threedollar/service/post/PostFacadeService.java b/threedollar-application/src/main/java/com/threedollar/service/post/PostFacadeService.java index f583749..dd82c75 100644 --- a/threedollar-application/src/main/java/com/threedollar/service/post/PostFacadeService.java +++ b/threedollar-application/src/main/java/com/threedollar/service/post/PostFacadeService.java @@ -36,16 +36,18 @@ public void deletePost(@NotBlank String workspaceId, } public PostAndCursorResponse getPostAndCursor(@Valid PostAndCursorRequest request, + String workspaceId, PostGroup postGroup) { return postService.getPostsAndCursor( postGroup, - request.getWorkspaceId(), + workspaceId, request.getTargetId(), request.getAccountId(), request.getCursor(), request.getSize()); } + public PostResponse getPostById(String workspaceId, String accountId, Long postId, @@ -60,6 +62,13 @@ public Long getPostCountByTargetId(String workspaceId, return postService.getPostCountByTargetId(workspaceId, postGroup, targetId); } - + public void updatePost(String workspaceId, + String accountId, + PostGroup postGroup, + Long postId, + String targetId, + PostUpdateRequest request) { + postService.update(workspaceId, accountId, postId, postGroup, targetId, request); + } } diff --git a/threedollar-application/src/main/java/com/threedollar/service/post/PostService.java b/threedollar-application/src/main/java/com/threedollar/service/post/PostService.java index 9f90466..17941a6 100644 --- a/threedollar-application/src/main/java/com/threedollar/service/post/PostService.java +++ b/threedollar-application/src/main/java/com/threedollar/service/post/PostService.java @@ -5,6 +5,7 @@ import com.threedollar.domain.post.PostGroup; import com.threedollar.domain.post.repository.PostRepository; import com.threedollar.service.post.request.PostAddRequest; +import com.threedollar.service.post.request.PostUpdateRequest; import com.threedollar.service.post.response.PostAndCursorResponse; import com.threedollar.service.post.response.PostResponse; import jakarta.validation.constraints.NotBlank; @@ -14,6 +15,7 @@ import java.util.List; import java.util.Objects; +import java.util.stream.Collectors; @Service @RequiredArgsConstructor @@ -72,11 +74,26 @@ public PostResponse getPostById(String workspaceId, } public Long getPostCountByTargetId(String workspaceId, - PostGroup postGroup, - String targetId) { + PostGroup postGroup, + String targetId) { return postRepository.postCountByWorkspaceIdAndPostGroupAndTargetId(workspaceId, postGroup, targetId); } + + public void update(String workspaceId, + String accountId, + Long postId, + PostGroup postGroup, + String targetId, + PostUpdateRequest request) { + + Post post = validatePost(workspaceId, accountId, postId, postGroup, targetId); + post.update(request.getTitle(), request.getContent(), request.getPostSections().stream() + .map(r -> r.toEntity(post)) + .collect(Collectors.toList())); + } + + private Post validatePost(String workspaceId, String accountId, Long postId, @@ -90,7 +107,10 @@ private Post validatePost(String workspaceId, } - private static boolean isOwner(Post post, String accountId) { + private static Boolean isOwner(Post post, String accountId) { + if (accountId == null) { + return null; + } return Objects.equals(post.getAccountId(), accountId); } diff --git a/threedollar-application/src/main/java/com/threedollar/service/post/request/GetPostRequest.java b/threedollar-application/src/main/java/com/threedollar/service/post/request/GetPostRequest.java index dc52a0b..f241b47 100644 --- a/threedollar-application/src/main/java/com/threedollar/service/post/request/GetPostRequest.java +++ b/threedollar-application/src/main/java/com/threedollar/service/post/request/GetPostRequest.java @@ -12,15 +12,12 @@ public class GetPostRequest { @Nullable private String accountId; - @NotBlank - private String workspaceId; @NotBlank private String targetId; - public GetPostRequest(String accountId, String workspaceId, String targetId) { + public GetPostRequest(String accountId, String targetId) { this.accountId = accountId; - this.workspaceId = workspaceId; this.targetId = targetId; } } diff --git a/threedollar-application/src/main/java/com/threedollar/service/post/request/PostAddRequest.java b/threedollar-application/src/main/java/com/threedollar/service/post/request/PostAddRequest.java index 9549fad..0f1a6e0 100644 --- a/threedollar-application/src/main/java/com/threedollar/service/post/request/PostAddRequest.java +++ b/threedollar-application/src/main/java/com/threedollar/service/post/request/PostAddRequest.java @@ -22,8 +22,6 @@ public class PostAddRequest { private String title; - private String workspaceId; - private String targetId; @NotBlank @@ -33,11 +31,10 @@ public class PostAddRequest { private List sections; @Builder - public PostAddRequest(PostGroup postGroup, Long parentId, String title, String targetId, String content, String workspaceId, List sections) { + public PostAddRequest(PostGroup postGroup, Long parentId, String title, String targetId, String content, List sections) { this.postGroup = postGroup; this.parentId = parentId; this.title = title; - this.workspaceId = workspaceId; this.content = content; this.targetId = targetId; this.sections = sections; diff --git a/threedollar-application/src/main/java/com/threedollar/service/post/request/PostAndCursorRequest.java b/threedollar-application/src/main/java/com/threedollar/service/post/request/PostAndCursorRequest.java index 241c5a4..6312f45 100644 --- a/threedollar-application/src/main/java/com/threedollar/service/post/request/PostAndCursorRequest.java +++ b/threedollar-application/src/main/java/com/threedollar/service/post/request/PostAndCursorRequest.java @@ -18,19 +18,15 @@ public class PostAndCursorRequest { @Max(30) private int size; - @NotBlank - private String workspaceId; - @NotBlank private String targetId; @Nullable private String accountId; - public PostAndCursorRequest(Long cursor, int size, String workspaceId, String targetId, String accountId) { + public PostAndCursorRequest(Long cursor, int size, String targetId, String accountId) { this.cursor = cursor; this.size = size; - this.workspaceId = workspaceId; this.targetId = targetId; this.accountId = accountId; } diff --git a/threedollar-application/src/main/java/com/threedollar/service/post/request/PostUpdateRequest.java b/threedollar-application/src/main/java/com/threedollar/service/post/request/PostUpdateRequest.java new file mode 100644 index 0000000..2b68cbc --- /dev/null +++ b/threedollar-application/src/main/java/com/threedollar/service/post/request/PostUpdateRequest.java @@ -0,0 +1,41 @@ +package com.threedollar.service.post.request; + +import com.threedollar.domain.post.Post; +import com.threedollar.domain.post.postsection.PostSection; +import jakarta.annotation.Nullable; +import jakarta.validation.constraints.NotNull; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.List; +import java.util.stream.Collectors; + +@NoArgsConstructor +@Getter +public class PostUpdateRequest { + + @Nullable + private String title; + + @Nullable + private String content; + + @NotNull + private List postSections; + + @Builder + public PostUpdateRequest(String title, String content, List postSections) { + this.title = title; + this.content = content; + this.postSections = postSections; + } + + public List toEntity(Post post) { + return postSections.stream().map(postSection -> postSection.toEntity(post)) + .collect(Collectors.toList()); + + } + + +} diff --git a/threedollar-application/src/main/java/com/threedollar/service/post/response/PostResponse.java b/threedollar-application/src/main/java/com/threedollar/service/post/response/PostResponse.java index d5b992f..9799323 100644 --- a/threedollar-application/src/main/java/com/threedollar/service/post/response/PostResponse.java +++ b/threedollar-application/src/main/java/com/threedollar/service/post/response/PostResponse.java @@ -26,7 +26,7 @@ public class PostResponse { private String targetId; - private boolean isOwner; + private Boolean isOwner; private LocalDateTime createTime; @@ -48,7 +48,7 @@ public PostResponse(Long postId, PostGroup postGroup, Long parentId, String titl this.postSections = postSections; } - public static PostResponse of(Post post, boolean isOwner) { + public static PostResponse of(Post post, Boolean isOwner) { return PostResponse.builder() .postId(post.getId()) .postGroup(post.getPostGroup()) diff --git a/threedollar-application/src/test/java/com/threedollar/service/post/PostServiceTest.java b/threedollar-application/src/test/java/com/threedollar/service/post/PostServiceTest.java index 5706aad..d9f03e5 100644 --- a/threedollar-application/src/test/java/com/threedollar/service/post/PostServiceTest.java +++ b/threedollar-application/src/test/java/com/threedollar/service/post/PostServiceTest.java @@ -34,8 +34,8 @@ void clean_up() { @Test void 사장님이_소식을_작성한다() { // given - PostAddRequest request = newRequest(); String workspaceId = "three-user"; + PostAddRequest request = newRequest(); String accountId = "user22"; // when @@ -79,7 +79,6 @@ private PostAddRequest newRequest() { PostGroup postGroup = PostGroup.NEWS_POST; String targetId = "33"; - String workspaceId = "threedollar-dev"; String title = "은평구 핫도그 아저씨"; String content = "콘텐트"; SectionType sectionType = SectionType.IMAGE; @@ -97,7 +96,6 @@ private PostAddRequest newRequest() { .title(title) .content(content) .targetId(targetId) - .workspaceId(workspaceId) .sections(List.of(postSectionRequest)) .build(); } diff --git a/threedollar-core/threedollar-domain/src/main/java/com/threedollar/domain/post/Post.java b/threedollar-core/threedollar-domain/src/main/java/com/threedollar/domain/post/Post.java index ba6f44d..7ef187c 100644 --- a/threedollar-core/threedollar-domain/src/main/java/com/threedollar/domain/post/Post.java +++ b/threedollar-core/threedollar-domain/src/main/java/com/threedollar/domain/post/Post.java @@ -66,6 +66,13 @@ public void add(PostSection postSection) { this.postSection.add(postSection); } + public void update(String title, String content, List sections) { + this.postSection.clear(); + this.postSection.addAll(sections); + this.title = title; + this.content = content; + } + public void delete() { this.status = PostStatus.DELETED; } diff --git a/threedollar-core/threedollar-domain/src/main/java/com/threedollar/domain/post/repository/PostRepositoryCustom.java b/threedollar-core/threedollar-domain/src/main/java/com/threedollar/domain/post/repository/PostRepositoryCustom.java index ce55d8e..d448f7e 100644 --- a/threedollar-core/threedollar-domain/src/main/java/com/threedollar/domain/post/repository/PostRepositoryCustom.java +++ b/threedollar-core/threedollar-domain/src/main/java/com/threedollar/domain/post/repository/PostRepositoryCustom.java @@ -9,7 +9,7 @@ public interface PostRepositoryCustom { Post findByIdAndWorkspaceIdAndAccountIdAndGroupAndTargetId(Long postId, - String accountId, + @Nullable String accountId, String workspaceId, PostGroup group, String targetId); diff --git a/threedollar-core/threedollar-domain/src/main/java/com/threedollar/domain/post/repository/PostRepositoryCustomImpl.java b/threedollar-core/threedollar-domain/src/main/java/com/threedollar/domain/post/repository/PostRepositoryCustomImpl.java index e221e16..25c51d0 100644 --- a/threedollar-core/threedollar-domain/src/main/java/com/threedollar/domain/post/repository/PostRepositoryCustomImpl.java +++ b/threedollar-core/threedollar-domain/src/main/java/com/threedollar/domain/post/repository/PostRepositoryCustomImpl.java @@ -21,8 +21,8 @@ public class PostRepositoryCustomImpl implements PostRepositoryCustom { public Post findByIdAndWorkspaceIdAndAccountIdAndGroupAndTargetId(Long postId, String accountId, String workspaceId, PostGroup postGroup, String targetId) { return jpaQueryFactory.selectFrom(post) .where( + existsAccountId(accountId), post.workspaceId.eq(workspaceId), - post.accountId.eq(accountId), post.id.eq(postId), post.postGroup.eq(postGroup), post.targetId.eq(targetId), @@ -72,5 +72,12 @@ private BooleanExpression existsCursor(Long cursor) { return post.id.lt(cursor); } + private BooleanExpression existsAccountId(String accountId) { + if (accountId == null) { + return null; + } + return post.accountId.eq(accountId); + } + }