From 1867da21ed6ece06f58df3cc456c64f2c9c6ba1f Mon Sep 17 00:00:00 2001 From: enjay27 Date: Fri, 11 Jun 2021 16:47:33 +0900 Subject: [PATCH] =?UTF-8?q?[#14]feat:=20=EA=B2=8C=EC=8B=9C=EA=B8=80=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dev/controller/post/PostController.java | 18 ++++++++---- .../repository/post/PostLikeRepository.java | 7 +++++ .../com/devin/dev/service/PostService.java | 28 ++++++++++++++++--- .../com/devin/dev/utils/ResponseMessage.java | 1 + 4 files changed, 45 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/devin/dev/repository/post/PostLikeRepository.java diff --git a/src/main/java/com/devin/dev/controller/post/PostController.java b/src/main/java/com/devin/dev/controller/post/PostController.java index d4a78ff..7e1f4b6 100644 --- a/src/main/java/com/devin/dev/controller/post/PostController.java +++ b/src/main/java/com/devin/dev/controller/post/PostController.java @@ -7,10 +7,7 @@ import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Sort; import org.springframework.data.web.PageableDefault; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import org.springframework.data.domain.Pageable; @RestController @@ -26,7 +23,7 @@ public DefaultResponse getPostList(@RequestParam(defaultValue = "-1")Long id, } @GetMapping("/postlist/{id}") - public DefaultResponse findReplies( + public DefaultResponse getPostDetails( @PathVariable("id") Long postId, @RequestParam(defaultValue = "true", name = "sort_reply") boolean sort_reply) { ReplyOrderCondition replyOrderCondition = new ReplyOrderCondition(); @@ -38,4 +35,15 @@ public DefaultResponse findReplies( return postService.getPost(postId, replyOrderCondition); } + @GetMapping("/postlist") + public DefaultResponse getPostListByCondition(PostSearchCondition condition, + @PageableDefault(page = 0, sort = "createdDate", direction = Sort.Direction.DESC) Pageable pageable) { + return postService.getPostInfoListByCondition(condition, pageable); + } + + @DeleteMapping("/postlist/{id}") + public DefaultResponse deletePost(@PathVariable("id") Long postId) { + return postService.deletePost(postId); + } + } diff --git a/src/main/java/com/devin/dev/repository/post/PostLikeRepository.java b/src/main/java/com/devin/dev/repository/post/PostLikeRepository.java new file mode 100644 index 0000000..7d9fcf0 --- /dev/null +++ b/src/main/java/com/devin/dev/repository/post/PostLikeRepository.java @@ -0,0 +1,7 @@ +package com.devin.dev.repository.post; + +import com.devin.dev.entity.post.PostLike; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface PostLikeRepository extends JpaRepository { +} diff --git a/src/main/java/com/devin/dev/service/PostService.java b/src/main/java/com/devin/dev/service/PostService.java index f870795..6776cbe 100644 --- a/src/main/java/com/devin/dev/service/PostService.java +++ b/src/main/java/com/devin/dev/service/PostService.java @@ -4,13 +4,13 @@ import com.devin.dev.controller.reply.ReplyOrderCondition; import com.devin.dev.dto.post.PostDetailsDto; import com.devin.dev.dto.post.PostInfoDto; -import com.devin.dev.entity.post.Post; -import com.devin.dev.entity.post.PostImage; -import com.devin.dev.entity.post.PostTag; -import com.devin.dev.entity.post.Subject; +import com.devin.dev.entity.post.*; +import com.devin.dev.entity.reply.Reply; +import com.devin.dev.entity.reply.ReplyImage; import com.devin.dev.entity.user.User; import com.devin.dev.model.DefaultResponse; import com.devin.dev.repository.post.PostImageRepository; +import com.devin.dev.repository.post.PostLikeRepository; import com.devin.dev.repository.post.PostRepository; import com.devin.dev.repository.post.PostTagRepository; import com.devin.dev.repository.reply.ReplyRepository; @@ -30,6 +30,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; @Service @RequiredArgsConstructor @@ -38,6 +39,7 @@ public class PostService { private final UserRepository userRepository; private final PostRepository postRepository; private final PostImageRepository postImageRepository; + private final PostLikeRepository postLikeRepository; private final PostTagRepository postTagRepository; private final SubjectRepository subjectRepository; private final ReplyRepository replyRepository; @@ -148,4 +150,22 @@ public DefaultResponse> getPostInfoList(Long id, Pageable page PageImpl postInfoDtosImpl = new PageImpl<>(postInfoDtos.toList(), pageable, postInfoDtos.getTotalElements()); return new DefaultResponse<>(StatusCode.OK, ResponseMessage.FOUND_POST, postInfoDtosImpl); } + + public DefaultResponse deletePost(Long postId) { + Optional postOptional = postRepository.findById(postId); + if (postOptional.isEmpty()) { + return new DefaultResponse<>(StatusCode.BAD_REQUEST, ResponseMessage.NOT_FOUND_POST); + } + Post post = postOptional.get(); + + postImageRepository.deleteAll(post.getImages()); + postTagRepository.deleteAll(post.getTags()); + postLikeRepository.deleteAll(post.getPostLikes()); + replyImageRepository.deleteAll(post.getReplies().stream().flatMap(reply -> reply.getImages().stream()).collect(Collectors.toList())); + replyLikeRepository.deleteAll(post.getReplies().stream().flatMap(reply -> reply.getLikes().stream()).collect(Collectors.toList())); + replyRepository.deleteAll(post.getReplies()); + postRepository.delete(post); + + return new DefaultResponse<>(StatusCode.OK, ResponseMessage.DELETED_POST); + } } diff --git a/src/main/java/com/devin/dev/utils/ResponseMessage.java b/src/main/java/com/devin/dev/utils/ResponseMessage.java index d55133c..024bb69 100644 --- a/src/main/java/com/devin/dev/utils/ResponseMessage.java +++ b/src/main/java/com/devin/dev/utils/ResponseMessage.java @@ -20,6 +20,7 @@ public enum ResponseMessage { FOUND_POST("게시글 검색 성공"), POST_UPLOAD_SUCCESS("게시글 등록 성공"), POST_EDIT_SUCCESS("게시글 수정 성공"), + DELETED_POST("게시글 삭제 성공"), REPLY_UPLOAD_SUCCESS("답변 등록 성공"), REPLY_EDIT_SUCCESS("답변 수정 성공"), REPLY_DELETE_SUCCESS("답변 삭제 성공"),