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); + } } 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; }