diff --git a/pick-application/src/main/kotlin/com/pickdsm/pickserverspring/domain/application/error/ApplicationErrorCode.kt b/pick-application/src/main/kotlin/com/pickdsm/pickserverspring/domain/application/error/ApplicationErrorCode.kt index d01c570a..cf16813b 100644 --- a/pick-application/src/main/kotlin/com/pickdsm/pickserverspring/domain/application/error/ApplicationErrorCode.kt +++ b/pick-application/src/main/kotlin/com/pickdsm/pickserverspring/domain/application/error/ApplicationErrorCode.kt @@ -7,7 +7,11 @@ enum class ApplicationErrorCode( private val message: String, ) : ErrorProperty { - APPLICATION_NOT_FOUND(404, "Application Not Found"), ; + CANNOT_APPLICATION_WEEKEND(401, "Cannot Application Weekend"), + + APPLICATION_NOT_FOUND(404, "Application Not Found"), + + ALREADY_APPLICATION_PICNIC_OR_ALREADY_PICNIC(409, "Already Application Picnic Or Already Picnic"), ; override fun status() = status override fun message() = message diff --git a/pick-application/src/main/kotlin/com/pickdsm/pickserverspring/domain/application/exception/AlreadyApplicationPicnicOrAlreadyPicnicException.kt b/pick-application/src/main/kotlin/com/pickdsm/pickserverspring/domain/application/exception/AlreadyApplicationPicnicOrAlreadyPicnicException.kt new file mode 100644 index 00000000..7ac2c737 --- /dev/null +++ b/pick-application/src/main/kotlin/com/pickdsm/pickserverspring/domain/application/exception/AlreadyApplicationPicnicOrAlreadyPicnicException.kt @@ -0,0 +1,8 @@ +package com.pickdsm.pickserverspring.domain.application.exception + +import com.pickdsm.pickserverspring.common.error.PickException +import com.pickdsm.pickserverspring.domain.application.error.ApplicationErrorCode + +object AlreadyApplicationPicnicOrAlreadyPicnicException : PickException( + ApplicationErrorCode.ALREADY_APPLICATION_PICNIC_OR_ALREADY_PICNIC, +) diff --git a/pick-application/src/main/kotlin/com/pickdsm/pickserverspring/domain/application/exception/CannotApplicationWeekendException.kt b/pick-application/src/main/kotlin/com/pickdsm/pickserverspring/domain/application/exception/CannotApplicationWeekendException.kt new file mode 100644 index 00000000..c46eebf1 --- /dev/null +++ b/pick-application/src/main/kotlin/com/pickdsm/pickserverspring/domain/application/exception/CannotApplicationWeekendException.kt @@ -0,0 +1,8 @@ +package com.pickdsm.pickserverspring.domain.application.exception + +import com.pickdsm.pickserverspring.common.error.PickException +import com.pickdsm.pickserverspring.domain.application.error.ApplicationErrorCode + +object CannotApplicationWeekendException : PickException( + ApplicationErrorCode.CANNOT_APPLICATION_WEEKEND, +) diff --git a/pick-application/src/main/kotlin/com/pickdsm/pickserverspring/domain/application/spi/QueryStatusSpi.kt b/pick-application/src/main/kotlin/com/pickdsm/pickserverspring/domain/application/spi/QueryStatusSpi.kt index 80c23328..cc88c78e 100644 --- a/pick-application/src/main/kotlin/com/pickdsm/pickserverspring/domain/application/spi/QueryStatusSpi.kt +++ b/pick-application/src/main/kotlin/com/pickdsm/pickserverspring/domain/application/spi/QueryStatusSpi.kt @@ -36,4 +36,6 @@ interface QueryStatusSpi { fun queryMovementStatusSizeByFloorAndToday(floor: Int): Int fun queryPicnicStatusSizeByToday(): Int + + fun existAwaitOrPicnicStatusByStudentId(studentId: UUID): Boolean } diff --git a/pick-application/src/main/kotlin/com/pickdsm/pickserverspring/domain/application/usecase/ApplicationUseCase.kt b/pick-application/src/main/kotlin/com/pickdsm/pickserverspring/domain/application/usecase/ApplicationUseCase.kt index a1b73d33..2f739559 100644 --- a/pick-application/src/main/kotlin/com/pickdsm/pickserverspring/domain/application/usecase/ApplicationUseCase.kt +++ b/pick-application/src/main/kotlin/com/pickdsm/pickserverspring/domain/application/usecase/ApplicationUseCase.kt @@ -17,7 +17,9 @@ import com.pickdsm.pickserverspring.domain.application.api.dto.response.QueryPic import com.pickdsm.pickserverspring.domain.application.api.dto.response.QueryPicnicStudentList import com.pickdsm.pickserverspring.domain.application.api.dto.response.QueryStudentStatusElement import com.pickdsm.pickserverspring.domain.application.api.dto.response.QueryStudentStatusList +import com.pickdsm.pickserverspring.domain.application.exception.AlreadyApplicationPicnicOrAlreadyPicnicException import com.pickdsm.pickserverspring.domain.application.exception.ApplicationNotFoundException +import com.pickdsm.pickserverspring.domain.application.exception.CannotApplicationWeekendException import com.pickdsm.pickserverspring.domain.application.exception.StatusNotFoundException import com.pickdsm.pickserverspring.domain.application.spi.CommandApplicationSpi import com.pickdsm.pickserverspring.domain.application.spi.QueryApplicationSpi @@ -34,6 +36,7 @@ import com.pickdsm.pickserverspring.domain.teacher.spi.TimeQueryTeacherSpi import com.pickdsm.pickserverspring.domain.time.exception.TimeNotFoundException import com.pickdsm.pickserverspring.domain.user.exception.UserNotFoundException import com.pickdsm.pickserverspring.domain.user.spi.UserSpi +import java.time.DayOfWeek import java.time.LocalDate import java.util.UUID @@ -55,6 +58,9 @@ class ApplicationUseCase( override fun saveApplicationToGoOut(request: DomainApplicationGoOutRequest) { val studentId = userSpi.getCurrentUserId() + checkIsExistAwaitOrPicnicStatus(studentId) + checkIsWeekends() + val status = Status( studentId = studentId, teacherId = UUID(0, 0), // TODO: 선생님 아이디 뭐로 넣을지 나중에 정하기 @@ -71,6 +77,18 @@ class ApplicationUseCase( ) } + private fun checkIsExistAwaitOrPicnicStatus(studentId: UUID) { + if (queryStatusSpi.existAwaitOrPicnicStatusByStudentId(studentId)) { + throw AlreadyApplicationPicnicOrAlreadyPicnicException + } + } + + private fun checkIsWeekends() { + if (LocalDate.now().dayOfWeek > DayOfWeek.FRIDAY) { + throw CannotApplicationWeekendException + } + } + override fun queryPicnicApplicationListByGradeAndClassNum( grade: String?, classNum: String?, diff --git a/pick-infrastructure/src/main/kotlin/com/pickdsm/pickserverspring/domain/application/persistence/adapter/StatusPersistenceAdapter.kt b/pick-infrastructure/src/main/kotlin/com/pickdsm/pickserverspring/domain/application/persistence/adapter/StatusPersistenceAdapter.kt index 65308dab..835fdfd0 100644 --- a/pick-infrastructure/src/main/kotlin/com/pickdsm/pickserverspring/domain/application/persistence/adapter/StatusPersistenceAdapter.kt +++ b/pick-infrastructure/src/main/kotlin/com/pickdsm/pickserverspring/domain/application/persistence/adapter/StatusPersistenceAdapter.kt @@ -9,6 +9,7 @@ import com.pickdsm.pickserverspring.domain.application.spi.StatusSpi import com.pickdsm.pickserverspring.domain.classroom.persistence.entity.QClassroomEntity.classroomEntity import com.pickdsm.pickserverspring.domain.classroom.persistence.entity.QClassroomMovementEntity.classroomMovementEntity import com.pickdsm.pickserverspring.global.annotation.Adapter +import com.querydsl.core.types.dsl.BooleanExpression import com.querydsl.jpa.impl.JPAQueryFactory import java.time.LocalDate import java.util.UUID @@ -188,4 +189,17 @@ class StatusPersistenceAdapter( statusEntity.type.eq(StatusType.PICNIC), ) .fetch().size + + override fun existAwaitOrPicnicStatusByStudentId(studentId: UUID): Boolean = + jpaQueryFactory + .select(statusEntity.id) + .from(statusEntity) + .where( + checkIsExistAwaitOrPicnic(), + statusEntity.studentId.eq(studentId), + ) + .fetchFirst() != null + + private fun checkIsExistAwaitOrPicnic(): BooleanExpression = + statusEntity.type.eq(StatusType.AWAIT).or(statusEntity.type.eq(StatusType.PICNIC)) }