Skip to content

Commit

Permalink
#137 feat: reviews cascade 설정
Browse files Browse the repository at this point in the history
  • Loading branch information
psyeon1120 committed Aug 17, 2023
1 parent 5670153 commit de941ea
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 2 additions & 0 deletions src/main/kotlin/com/psr/psr/review/entity/Review.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/com/psr/psr/review/entity/ReviewReport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ import org.springframework.stereotype.Repository

@Repository
interface ReviewImgRepository: JpaRepository<ReviewImg, Long> {
fun deleteByReview(review: Review)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ import org.springframework.stereotype.Repository
@Repository
interface ReviewReportRepository: JpaRepository<ReviewReport, Long> {
fun findByReviewAndUserAndStatus(review: Review, user: User, status: String): ReviewReport?
fun deleteByReview(review: Review)
}

0 comments on commit de941ea

Please sign in to comment.