generated from pricelees/issue-pr-template
77 lines
2.7 KiB
Kotlin
77 lines
2.7 KiB
Kotlin
package roomescape.reservation.infrastructure.persistence
|
|
|
|
import org.springframework.data.jpa.domain.Specification
|
|
import roomescape.member.infrastructure.persistence.MemberEntity
|
|
import roomescape.theme.infrastructure.persistence.ThemeEntity
|
|
import roomescape.time.infrastructure.persistence.TimeEntity
|
|
import java.time.LocalDate
|
|
|
|
class ReservationSearchSpecification(
|
|
private var spec: Specification<ReservationEntity> = Specification { _, _, _ -> null }
|
|
) {
|
|
fun sameThemeId(themeId: Long?): ReservationSearchSpecification = andIfNotNull(themeId?.let {
|
|
Specification { root, _, cb ->
|
|
cb.equal(root.get<ThemeEntity>("theme").get<Long>("id"), themeId)
|
|
}
|
|
})
|
|
|
|
fun sameMemberId(memberId: Long?): ReservationSearchSpecification = andIfNotNull(memberId?.let {
|
|
Specification { root, _, cb ->
|
|
cb.equal(root.get<MemberEntity>("member").get<Long>("id"), memberId)
|
|
}
|
|
})
|
|
|
|
fun sameTimeId(timeId: Long?): ReservationSearchSpecification = andIfNotNull(timeId?.let {
|
|
Specification { root, _, cb ->
|
|
cb.equal(root.get<TimeEntity>("time").get<Long>("id"), timeId)
|
|
}
|
|
})
|
|
|
|
fun sameDate(date: LocalDate?): ReservationSearchSpecification = andIfNotNull(date?.let {
|
|
Specification { root, _, cb ->
|
|
cb.equal(root.get<LocalDate>("date"), date)
|
|
}
|
|
})
|
|
|
|
fun confirmed(): ReservationSearchSpecification = andIfNotNull { root, _, cb ->
|
|
cb.or(
|
|
cb.equal(
|
|
root.get<ReservationStatus>("reservationStatus"),
|
|
ReservationStatus.CONFIRMED
|
|
),
|
|
cb.equal(
|
|
root.get<ReservationStatus>("reservationStatus"),
|
|
ReservationStatus.CONFIRMED_PAYMENT_REQUIRED
|
|
)
|
|
)
|
|
}
|
|
|
|
fun waiting(): ReservationSearchSpecification = andIfNotNull { root, _, cb ->
|
|
cb.equal(
|
|
root.get<ReservationStatus>("reservationStatus"),
|
|
ReservationStatus.WAITING
|
|
)
|
|
}
|
|
|
|
fun dateStartFrom(dateFrom: LocalDate?): ReservationSearchSpecification = andIfNotNull(dateFrom?.let {
|
|
Specification { root, _, cb ->
|
|
cb.greaterThanOrEqualTo(root.get("date"), dateFrom)
|
|
}
|
|
})
|
|
|
|
fun dateEndAt(dateTo: LocalDate?): ReservationSearchSpecification = andIfNotNull(dateTo?.let {
|
|
Specification { root, _, cb ->
|
|
cb.lessThanOrEqualTo(root.get("date"), dateTo)
|
|
}
|
|
})
|
|
|
|
fun build(): Specification<ReservationEntity> {
|
|
return this.spec
|
|
}
|
|
|
|
private fun andIfNotNull(condition: Specification<ReservationEntity>?): ReservationSearchSpecification {
|
|
condition?.let { this.spec = this.spec.and(condition) }
|
|
return this
|
|
}
|
|
}
|