From de941ea8da3c87a0dae96d3c9a193eb973160a43 Mon Sep 17 00:00:00 2001 From: psyeon1120 Date: Thu, 17 Aug 2023 20:04:22 +0900 Subject: [PATCH] =?UTF-8?q?#137=20feat:=20reviews=20cascade=20=EC=84=A4?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../entityListener/ProductEntityListener.kt | 2 -- .../entityListener/ReviewEntityListener.kt | 16 ++++++++++++++++ .../kotlin/com/psr/psr/review/entity/Review.kt | 2 ++ .../com/psr/psr/review/entity/ReviewReport.kt | 2 ++ .../psr/review/repository/ReviewImgRepository.kt | 1 + .../review/repository/ReviewReportRepository.kt | 1 + 6 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/com/psr/psr/global/entityListener/ReviewEntityListener.kt diff --git a/src/main/kotlin/com/psr/psr/global/entityListener/ProductEntityListener.kt b/src/main/kotlin/com/psr/psr/global/entityListener/ProductEntityListener.kt index 546ad48..a69c572 100644 --- a/src/main/kotlin/com/psr/psr/global/entityListener/ProductEntityListener.kt +++ b/src/main/kotlin/com/psr/psr/global/entityListener/ProductEntityListener.kt @@ -5,8 +5,6 @@ import com.psr.psr.product.repository.ProductImgRepository import com.psr.psr.product.repository.ProductLikeRepository import jakarta.persistence.PreRemove - - class ProductEntityListener { @PreRemove fun onUpdate(product: Product) { diff --git a/src/main/kotlin/com/psr/psr/global/entityListener/ReviewEntityListener.kt b/src/main/kotlin/com/psr/psr/global/entityListener/ReviewEntityListener.kt new file mode 100644 index 0000000..216a7f6 --- /dev/null +++ b/src/main/kotlin/com/psr/psr/global/entityListener/ReviewEntityListener.kt @@ -0,0 +1,16 @@ +package com.psr.psr.global.entityListener + +import com.psr.psr.review.entity.Review +import com.psr.psr.review.repository.ReviewImgRepository +import com.psr.psr.review.repository.ReviewReportRepository +import jakarta.persistence.PreRemove + +class ReviewEntityListener { + @PreRemove + fun onUpdate(review: Review) { + val reviewImgRepository: ReviewImgRepository = BeanUtils.getBean(ReviewImgRepository::class.java) + reviewImgRepository.deleteByReview(review) + val reviewReportRepository: ReviewReportRepository = BeanUtils.getBean(ReviewReportRepository::class.java) + reviewReportRepository.deleteByReview(review) + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/psr/psr/review/entity/Review.kt b/src/main/kotlin/com/psr/psr/review/entity/Review.kt index cf18ae0..0a3dd71 100644 --- a/src/main/kotlin/com/psr/psr/review/entity/Review.kt +++ b/src/main/kotlin/com/psr/psr/review/entity/Review.kt @@ -1,6 +1,7 @@ package com.psr.psr.review.entity import com.psr.psr.global.entity.BaseEntity +import com.psr.psr.global.entityListener.ReviewEntityListener import com.psr.psr.order.entity.Order import com.psr.psr.product.entity.Product import com.psr.psr.review.dto.ReviewReq @@ -10,6 +11,7 @@ import org.hibernate.annotations.Where import org.jetbrains.annotations.NotNull @Entity +@EntityListeners(ReviewEntityListener::class) @SQLDelete(sql = "UPDATE review SET status = 'inactive', updated_at = current_timestamp WHERE id = ?") data class Review( @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/src/main/kotlin/com/psr/psr/review/entity/ReviewReport.kt b/src/main/kotlin/com/psr/psr/review/entity/ReviewReport.kt index 01c09f6..bca24c3 100644 --- a/src/main/kotlin/com/psr/psr/review/entity/ReviewReport.kt +++ b/src/main/kotlin/com/psr/psr/review/entity/ReviewReport.kt @@ -4,9 +4,11 @@ import com.psr.psr.global.entity.BaseEntity import com.psr.psr.global.entity.ReportCategory import com.psr.psr.user.entity.User import jakarta.persistence.* +import org.hibernate.annotations.SQLDelete import org.jetbrains.annotations.NotNull @Entity +@SQLDelete(sql = "UPDATE review_report SET status = 'inactive', updated_at = current_timestamp WHERE id = ?") data class ReviewReport( @Id @GeneratedValue(strategy = GenerationType.IDENTITY) var id: Long? = null, diff --git a/src/main/kotlin/com/psr/psr/review/repository/ReviewImgRepository.kt b/src/main/kotlin/com/psr/psr/review/repository/ReviewImgRepository.kt index af09d84..68446ad 100644 --- a/src/main/kotlin/com/psr/psr/review/repository/ReviewImgRepository.kt +++ b/src/main/kotlin/com/psr/psr/review/repository/ReviewImgRepository.kt @@ -7,4 +7,5 @@ import org.springframework.stereotype.Repository @Repository interface ReviewImgRepository: JpaRepository { + fun deleteByReview(review: Review) } \ No newline at end of file diff --git a/src/main/kotlin/com/psr/psr/review/repository/ReviewReportRepository.kt b/src/main/kotlin/com/psr/psr/review/repository/ReviewReportRepository.kt index c8acce8..e0a6790 100644 --- a/src/main/kotlin/com/psr/psr/review/repository/ReviewReportRepository.kt +++ b/src/main/kotlin/com/psr/psr/review/repository/ReviewReportRepository.kt @@ -9,4 +9,5 @@ import org.springframework.stereotype.Repository @Repository interface ReviewReportRepository: JpaRepository { fun findByReviewAndUserAndStatus(review: Review, user: User, status: String): ReviewReport? + fun deleteByReview(review: Review) } \ No newline at end of file