Skip to content

Commit

Permalink
feat: rating 도메인 모델 만들기
Browse files Browse the repository at this point in the history
  • Loading branch information
HyungJu committed Jan 30, 2024
1 parent ebbbe1c commit a84691d
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ enum class ErrorCode(
CAFE_NOT_FOUND("CE0001", "카페를 찾을 수 없습니다."),

CAFE_ROOM_NOT_FOUND("RE0001", "카페 방을 찾을 수 없습니다."),

INVALID_RATING_VALUE("RA0001", "평점 값이 올바르지 않습니다.")
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package kr.nagaza.nagazaserver.domain.model

import kr.nagaza.nagazaserver.domain.model.rating.Rating
import java.util.*

data class CafeRoomReview(
val reviewId: String,
val roomId: String,
val userId: String,
val content: String,
val detail: CafeRoomReviewDetail,
val rating: Rating,
val createdAt: Date,
)

data class CafeRoomReviewDetail(
val reviewId: String,
val userCnt: Int?,
val isCleared: Boolean?,
val isLifeTheme: Boolean?,
val usedHintCnt: Int?,
val difficultyPoint: Int?,
val activityPoint: Int?,
val interiorPoint: Int?,
val productionPoint: Int?,
val deviceRatio: Double?,
val userCnt: Int,
val isCleared: Boolean,
val usedHintCnt: Int,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package kr.nagaza.nagazaserver.domain.model.rating

import kr.nagaza.nagazaserver.domain.model.rating.field.RatingField
import kr.nagaza.nagazaserver.domain.model.rating.field.RatingFieldType
import kr.nagaza.nagazaserver.domain.model.rating.field.getConstraint

class Rating(
val fields: Map<RatingFieldType, RatingField>,
) {
companion object {
fun builder(): RatingBuilder {
return RatingBuilder()
}
}

val sum: Double
get() {
return fields
.values.map { it.value * getConstraint(it.type).weight }
.sum()
.let {
if (it >= 10.0) 10.0 else it
}
}

fun getRoadType(): RoadType {
if (sum in 0.0..2.0) {
return RoadType.MUD_ROAD
} else if (sum in 2.1..4.0) {
return RoadType.DIRT_ROAD
} else if (sum in 4.1..6.0) {
return RoadType.GRASS_ROAD
} else if (sum in 6.1..8.0) {
return RoadType.FLOWER_ROAD
} else {
return RoadType.FULL_OF_FLOWER_ROAD
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package kr.nagaza.nagazaserver.domain.model.rating

import kr.nagaza.nagazaserver.domain.model.rating.field.RatingField
import kr.nagaza.nagazaserver.domain.model.rating.field.RatingFieldType

class RatingBuilder {
private val fields: MutableMap<RatingFieldType, RatingField> = mutableMapOf()

fun addRating(field: RatingField): RatingBuilder {
fields[field.type] = field
return this
}

fun build(): Rating {
if (fields.size == RatingFieldType.values().size) {
return Rating(fields)
} else {
throw Exception("Not all ratings are set")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package kr.nagaza.nagazaserver.domain.model.rating

enum class RoadType {
MUD_ROAD,
DIRT_ROAD,
GRASS_ROAD,
FLOWER_ROAD,
FULL_OF_FLOWER_ROAD,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import kr.nagaza.nagazaserver.domain.exception.DomainException
import kr.nagaza.nagazaserver.domain.exception.ErrorCode

class InvalidRatingValueException : DomainException(ErrorCode.INVALID_RATING_VALUE)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package kr.nagaza.nagazaserver.domain.model.rating.field

val constraints: Map<RatingFieldType, RatingConstraint> =
mapOf(
RatingFieldType.EXPRESSION to RatingConstraint(1, 5, 0.765),
RatingFieldType.LOCK to RatingConstraint(1, 9, 0.294),
RatingFieldType.INTERIOR to RatingConstraint(1, 5, 0.412),
RatingFieldType.ACTIVITY to RatingConstraint(1, 5, 0.235),
RatingFieldType.DIFFICULTY to RatingConstraint(1, 5, 0.059),
RatingFieldType.IS_IMPRESSED to RatingConstraint(0, 1, 1.0),
)

fun getConstraint(type: RatingFieldType): RatingConstraint {
return constraints[type]!!
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package kr.nagaza.nagazaserver.domain.model.rating.field

data class RatingConstraint(
val min: Int,
val max: Int,
val weight: Double,
) {
fun getValue(value: Int): Double {
return value * weight
}

fun isInRange(value: Int): Boolean {
return value in min..max
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package kr.nagaza.nagazaserver.domain.model.rating.field

import InvalidRatingValueException

data class RatingField(
val type: RatingFieldType,
val value: Int,
) {
init {
if (!getConstraint(type).isInRange(value)) {
throw InvalidRatingValueException()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package kr.nagaza.nagazaserver.domain.model.rating.field

enum class RatingFieldType {
EXPRESSION,
LOCK,
INTERIOR,
ACTIVITY,
DIFFICULTY,
IS_IMPRESSED,
}

0 comments on commit a84691d

Please sign in to comment.