Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.team.cklob.gami.domain.admin.service.impl;

import com.team.cklob.gami.domain.admin.service.AdminPostService;
import com.team.cklob.gami.domain.comment.repository.CommentRepository;
import com.team.cklob.gami.domain.post.entity.Post;
import com.team.cklob.gami.domain.post.exception.NotFoundPostException;
import com.team.cklob.gami.domain.post.repository.PostImageRepository;
import com.team.cklob.gami.domain.post.repository.PostRepository;
import com.team.cklob.gami.domain.postlike.repository.PostLikeRepository;
import com.team.cklob.gami.domain.report.repository.ReportRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -14,12 +18,22 @@
public class AdminPostServiceImpl implements AdminPostService {

private final PostRepository postRepository;
private final CommentRepository commentRepository;
private final PostImageRepository postImageRepository;
private final PostLikeRepository postLikeRepository;
private final ReportRepository reportRepository;

@Override
public void deletePost(Long postId) {
Post post = postRepository.findById(postId)
.orElseThrow(NotFoundPostException::new);

commentRepository.deleteAllByPostId(postId);
postRepository.deleteAllFromLikeTableByPostId(postId);
postImageRepository.deleteAllByPostId(postId);
postLikeRepository.deleteAllByPostId(postId);
reportRepository.deleteAllByPostId(postId);

postRepository.delete(post);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

import com.team.cklob.gami.domain.comment.entity.Comment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface CommentRepository extends JpaRepository<Comment, Long> {

List<Comment> findByPostIdOrderByCreatedAtAsc(Long postId);

@Modifying(clearAutomatically = true, flushAutomatically = true)
@Query("delete from Comment c where c.post.id = :postId")
void deleteAllByPostId(@Param("postId") Long postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@

import com.team.cklob.gami.domain.post.entity.PostImage;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface PostImageRepository extends JpaRepository<PostImage, Long> {

List<PostImage> findAllByPostId(Long postId);
}

@Modifying(clearAutomatically = true, flushAutomatically = true)
@Query("delete from PostImage pi where pi.post.id = :postId")
void deleteAllByPostId(@Param("postId") Long postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ select count(c)
void decreaseLike(@Param("postId") Long postId);

List<Post> findAllByMemberId(Long memberId);

@Modifying(clearAutomatically = true, flushAutomatically = true)
@Query(value = "delete from `like` where post_id = :postId", nativeQuery = true)
void deleteAllFromLikeTableByPostId(@Param("postId") Long postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.team.cklob.gami.domain.postlike.entity.PostLike;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Optional;

Expand All @@ -10,4 +13,8 @@ public interface PostLikeRepository extends JpaRepository<PostLike, Long> {
boolean existsByMemberEmailAndPost_Id(String memberEmail, Long postId);

Optional<PostLike> findByMemberEmailAndPost_Id(String memberEmail, Long postId);

@Modifying(clearAutomatically = true, flushAutomatically = true)
@Query("delete from PostLike pl where pl.post.id = :postId")
void deleteAllByPostId(@Param("postId") Long postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.team.cklob.gami.domain.report.entity.Report;
import com.team.cklob.gami.domain.report.entity.constant.ReportType;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

Expand All @@ -22,4 +24,8 @@ boolean existsByReporterIdAndPostIdAndReportType(
JOIN FETCH r.post
""")
List<Report> findAllWithReporterAndPost();

@Modifying(clearAutomatically = true, flushAutomatically = true)
@Query("delete from Report r where r.post.id = :postId")
void deleteAllByPostId(@Param("postId") Long postId);
}