Skip to content

Commit

Permalink
[J2KB-3rd-Season#14]feat: 게시글 삭제 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
enjay27 committed Jun 11, 2021
1 parent 0ecb5a7 commit 1867da2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
18 changes: 13 additions & 5 deletions src/main/java/com/devin/dev/controller/post/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,7 +23,7 @@ public DefaultResponse<?> getPostList(@RequestParam(defaultValue = "-1")Long id,
}

@GetMapping("/postlist/{id}")
public DefaultResponse<PostDetailsDto> findReplies(
public DefaultResponse<PostDetailsDto> getPostDetails(
@PathVariable("id") Long postId,
@RequestParam(defaultValue = "true", name = "sort_reply") boolean sort_reply) {
ReplyOrderCondition replyOrderCondition = new ReplyOrderCondition();
Expand All @@ -38,4 +35,15 @@ public DefaultResponse<PostDetailsDto> 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<PostDetailsDto> deletePost(@PathVariable("id") Long postId) {
return postService.deletePost(postId);
}

}
Original file line number Diff line number Diff line change
@@ -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<PostLike, Long> {
}
28 changes: 24 additions & 4 deletions src/main/java/com/devin/dev/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,6 +30,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
Expand All @@ -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;
Expand Down Expand Up @@ -148,4 +150,22 @@ public DefaultResponse<Page<PostInfoDto>> getPostInfoList(Long id, Pageable page
PageImpl<PostInfoDto> postInfoDtosImpl = new PageImpl<>(postInfoDtos.toList(), pageable, postInfoDtos.getTotalElements());
return new DefaultResponse<>(StatusCode.OK, ResponseMessage.FOUND_POST, postInfoDtosImpl);
}

public DefaultResponse<PostDetailsDto> deletePost(Long postId) {
Optional<Post> 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);
}
}
1 change: 1 addition & 0 deletions src/main/java/com/devin/dev/utils/ResponseMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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("답변 삭제 성공"),
Expand Down

0 comments on commit 1867da2

Please sign in to comment.