From e208e07e13c52e8b82f9a6a21d29d0cd60dc57d3 Mon Sep 17 00:00:00 2001 From: kitaee Date: Tue, 26 Nov 2024 05:37:26 +0900 Subject: [PATCH 1/2] =?UTF-8?q?add:=20=EA=B2=8C=EC=8B=9C=EA=B8=80=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C=20=EA=B6=8C=ED=95=9C=20=EA=B4=80=EB=A0=A8=20?= =?UTF-8?q?=EC=98=88=EC=99=B8=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/pilot/instagram/exception/ErrorCode.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/pilot/instagram/exception/ErrorCode.java b/src/main/java/pilot/instagram/exception/ErrorCode.java index bb35733..8696565 100644 --- a/src/main/java/pilot/instagram/exception/ErrorCode.java +++ b/src/main/java/pilot/instagram/exception/ErrorCode.java @@ -12,7 +12,8 @@ public enum ErrorCode { USER_NOT_FOUND("아이디를 찾을 수 없습니다"), // POST - POST_NOT_FOUND("게시글을 찾을 수 없습니다."); + POST_NOT_FOUND("게시글을 찾을 수 없습니다."), + UNAUTHORIZED_POST_DELETE("게시글 삭제 권한이 없습니다."); private final String message; } From e7f57bd6f571559b73069c5e01ec0fe6e02e7611 Mon Sep 17 00:00:00 2001 From: kitaee Date: Tue, 26 Nov 2024 05:37:39 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=EA=B2=8C=EC=8B=9C=EA=B8=80=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C=20API=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../instagram/domain/post/PostController.java | 7 +++++++ .../post/dto/response/PostDeleteResponse.java | 21 +++++++++++++++++++ .../domain/post/service/PostService.java | 12 +++++++++++ 3 files changed, 40 insertions(+) create mode 100644 src/main/java/pilot/instagram/domain/post/dto/response/PostDeleteResponse.java diff --git a/src/main/java/pilot/instagram/domain/post/PostController.java b/src/main/java/pilot/instagram/domain/post/PostController.java index bb09ab6..6a23131 100644 --- a/src/main/java/pilot/instagram/domain/post/PostController.java +++ b/src/main/java/pilot/instagram/domain/post/PostController.java @@ -8,6 +8,7 @@ import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; import pilot.instagram.domain.post.dto.request.PostRequest; +import pilot.instagram.domain.post.dto.response.PostDeleteResponse; import pilot.instagram.domain.post.dto.response.PostPagingResponse; import pilot.instagram.domain.post.dto.response.PostResponse; import pilot.instagram.domain.post.service.PostService; @@ -36,4 +37,10 @@ public ApiResponse> getPosts(HttpSession session, Pagea String userId = session.getAttribute("userId").toString(); return ApiResponse.of(HttpStatus.OK, postService.getPosts(userId, pageable)); } + + @DeleteMapping("/{postId}") + public ApiResponse deletePost(@PathVariable("postId") Long postId, HttpSession session) { + String userId = session.getAttribute("userId").toString(); + return ApiResponse.of(HttpStatus.OK, postService.deletePost(postId, userId)); + } } diff --git a/src/main/java/pilot/instagram/domain/post/dto/response/PostDeleteResponse.java b/src/main/java/pilot/instagram/domain/post/dto/response/PostDeleteResponse.java new file mode 100644 index 0000000..de9f5d1 --- /dev/null +++ b/src/main/java/pilot/instagram/domain/post/dto/response/PostDeleteResponse.java @@ -0,0 +1,21 @@ +package pilot.instagram.domain.post.dto.response; + +import lombok.Builder; +import lombok.Getter; +import pilot.instagram.domain.post.entity.Post; + +@Getter +public class PostDeleteResponse { + private Long id; + + @Builder + private PostDeleteResponse(Long id) { + this.id = id; + } + + public static PostDeleteResponse of(Post post) { + return PostDeleteResponse.builder() + .id(post.getId()) + .build(); + } +} diff --git a/src/main/java/pilot/instagram/domain/post/service/PostService.java b/src/main/java/pilot/instagram/domain/post/service/PostService.java index 4a37e19..6be3c80 100644 --- a/src/main/java/pilot/instagram/domain/post/service/PostService.java +++ b/src/main/java/pilot/instagram/domain/post/service/PostService.java @@ -6,6 +6,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import pilot.instagram.domain.post.dto.request.PostRequest; +import pilot.instagram.domain.post.dto.response.PostDeleteResponse; import pilot.instagram.domain.post.dto.response.PostPagingResponse; import pilot.instagram.domain.post.dto.response.PostResponse; import pilot.instagram.domain.post.entity.Post; @@ -38,4 +39,15 @@ public PostResponse getPost(Long postId) { public Page getPosts(String userId, Pageable pageable) { return postRepository.findPostByUserIdWithPaged(userId, pageable); } + + @Transactional + public PostDeleteResponse deletePost(Long postId, String userId) { + Post post = postRepository.findById(postId) + .orElseThrow(() -> new IllegalArgumentException(ErrorCode.POST_NOT_FOUND.getMessage())); + if(!post.getUser().getId().equals(userId)) { + throw new IllegalArgumentException(ErrorCode.UNAUTHORIZED_POST_DELETE.getMessage()); + } + postRepository.delete(post); + return PostDeleteResponse.of(post); + } }