From 161c433cdf451529f53cc4cec1b512d5087ea733 Mon Sep 17 00:00:00 2001 From: yunseo02 <154687627+yunseo02@users.noreply.github.com> Date: Wed, 12 Feb 2025 17:32:11 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20[#121]=20feature:=20=EA=B2=8C?= =?UTF-8?q?=EC=8B=9C=EB=AC=BC=20=EA=B3=B5=EA=B0=9C=EB=B2=94=EC=9C=84=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 게시물 수정해서 공개범위만 수정할 수 있는 api입니다. --- .../post/controller/PostController.java | 66 +++++++++++++++++++ .../example/mody/domain/post/entity/Post.java | 4 ++ .../post/service/PostCommandService.java | 2 + .../post/service/PostCommandServiceImpl.java | 10 +++ 4 files changed, 82 insertions(+) diff --git a/src/main/java/com/example/mody/domain/post/controller/PostController.java b/src/main/java/com/example/mody/domain/post/controller/PostController.java index db65263..3daf39f 100644 --- a/src/main/java/com/example/mody/domain/post/controller/PostController.java +++ b/src/main/java/com/example/mody/domain/post/controller/PostController.java @@ -477,4 +477,70 @@ public BaseResponse updatePost( return BaseResponse.onSuccess(null); } + + @PatchMapping("/{postId}/public") + @Operation(summary = "게시글 나만 보기 수정 API", description = "인증된 유저의 게시글 나만 보기 수정 API.") + @ApiResponses({ + @ApiResponse(responseCode = "COMMON200", description = "게시글 수정 성공"), + @ApiResponse( + responseCode = "COMMON401", + description = "로그인을 하지 않았을 경우", + content = @Content( + mediaType = "application/json", + examples = @ExampleObject( + value = """ + { + "timestamp": "2025-01-26T21:23:51.4515304", + "code": "COMMON401", + "message": "인증이 필요합니다." + } + """ + ) + ) + ), + @ApiResponse( + responseCode = "POST403", + description = "작성자가 아닌 유저의 요청으로 권한이 없는 경우", + content = @Content( + mediaType = "application/json", + examples = @ExampleObject( + value = """ + { + "timestamp": "2025-01-27T20:56:55.7942672", + "code": "POST403", + "message": "해당 게시글에 대한 권한이 없습니다." + } + """ + ) + ) + ), + @ApiResponse( + responseCode = "POST404", + description = "해당 게시물을 찾을 수 없습니다.", + content = @Content( + mediaType = "application/json", + examples = @ExampleObject( + value = """ + { + "timestamp": "2024-01-13T10:00:00", + "isSuccess": "false", + "code": "POST404", + "message": "해당 게시물을 찾을 수 없습니다." + } + """ + ) + ) + ) + }) + @Parameters({ + @Parameter(name = "postId", description = "게시글 아이디, path variable 입니다") + }) + public BaseResponse updatePostIsPublic( + @PathVariable(name = "postId") Long postId, + @AuthenticationPrincipal CustomUserDetails customUserDetails) { + + postCommandService.togglePostPublic(customUserDetails.getMember(), postId); + return BaseResponse.onSuccess(null); + } + } diff --git a/src/main/java/com/example/mody/domain/post/entity/Post.java b/src/main/java/com/example/mody/domain/post/entity/Post.java index c12ab35..c66ee58 100644 --- a/src/main/java/com/example/mody/domain/post/entity/Post.java +++ b/src/main/java/com/example/mody/domain/post/entity/Post.java @@ -96,4 +96,8 @@ public void updatePost(String content, boolean isPublic) { this.content = content; this.isPublic = isPublic; } + + public void toggleIsPublic() { + this.isPublic = !isPublic; + } } diff --git a/src/main/java/com/example/mody/domain/post/service/PostCommandService.java b/src/main/java/com/example/mody/domain/post/service/PostCommandService.java index c5cfb86..dfb5cab 100644 --- a/src/main/java/com/example/mody/domain/post/service/PostCommandService.java +++ b/src/main/java/com/example/mody/domain/post/service/PostCommandService.java @@ -15,4 +15,6 @@ public interface PostCommandService { public void reportPost(Member member, Long postId); public void updatePost(PostUpdateRequest request, Long postId, Member member); + + void togglePostPublic(Member member, Long postId); } diff --git a/src/main/java/com/example/mody/domain/post/service/PostCommandServiceImpl.java b/src/main/java/com/example/mody/domain/post/service/PostCommandServiceImpl.java index 286105d..27e3b2d 100644 --- a/src/main/java/com/example/mody/domain/post/service/PostCommandServiceImpl.java +++ b/src/main/java/com/example/mody/domain/post/service/PostCommandServiceImpl.java @@ -166,6 +166,16 @@ public void updatePost(PostUpdateRequest request, Long postId, Member member){ post.updatePost(request.getContent(), request.getIsPublic()); } + @Override + public void togglePostPublic(Member member, Long postId) { + + Post post = postRepository.findById(postId) + .orElseThrow(() -> new PostException(POST_NOT_FOUND)); + checkAuthorization(member, post); + post.toggleIsPublic(); + } + + @Transactional protected void delete(Post post) { List postImageUrls = postImageRepository.findPostImageUrlByPostId(post.getId());