generated from pricelees/issue-pr-template
<!-- 제목 양식 --> <!-- [이슈번호] 작업 요약 (예시: [#10] Gitea 템플릿 생성) --> ## 📝 관련 이슈 및 PR **PR과 관련된 이슈 번호** - #18 ## ✨ 작업 내용 <!-- 어떤 작업을 했는지 알려주세요! --> - 기존 자바와의 호환성을 위해 사용하던 \@Jvm.. 어노테이션 및 팩토리 메서드 제거 - 기존에 get, find, save 등으로 산재되어 있던 메서드 컨벤션 통일 - 일부 API endpoint 수정 - 테이블 이름 단수 -> 복수 수정 추가적으로 개선이 필요한 점은 있지만, 이는 기능 개선 과정에서 수정할 예정 ## 🧪 테스트 <!-- 어떤 테스트를 생각했고 진행했는지 알려주세요! --> 각 작업 마다 전체 테스트 수행 및 정상 동작 확인 ## 📚 참고 자료 및 기타 <!-- 참고한 자료, 또는 논의할 사항이 있다면 알려주세요! --> Reviewed-on: #19 Co-authored-by: pricelees <priceelees@gmail.com> Co-committed-by: pricelees <priceelees@gmail.com>
74 lines
2.8 KiB
Kotlin
74 lines
2.8 KiB
Kotlin
package roomescape.reservation.business
|
|
|
|
import org.springframework.data.repository.findByIdOrNull
|
|
import org.springframework.http.HttpStatus
|
|
import org.springframework.stereotype.Service
|
|
import org.springframework.transaction.annotation.Transactional
|
|
import roomescape.common.exception.ErrorType
|
|
import roomescape.common.exception.RoomescapeException
|
|
import roomescape.reservation.infrastructure.persistence.ReservationEntity
|
|
import roomescape.reservation.infrastructure.persistence.ReservationRepository
|
|
import roomescape.reservation.infrastructure.persistence.TimeEntity
|
|
import roomescape.reservation.infrastructure.persistence.TimeRepository
|
|
import roomescape.reservation.web.*
|
|
import java.time.LocalDate
|
|
import java.time.LocalTime
|
|
|
|
@Service
|
|
class TimeService(
|
|
private val timeRepository: TimeRepository,
|
|
private val reservationRepository: ReservationRepository
|
|
) {
|
|
@Transactional(readOnly = true)
|
|
fun findById(id: Long): TimeEntity = timeRepository.findByIdOrNull(id)
|
|
?: throw RoomescapeException(
|
|
ErrorType.TIME_NOT_FOUND,
|
|
"[timeId: $id]",
|
|
HttpStatus.BAD_REQUEST
|
|
)
|
|
|
|
@Transactional(readOnly = true)
|
|
fun findTimes(): TimeRetrieveListResponse = timeRepository.findAll().toRetrieveListResponse()
|
|
|
|
@Transactional
|
|
fun createTime(timeCreateRequest: TimeCreateRequest): TimeCreateResponse {
|
|
val startAt: LocalTime = timeCreateRequest.startAt
|
|
|
|
if (timeRepository.existsByStartAt(startAt)) {
|
|
throw RoomescapeException(
|
|
ErrorType.TIME_DUPLICATED, "[startAt: $startAt]", HttpStatus.CONFLICT
|
|
)
|
|
}
|
|
|
|
return TimeEntity(startAt = startAt)
|
|
.also { timeRepository.save(it) }
|
|
.toCreateResponse()
|
|
}
|
|
|
|
@Transactional
|
|
fun deleteTime(id: Long) {
|
|
val time: TimeEntity = findById(id)
|
|
reservationRepository.findByTime(time)
|
|
.also {
|
|
if (it.isNotEmpty()) {
|
|
throw RoomescapeException(
|
|
ErrorType.TIME_IS_USED_CONFLICT, "[timeId: $id]", HttpStatus.CONFLICT
|
|
)
|
|
}
|
|
timeRepository.deleteById(id)
|
|
}
|
|
}
|
|
|
|
@Transactional(readOnly = true)
|
|
fun findTimesWithAvailability(date: LocalDate, themeId: Long): TimeWithAvailabilityListResponse {
|
|
val allTimes = timeRepository.findAll()
|
|
val reservations: List<ReservationEntity> = reservationRepository.findByDateAndThemeId(date, themeId)
|
|
|
|
return TimeWithAvailabilityListResponse(allTimes.map { time ->
|
|
val isAvailable: Boolean = reservations.none { reservation -> reservation.time.id == time.id }
|
|
|
|
TimeWithAvailabilityResponse(time.id!!, time.startAt, isAvailable)
|
|
})
|
|
}
|
|
}
|