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

feat: 장소 리뷰 도메인 평점 계산 및 한줄평 통계 조회 기능 추가 #54

Merged
merged 4 commits into from
Dec 29, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package kr.wooco.woocobe.place.domain.gateway

import kr.wooco.woocobe.place.domain.model.PlaceOneLineReviewStat

interface PlaceOneLineReviewStorageGateway {
fun getOneLineReviewStats(placeId: Long): List<PlaceOneLineReviewStat>
}
29 changes: 19 additions & 10 deletions src/main/kotlin/kr/wooco/woocobe/place/domain/model/Place.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,29 @@ class Place(
var reviewCount: Long,
// 한줄평 통계 로직 고려 중
) {
fun increaseReviewCount() =
apply {
reviewCount++
}
fun addReview(newRating: Double) {
averageRating = ((averageRating * reviewCount) + newRating) / (reviewCount + 1)
reviewCount++
}

fun decreaseReviewCount() =
apply {
reviewCount--
fun updateReview(
oldRating: Double,
newRating: Double,
) {
if (reviewCount > 0) {
averageRating += (newRating - oldRating) / reviewCount
}
}

fun updateAverageRating(rating: Double) =
apply {
averageRating = (averageRating * reviewCount + rating) / (reviewCount)
fun deleteReview(oldRating: Double) {
if (reviewCount > 1) {
averageRating = ((averageRating * reviewCount) - oldRating) / (reviewCount - 1)
reviewCount--
} else {
averageRating = 0.0
reviewCount = 0
}
}

companion object {
fun register(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package kr.wooco.woocobe.place.domain.model

class PlaceOneLineReviewStat(
val content: String,
val count: Long,
)
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class AddPlaceReviewUseCase(
oneLineReview = input.oneLineReviews,
imageUrls = input.imageUrls,
).also(placeReviewStorageGateway::save)
place.increaseReviewCount()

place.addReview(input.rating)
placeStorageGateway.save(place)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ class DeletePlaceReviewUseCase(

placeReviewStorageGateway.deleteByPlaceReviewId(placeReviewId = placeReview.id)

placeReview.place.decreaseReviewCount()
val place = placeReview.place

placeStorageGateway.save(placeReview.place)
place.deleteReview(oldRating = placeReview.rating)

placeStorageGateway.save(place)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package kr.wooco.woocobe.place.domain.usecase

import kr.wooco.woocobe.common.domain.usecase.UseCase
import kr.wooco.woocobe.place.domain.gateway.PlaceOneLineReviewStorageGateway
import kr.wooco.woocobe.place.domain.model.PlaceOneLineReviewStat

data class GetOneLineReviewStatsInput(
val placeId: Long,
)

data class GetOneLineReviewStatsOutput(
val placeOneLineReviewRank: List<PlaceOneLineReviewStat>,
)

class GetOneLineReviewStatsUseCase(
private val placeOneLineReviewStorageGateway: PlaceOneLineReviewStorageGateway,
) : UseCase<GetOneLineReviewStatsInput, GetOneLineReviewStatsOutput> {
override fun execute(input: GetOneLineReviewStatsInput): GetOneLineReviewStatsOutput {
val placeOneLineReviewRank =
placeOneLineReviewStorageGateway.getOneLineReviewStats(input.placeId)

return GetOneLineReviewStatsOutput(
placeOneLineReviewRank = placeOneLineReviewRank,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kr.wooco.woocobe.place.domain.usecase

import kr.wooco.woocobe.common.domain.usecase.UseCase
import kr.wooco.woocobe.place.domain.gateway.PlaceReviewStorageGateway
import kr.wooco.woocobe.place.domain.gateway.PlaceStorageGateway
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

Expand All @@ -17,6 +18,7 @@ data class UpdatePlaceReviewInput(
@Service
class UpdatePlaceReviewUseCase(
private val placeReviewStorageGateway: PlaceReviewStorageGateway,
private val placeStorageGateway: PlaceStorageGateway,
) : UseCase<UpdatePlaceReviewInput, Unit> {
@Transactional
override fun execute(input: UpdatePlaceReviewInput) {
Expand All @@ -34,5 +36,9 @@ class UpdatePlaceReviewUseCase(
oneLineReviews = input.oneLineReviews,
imageUrls = input.imageUrls,
).also(placeReviewStorageGateway::save)

val place = placeReview.place
place.updateReview(oldRating = placeReview.rating, newRating = input.rating)
placeStorageGateway.save(place)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package kr.wooco.woocobe.place.infrastructure.gateway

import kr.wooco.woocobe.place.domain.gateway.PlaceOneLineReviewStorageGateway
import kr.wooco.woocobe.place.domain.model.PlaceOneLineReviewStat
import kr.wooco.woocobe.place.infrastructure.storage.PlaceOneLineReviewJpaRepository
import org.springframework.stereotype.Component

@Component
class JpaPlaceOneLineReviewStorageGateway(
private val placeOneLineReviewRepository: PlaceOneLineReviewJpaRepository,
) : PlaceOneLineReviewStorageGateway {
override fun getOneLineReviewStats(placeId: Long): List<PlaceOneLineReviewStat> {
val stats = placeOneLineReviewRepository.findPlaceOneLineReviewStatsByPlaceId(placeId)
return stats.map { row ->
val content = row["content"] ?: throw RuntimeException()
val count = row["count"] ?: throw RuntimeException()

PlaceOneLineReviewStat(
content = content.toString(),
count = count,
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class JpaPlaceReviewStorageGateway(
placeReview.oneLineReviews
.map {
PlaceOneLineReviewEntity.of(
placeId = placeReviewEntity.placeId,
placeReviewId = placeReviewEntity.id!!,
content = it.content,
)
Expand Down Expand Up @@ -55,7 +56,9 @@ class JpaPlaceReviewStorageGateway(
map { placeReviewEntity ->
placeReviewEntity.toDomain(
user = userEntities.find { placeReviewEntity.userId == it.id }!!.toDomain(),
place = placeEntities.find { placeReviewEntity.placeId == it.id }!!.toDomain(),
place = placeEntities
.find { placeReviewEntity.placeId == it.id }!!
.toDomain(),
placeOneLineReview = placeOneLineReviewEntities
.filter { placeReviewEntity.id == it.placeReviewId }
.map { it.toDomain() },
Expand All @@ -74,7 +77,9 @@ class JpaPlaceReviewStorageGateway(
map { placeReviewEntity ->
placeReviewEntity.toDomain(
user = userEntity.find { placeReviewEntity.userId == it.id }!!.toDomain(),
place = placeJpaRepository.findByIdOrNull(placeReviewEntity.placeId)!!.toDomain(),
place = placeJpaRepository
.findByIdOrNull(placeReviewEntity.placeId)!!
.toDomain(),
placeOneLineReview = placeOneLineReviewEntity
.filter { placeReviewEntity.id == it.placeReviewId }
.map { it.toDomain() },
Expand All @@ -93,7 +98,9 @@ class JpaPlaceReviewStorageGateway(
map { placeReviewEntity ->
placeReviewEntity.toDomain(
user = userEntity.toDomain(),
place = placeJpaRepository.findByIdOrNull(placeReviewEntity.placeId)!!.toDomain(),
place = placeJpaRepository
.findByIdOrNull(placeReviewEntity.placeId)!!
.toDomain(),
placeOneLineReview = placeOneLineReviewEntity
.filter { placeReviewEntity.id == it.placeReviewId }
.map { it.toDomain() },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import kr.wooco.woocobe.place.domain.model.PlaceOneLineReview
class PlaceOneLineReviewEntity(
@Column(name = "content")
val content: String,
@Column(name = "place_id")
@Column(name = "place_Review_id")
val placeReviewId: Long,
@Column(name = "place_id")
val placeId: Long,
@Id @Tsid
@Column(name = "place_one_line_review_id")
val id: Long? = 0L,
Expand All @@ -23,10 +25,12 @@ class PlaceOneLineReviewEntity(

companion object {
fun of(
placeId: Long,
placeReviewId: Long,
content: String,
): PlaceOneLineReviewEntity =
PlaceOneLineReviewEntity(
placeId = placeId,
placeReviewId = placeReviewId,
content = content,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
package kr.wooco.woocobe.place.infrastructure.storage

import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query

interface PlaceOneLineReviewJpaRepository : JpaRepository<PlaceOneLineReviewEntity, Long> {
fun findAllByPlaceReviewIdOrderByCreatedAt(placeReviewId: Long): List<PlaceOneLineReviewEntity>

fun findAllByPlaceReviewIdInOrderByCreatedAt(placeReviewId: List<Long>): List<PlaceOneLineReviewEntity>

@Query(
"""
select r.content as content, count(r.content) as count
from PlaceOneLineReviewEntity r
where r.placeId = :placeId
group by r.content
order by count desc
""",
)
fun findPlaceOneLineReviewStatsByPlaceId(placeId: Long): List<Map<String, Long>>
}
Loading