-
Notifications
You must be signed in to change notification settings - Fork 1
/
Review.kt
84 lines (70 loc) · 2.23 KB
/
Review.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.restaurant.be.review.domain.entity
import com.restaurant.be.common.entity.BaseEntity
import com.restaurant.be.common.exception.InvalidLikeCountException
import com.restaurant.be.review.domain.entity.QReview.review
import com.restaurant.be.review.presentation.dto.UpdateReviewRequest
import com.restaurant.be.user.domain.entity.User
import javax.persistence.CascadeType
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.FetchType
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
import javax.persistence.JoinColumn
import javax.persistence.ManyToOne
import javax.persistence.OneToMany
import javax.persistence.Table
@Entity
@Table(name = "restaurant_reviews")
class Review(
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long? = null,
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
val user: User,
@Column(nullable = false)
val restaurantId: Long,
@Column(nullable = false)
var content: String,
@Column(nullable = false)
var rating: Double,
@Column(name = "like_count", nullable = false)
var likeCount: Long = 0,
@Column(name = "view_count", nullable = false)
var viewCount: Long = 0,
@OneToMany(cascade = [CascadeType.ALL], orphanRemoval = true)
@JoinColumn(name = "review_id")
var images: MutableList<ReviewImage> = mutableListOf()
) : BaseEntity() {
fun addImage(reviewImage: ReviewImage) {
images.add(reviewImage)
}
fun updateReview(request: UpdateReviewRequest) {
val updateRequest = request.review
this.content = updateRequest.content
this.rating = updateRequest.rating
this.images.clear()
updateRequest.imageUrls.forEach {
this.addImage(
ReviewImage(
imageUrl = it
)
)
}
}
fun incrementViewCount() {
this.viewCount++
}
fun incrementLikeCount() {
this.likeCount++
}
fun decrementLikeCount() {
if (this.likeCount == 0L) {
throw InvalidLikeCountException()
}
this.likeCount--
}
}