Skip to content

Commit

Permalink
ApplicationListエンティティ削除
Browse files Browse the repository at this point in the history
  • Loading branch information
eve00 committed Dec 22, 2023
1 parent f0ad38c commit 562751c
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 102 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package data.repository

import domain.entity.*

interface CourseTakingApplicationsRepository {

suspend fun findByStudentId(studentId: StudentId): List<CourseTakingApplication>
suspend fun findByCourseId(courseId: CourseId): List<CourseTakingApplication>
suspend fun findByCourseTakingApplicationId(courseTakingApplicationId: CourseTakingApplicationId): CourseTakingApplication
suspend fun save(courseTakingApplication: CourseTakingApplication)
suspend fun delete(courseTakingApplication: CourseTakingApplication)

}
27 changes: 15 additions & 12 deletions src/main/kotlin/domain/entity/CourseTakingApplication.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package domain.entity

import domain.entity.common.Identifier
import java.util.UUID

typealias CourseTakingApplicationId = Identifier<CourseTakingApplication, String>

class CourseTakingApplication(
private val id: CourseTakingApplicationId,
private val studentId: StudentId,
private val courseId: CourseId,
status: Status
private val state: State
) {
private var _status = status
private var _state = state

fun getId(): CourseTakingApplicationId {
return id
Expand All @@ -20,27 +19,31 @@ class CourseTakingApplication(
fun getStudentId(): StudentId {
return studentId
}

fun getCourseId(): CourseId {
return courseId
}

fun getStatus(): Status {
return _status
fun getState(): State {
return _state
}

/*登録される*/
fun confirm(){
_status = Status.UNCONFIRMED
/*抽選で当選する*/

fun confirm() {
if (_state == State.UNCONFIRMED)
_state = State.CONFIRMED
}

/*抽選で落選する*/
fun invalidate(){
_status = Status.INVALIDATED
fun invalidate() {
if (_state == State.UNCONFIRMED)
_state = State.INVALIDATED
}


}

enum class Status {
UNCONFIRMED, CONFIRMED, INVALIDATED
enum class State {
CREATED, UNCONFIRMED, CONFIRMED, INVALIDATED
}
24 changes: 0 additions & 24 deletions src/main/kotlin/domain/entity/CourseTakingApplicationList.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package domain.service

import domain.entity.*
import org.http4k.core.Request

interface CourseTakingApplicationService {
suspend fun applyCourseTaking(courseTakingApplicationId: CourseTakingApplicationId, studentId:StudentId, courseId: CourseId)
suspend fun cancelCourseTaking(studentId:StudentId,courseTakingApplicationId: CourseTakingApplicationId)
suspend fun cancelCourseTaking(courseTakingApplicationId: CourseTakingApplicationId)
suspend fun applyCourseTakingBasedOnFirstserved(courseTakingApplicationId: CourseTakingApplicationId, studentId:StudentId, courseId: CourseId)
suspend fun getCourseTakingApplicationList(studentId: StudentId): CourseTakingApplicationList

suspend fun getCourseTakingApplications(studentId: StudentId): List<CourseTakingApplication>
}
Original file line number Diff line number Diff line change
@@ -1,41 +1,55 @@
package domain.service.impl

import data.repository.CourseMembersRepository
import data.repository.CourseTakingApplicationListsRepository
import data.repository.CourseTakingApplicationsRepository
import data.repository.StudentsRepository
import domain.entity.CourseId
import domain.entity.CourseTakingApplication
import domain.entity.Student
import domain.entity.StudentId
import domain.entity.State
import domain.service.CourseRegistrationService
import domain.service.CourseService
import domain.service.CourseTakingApplicationService

class CourseRegistrationServiceImpl(
private val courseService: CourseService,
private val courseTakingApplicationListsRepository: CourseTakingApplicationListsRepository,
private val courseTakingApplicationService: CourseTakingApplicationService,
private val courseTakingApplicationsRepository: CourseTakingApplicationsRepository,
private val courseMembersRepository: CourseMembersRepository,
private val studentsRepository: StudentsRepository
) : CourseRegistrationService {


override suspend fun drawingAndRegisterMembers(courseId: CourseId) {
/*抽選*/
val max = courseService.getCourse(courseId).getMax()
val drawedCourseTakingApplications = getCourseTakingApplications(courseId).shuffled()
.subList(0, max - 1)
drawedCourseTakingApplications.forEach {
it.confirm()
}

val invalidatedCourseTakingApplications = getCourseTakingApplications(courseId).filter {
it.getState() == State.UNCONFIRMED
}
invalidatedCourseTakingApplications.forEach {
it.invalidate()
}

drawedCourseTakingApplications.forEach { courseTakingApplicationsRepository.save(it) }
invalidatedCourseTakingApplications.forEach { courseTakingApplicationsRepository.save(it) }

/*登録*/
val memberIds =
courseTakingApplicationListsRepository.findByCourseId(courseId).getCourseTakingApplicationList().shuffled()
.subList(0, max - 1).map { courseTakingApplication ->
courseTakingApplication.getStudentId()
}
drawedCourseTakingApplications.map { courseTakingApplication ->
courseTakingApplication.getStudentId()
}
val members = studentsRepository.findAll().filter { student ->
memberIds.contains(student.getId())
}

courseMembersRepository.save(courseId, members)
/*TODO: キャンセル処理*/
}

override suspend fun registerMembers(courseId: CourseId) {
val memberIds =
courseTakingApplicationListsRepository.findByCourseId(courseId).getCourseTakingApplicationList()
getCourseTakingApplications(courseId)
.map { courseTakingApplication ->
courseTakingApplication.getStudentId()
}
Expand All @@ -45,4 +59,8 @@ class CourseRegistrationServiceImpl(

courseMembersRepository.save(courseId, members)
}

private suspend fun getCourseTakingApplications(courseId: CourseId): List<CourseTakingApplication> {
return courseTakingApplicationsRepository.findByCourseId(courseId)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package domain.service.impl

import data.repository.CourseTakingApplicationListsRepository
import domain.entity.CourseId
import domain.entity.CourseTakingApplicationId
import domain.entity.CourseTakingApplicationList
import domain.entity.StudentId
import data.repository.CourseTakingApplicationsRepository
import domain.entity.*
import domain.service.CourseTakingApplicationService
import domain.service.FirstServedManagementService

Expand All @@ -13,7 +10,7 @@ import domain.service.FirstServedManagementService
*
* */
class CourseTakingApplicationServiceImpl(
val repository: CourseTakingApplicationListsRepository,
val repository: CourseTakingApplicationsRepository,
val firstServedManagementService: FirstServedManagementService
) : CourseTakingApplicationService {

Expand All @@ -22,31 +19,28 @@ class CourseTakingApplicationServiceImpl(
studentId: StudentId,
courseId: CourseId
) {
val courseTakingApplicationList = getCourseTakingApplicationList(studentId)
courseTakingApplicationList.createCourseTakingApplication(courseTakingApplicationId,studentId, courseId)
repository.save(courseTakingApplicationList)
val newCourseTakingApplication =
CourseTakingApplication(courseTakingApplicationId, studentId, courseId, State.UNCONFIRMED)
repository.save(newCourseTakingApplication)
}

override suspend fun cancelCourseTaking(
studentId: StudentId,
courseTakingApplicationId: CourseTakingApplicationId
) {
val courseTakingApplicationList = getCourseTakingApplicationList(studentId)
courseTakingApplicationList.deleteCourseTakingApplication(courseTakingApplicationId)
repository.save(courseTakingApplicationList)
val canceledCourseTakingApplication = repository.findByCourseTakingApplicationId(courseTakingApplicationId)
repository.delete(canceledCourseTakingApplication)
}

override suspend fun applyCourseTakingBasedOnFirstserved(
courseTakingApplicationId: CourseTakingApplicationId,
studentId: StudentId,
courseId: CourseId
) {
if (firstServedManagementService.checkCanTake(courseId)) {
if (firstServedManagementService.checkCanTake(courseId))
applyCourseTaking(courseTakingApplicationId, studentId, courseId)
}
}

override suspend fun getCourseTakingApplicationList(studentId: StudentId): CourseTakingApplicationList {
override suspend fun getCourseTakingApplications(studentId: StudentId): List<CourseTakingApplication> {
return repository.findByStudentId(studentId)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package domain.service.impl

import data.repository.CourseTakingApplicationListsRepository
import data.repository.CourseTakingApplicationsRepository
import domain.entity.Course
import domain.entity.CourseId
import domain.service.CourseService
import domain.service.FirstServedManagementService

class FirstServedManagementServiceImpl(
val repository: CourseTakingApplicationListsRepository,
val repository: CourseTakingApplicationsRepository,
val courseService: CourseService
) : FirstServedManagementService {
override suspend fun checkCanTake(courseId: CourseId): Boolean {
return repository.findByCourseId(courseId).getSize() < courseService.getCourse(courseId).getMax()
return repository.findByCourseId(courseId).size < courseService.getCourse(courseId).getMax()
}

override suspend fun getCoursesCanTake(): List<Course> {
return courseService.getCourses()
.filter { course ->
repository.findByCourseId(course.getId()).getSize() < course.getMax()
repository.findByCourseId(course.getId()).size < course.getMax()
}
}

Expand Down
Loading

0 comments on commit 562751c

Please sign in to comment.