fix post deletion cascade

This commit is contained in:
Tim
2025-07-14 17:34:15 +08:00
parent 1974b60bdd
commit e0e1dea166
4 changed files with 59 additions and 0 deletions

View File

@@ -10,4 +10,5 @@ import java.util.Optional;
public interface PostReadRepository extends JpaRepository<PostRead, Long> {
Optional<PostRead> findByUserAndPost(User user, Post post);
long countByUser(User user);
void deleteByPost(Post post);
}

View File

@@ -41,4 +41,9 @@ public class PostReadService {
.orElseThrow(() -> new IllegalArgumentException("User not found"));
return postReadRepository.countByUser(user);
}
@org.springframework.transaction.annotation.Transactional
public void deleteByPost(Post post) {
postReadRepository.deleteByPost(post);
}
}

View File

@@ -360,6 +360,7 @@ public class PostService {
reactionRepository.findByPost(post).forEach(reactionRepository::delete);
postSubscriptionRepository.findByPost(post).forEach(postSubscriptionRepository::delete);
notificationRepository.deleteAll(notificationRepository.findByPost(post));
postReadService.deleteByPost(post);
postRepository.delete(post);
}