Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jericho #53

Merged
merged 1 commit into from
Nov 25, 2023
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
2 changes: 1 addition & 1 deletion src/main/java/ne/ordinary/dd/domain/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class Comment extends BaseTime {
private boolean isRemoved = false;


@OneToMany(fetch = FetchType.EAGER, mappedBy = "parent")
@OneToMany(fetch = FetchType.EAGER, mappedBy = "parent", orphanRemoval = true, cascade = CascadeType.REMOVE)
private List<Comment> childList = new ArrayList<>();


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ne.ordinary.dd.domain.Comment;
import ne.ordinary.dd.domain.CommentLike;
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 org.springframework.stereotype.Repository;
Expand Down Expand Up @@ -34,6 +35,7 @@ public interface CommentLikeRepository extends JpaRepository<CommentLike, Long>
"where cl.comment.commentId = :commentId and cl.user.id = :userId")
int countByCommentIdAndUserId(@Param("commentId") Long commentId, @Param("userId") Long userId);

@Modifying
@Query("delete from CommentLike cl " +
"where cl.comment.feed.id = :feedId")
void deleteByFeedId(@Param("feedId") Long id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ne.ordinary.dd.domain.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 org.springframework.stereotype.Repository;
Expand All @@ -26,4 +27,9 @@ public interface CommentRepository extends JpaRepository<Comment, Long> {
"from Comment c " +
"where c.parent.commentId = :parentId")
List<Comment> findByParentId(@Param("parentId") Long parentId);

@Modifying
@Query("delete from Comment c " +
"where c.feed.id = :feedId")
void deleteByFeedId(@Param("feedId") Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ne.ordinary.dd.domain.FeedLike;
import ne.ordinary.dd.domain.User;
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 org.springframework.stereotype.Repository;
Expand All @@ -22,6 +23,7 @@ public interface FeedLikeRepository extends JpaRepository<FeedLike, Long> {

Optional<FeedLike> findByFeedAndUser(Feed feed, User user);

@Modifying
@Query("delete from FeedLike fl " +
"where fl.feed.id = :feedId")
void deleteByFeedId(@Param("feedId") Long id);
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/ne/ordinary/dd/service/FeedService.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class FeedService {
private final CommentRepository commentRepository;
private final CommentLikeRepository commentLikeRepository;
private final FeedLikeRepository feedLikeRepository;
private final CommentService commentService;

public FeedResponse.FeedDTO getFeed(Long id) {
Feed feedPS = feedsRepository.findById(id).orElseThrow(
Expand Down Expand Up @@ -158,10 +159,14 @@ public void deleteFeed(Long id, FeedRequest.DeleteDTO deleteDTO) {

try {
commentLikeRepository.deleteByFeedId(feedPS.getId());
commentRepository.deleteById(feedPS.getId());
List<Comment> comments = commentRepository.findByFeedId(feedPS.getId());
for (Comment c : comments) {
commentService.deleteComment(c.getCommentId());
}
feedLikeRepository.deleteByFeedId(feedPS.getId());
feedsRepository.deleteById(feedPS.getId());
} catch (Exception e) {
e.printStackTrace();
throw new Exception500("피드 삭제가 실패했습니다.");
}
}
Expand Down