Skip to content

Commit

Permalink
先着管理
Browse files Browse the repository at this point in the history
  • Loading branch information
eve00 committed Jan 6, 2024
1 parent 1c47dad commit f4b8c35
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import domain.entity.CourseId

interface FirstServedManagementService {
suspend fun getCoursesCanTake(): List<Course>
suspend fun checkCanTake(courseId:CourseId):Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ import domain.entity.CourseId
import domain.service.FirstServedManagementService

class FirstServedManagementServiceImpl(
private val repository: CourseTakingApplicationsRepository,
private val courseTakingApplicationsRepository: CourseTakingApplicationsRepository,
private val coursesRepository: CoursesRepository
) : FirstServedManagementService {
override suspend fun getCoursesCanTake(): List<Course> {
return coursesRepository.findAll()
.filter { course ->
repository.findByCourseId(course.getId()).size < course.getMax()
courseTakingApplicationsRepository.findByCourseId(course.getId()).size < course.getMax()
}
}

override suspend fun checkCanTake(courseId:CourseId): Boolean {
return coursesRepository.findById(courseId).getMax() >
courseTakingApplicationsRepository.findByCourseId(courseId).size
}
}
46 changes: 27 additions & 19 deletions src/main/kotlin/webServer/Routes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import org.http4k.core.Status.Companion.OK
* */
class CourseTakingAndRegistration(
private val courseTakingApplicationService: CourseTakingApplicationService,
private val firstServedManagementServiceImpl: FirstServedManagementServiceImpl,
private val firstServedManagementService: FirstServedManagementServiceImpl,
private val courseRegistrationService: CourseRegistrationService,
private val coursesRepository: CoursesRepository
) : HttpHandler {
Expand All @@ -33,11 +33,11 @@ class CourseTakingAndRegistration(
/*先着申請可能な科目*/
"/course" bind Method.GET to ::getCoursesCanTake,
/*申請*/
"/application/createApplication" bind Method.POST to ::applyCourseTaking,
"/application/cancelApplication" bind Method.DELETE to ::cancelCourseTaking,
"{format}/application/{studentId}/{courseId}" bind Method.POST to ::applyCourseTaking,
"/application/{studentId}/{courseTakingApplicationId}" bind Method.DELETE to ::cancelCourseTaking,
/*抽選・登録*/
"/course/{courseId}/drawAndRegisterMembers" bind Method.POST to ::drawAndRegisterCourseMembers,
"/course/{courseId}/registerMembers" bind Method.POST to ::registerCourseMembers,
"/course/{courseId}" bind Method.POST to ::drawAndRegisterCourseMembers,
"/course/{courseId}" bind Method.POST to ::registerCourseMembers,
)


Expand All @@ -59,7 +59,7 @@ class CourseTakingAndRegistration(
private fun getCoursesCanTake(request: Request): Response {
val result = CoroutineScope(Dispatchers.IO).async {
runCatching {
firstServedManagementServiceImpl.getCoursesCanTake()
firstServedManagementService.getCoursesCanTake()
}
}

Expand All @@ -78,7 +78,7 @@ class CourseTakingAndRegistration(
val result = CoroutineScope(Dispatchers.IO).async {
runCatching {
/*requestからstudentIdを取得*/
val studentId: StudentId = StudentId(request.path("studentId") ?: "")
val studentId: StudentId = StudentId("someStudentId")

courseTakingApplicationService.getCourseTakingApplications(studentId)
}
Expand All @@ -99,15 +99,20 @@ class CourseTakingAndRegistration(
val result = CoroutineScope(Dispatchers.IO).async {
runCatching {
/*requestからuser, applicationを取得*/
val studentId: StudentId = StudentId(request.path("studentId") ?: "")
val courseId: CourseId = CourseId(request.path("courseId") ?: "")

/*申請*/
courseTakingApplicationService.applyCourseTaking(
CourseTakingApplicationId(UUID.randomUUID().toString()),
studentId,
courseId
)
val studentId: StudentId = StudentId("someStudentId")
val courseId: CourseId = CourseId("someCourseId")
val format: String = "first-served"
/*先着管理*/
if (firstServedManagementService.checkCanTake(courseId)) {
/*申請*/
courseTakingApplicationService.applyCourseTaking(
CourseTakingApplicationId(UUID.randomUUID().toString()),
studentId,
courseId
)
} else {
throw Exception("すでに満員です。")
}
}
}

Expand All @@ -127,10 +132,13 @@ class CourseTakingAndRegistration(
runCatching {
/*requestからapplicationIdを取得*/
val courseTakingApplicationId: CourseTakingApplicationId =
CourseTakingApplicationId(request.path("courseTakingApplicationId") ?: "")
CourseTakingApplicationId("someCourseTakingApplicationId")
val studentId: StudentId =
StudentId("someStudentId")

/*申請のキャンセル*/
courseTakingApplicationService.cancelCourseTaking(
studentId,
courseTakingApplicationId,
)
}
Expand All @@ -148,7 +156,7 @@ class CourseTakingAndRegistration(
val result = CoroutineScope(Dispatchers.IO).async {
runCatching {
/*requestからcourseIdを取得*/
val courseId: CourseId = CourseId(request.path("courseId") ?: "")
val courseId: CourseId = CourseId("someCourseId")
/*抽選する*/
courseRegistrationService.drawingAndRegisterMembers(courseId)
}
Expand All @@ -167,7 +175,7 @@ class CourseTakingAndRegistration(
val result = CoroutineScope(Dispatchers.IO).async {
runCatching {
/*requestからcourseIdを取得*/
val courseId: CourseId = CourseId(request.path("courseId") ?: "")
val courseId: CourseId = CourseId("someCourseId")
/*抽選する*/
courseRegistrationService.registerMembers(courseId)
}
Expand Down

0 comments on commit f4b8c35

Please sign in to comment.