diff --git a/src/main/java/kr/tgwing/tech/blog/repository/LikeHistoryRepository.java b/src/main/java/kr/tgwing/tech/blog/repository/LikeHistoryRepository.java index 18f7eae..0ade879 100644 --- a/src/main/java/kr/tgwing/tech/blog/repository/LikeHistoryRepository.java +++ b/src/main/java/kr/tgwing/tech/blog/repository/LikeHistoryRepository.java @@ -3,10 +3,12 @@ import org.springframework.data.jpa.repository.JpaRepository; import kr.tgwing.tech.blog.entity.LikeHistory; +import kr.tgwing.tech.blog.entity.Post; /** * LikeHistoryRepository */ public interface LikeHistoryRepository extends JpaRepository { + void deleteAllByPost(Post post); } diff --git a/src/main/java/kr/tgwing/tech/blog/service/PostServiceImpl.java b/src/main/java/kr/tgwing/tech/blog/service/PostServiceImpl.java index 35b79cc..45b7ee3 100644 --- a/src/main/java/kr/tgwing/tech/blog/service/PostServiceImpl.java +++ b/src/main/java/kr/tgwing/tech/blog/service/PostServiceImpl.java @@ -52,6 +52,7 @@ public class PostServiceImpl implements PostService { private final LikeHistoryRepository likeHistoryRepository; @Override + @Transactional(readOnly = true) public PostDetail getPost(Long postId, String userStudentNumber) { User user = getUserEntity(userStudentNumber); Post post = getPostEntity(postId); @@ -119,12 +120,15 @@ public void deletePost(Long postId, String writerStudentNumber) { // 해당 URL을 요청한 사람이 공지 작성자인 경우에만 삭제 가능 if(post.getWriter().equals(postWriter)) { + likeHistoryRepository.deleteAllByPost(post); postRepository.deleteById(postId); } else { - throw new UserIsNotPostWriterException(); } + throw new UserIsNotPostWriterException(); + } } @Override + @Transactional(readOnly = true) public Page getPostOverviews(PostQuery query, String userStudentNumber, Pageable pageable) { User user = getUserEntity(userStudentNumber); Specification spec = PostSpecifications.hasTitleLike(query.getKeyword()) @@ -147,6 +151,7 @@ public Page getPostOverviews(PostQuery query, String userStudentNu } @Override + @Transactional(readOnly = true) public PostOverview getPostOverview(Long postId, String userStudentNumber) { Post post = getPostEntity(postId); User user = getUserEntity(userStudentNumber); @@ -201,6 +206,7 @@ public void deleteComment(Long postId, Long commentId, String writerStudentNumbe } @Override + @Transactional(readOnly = true) public Page getComments(Long postId, Pageable pageable) { Post post = postRepository.findById(postId) .orElseThrow(PostNotFoundException::new); @@ -260,6 +266,7 @@ public void deleteReply(Long postId, Long commentId, Long replyId, String writer } @Override + @Transactional(readOnly = true) public Page getReplies(Long postId, Long commentId, Pageable pageable) { Comment comment = commentRepository.findById(commentId) .orElseThrow(CommentNotFoundException::new);