-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- ApplicationにCourseを持たせる - 申請コンテキストではApplicationListを扱う - 申請、キャンセルの操作は更新後のApplicationListを返す
- Loading branch information
Showing
5 changed files
with
115 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/kotlin/domain/entity/CourseTakingApplicationList.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package domain.entity | ||
|
||
class CourseTakingApplicationList( | ||
private val id: StudentId, | ||
private var courseTakingApplications: MutableList<CourseTakingApplication>, | ||
private var credits: Int | ||
) { | ||
|
||
|
||
private val maxCredits: Int = 26 | ||
fun getCourseTakingApplicationOfId(id: CourseTakingApplicationId): CourseTakingApplication? { | ||
return courseTakingApplications.find { courseTakingApplication -> | ||
courseTakingApplication.getId() == id | ||
} | ||
} | ||
|
||
fun addCourseTakingApplication(courseTakingApplication: CourseTakingApplication) { | ||
if (checkCanAdd(courseTakingApplication)){ | ||
courseTakingApplications.add(courseTakingApplication) | ||
updateCredits() | ||
}else { | ||
throw IllegalStateException("取得可能な単位数を超過しています。") | ||
} | ||
} | ||
fun removeCourseTakingApplication(courseTakingApplicationId: CourseTakingApplicationId) { | ||
courseTakingApplications.remove(getCourseTakingApplicationOfId(courseTakingApplicationId)) | ||
updateCredits() | ||
} | ||
|
||
private fun checkCanAdd(courseTakingApplication: CourseTakingApplication): Boolean { | ||
return maxCredits >= credits + courseTakingApplication.getCourse().getCredit() | ||
} | ||
|
||
private fun updateCredits() { | ||
credits = courseTakingApplications.map { courseTakingApplication -> | ||
courseTakingApplication.getCourse().getCredit() | ||
}.reduce { acc, credit -> acc + credit } | ||
} | ||
|
||
fun getCredits(): Int { | ||
return credits | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 50 additions & 11 deletions
61
src/main/kotlin/domain/service/impl/CourseTakingApplicationServiceImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,76 @@ | ||
package domain.service.impl | ||
|
||
import data.repository.CourseTakingApplicationsRepository | ||
|
||
import data.repository.CoursesRepository | ||
import domain.entity.* | ||
import domain.service.CourseTakingApplicationService | ||
import domain.service.FirstServedManagementService | ||
|
||
/* | ||
* 履修の申請に関する機能を提供するクラス | ||
* | ||
* */ | ||
class CourseTakingApplicationServiceImpl( | ||
val repository: CourseTakingApplicationsRepository, | ||
private val courseTakingApplicationRepository: CourseTakingApplicationsRepository, | ||
private val coursesRepository: CoursesRepository | ||
) : CourseTakingApplicationService { | ||
|
||
|
||
/*申請情報の永続化*/ | ||
override suspend fun applyCourseTaking( | ||
courseTakingApplicationId: CourseTakingApplicationId, | ||
studentId: StudentId, | ||
courseId: CourseId | ||
) { | ||
val newCourseTakingApplication = | ||
CourseTakingApplication(courseTakingApplicationId, studentId, courseId, State.UNCONFIRMED) | ||
repository.save(newCourseTakingApplication) | ||
): CourseTakingApplicationList { | ||
val courseTakingApplicationList = getCourseTakingApplicationList(studentId) | ||
|
||
val newCourseTakingApplication = createCourseTakingApplication(courseTakingApplicationId,studentId,courseId) | ||
courseTakingApplicationList.addCourseTakingApplication(newCourseTakingApplication) | ||
courseTakingApplicationRepository.save(newCourseTakingApplication) | ||
|
||
return courseTakingApplicationList | ||
} | ||
|
||
override suspend fun cancelCourseTaking( | ||
studentId: StudentId, | ||
courseTakingApplicationId: CourseTakingApplicationId | ||
) { | ||
val canceledCourseTakingApplication = repository.findByCourseTakingApplicationId(courseTakingApplicationId) | ||
repository.delete(canceledCourseTakingApplication) | ||
): CourseTakingApplicationList { | ||
val courseTakingApplicationList = getCourseTakingApplicationList(studentId) | ||
|
||
courseTakingApplicationList.removeCourseTakingApplication(courseTakingApplicationId) | ||
courseTakingApplicationRepository.delete(courseTakingApplicationId) | ||
|
||
return courseTakingApplicationList | ||
} | ||
|
||
override suspend fun getCourseTakingApplications(studentId: StudentId): List<CourseTakingApplication> { | ||
return repository.findByStudentId(studentId) | ||
override suspend fun getCourseTakingApplications(studentId: StudentId): CourseTakingApplicationList { | ||
return getCourseTakingApplicationList(studentId) | ||
} | ||
|
||
/*CourseTakingApplicationListの生成*/ | ||
private suspend fun getCourseTakingApplicationList(studentId: StudentId): CourseTakingApplicationList { | ||
val courseTakingApplications = courseTakingApplicationRepository.findByStudentId(studentId) | ||
return CourseTakingApplicationList( | ||
studentId, | ||
courseTakingApplications.toMutableList(), | ||
courseTakingApplications.map { courseTakingApplication -> | ||
courseTakingApplication.getCourse().getCredit() | ||
}.reduce { acc, credit -> acc + credit } | ||
) | ||
} | ||
|
||
/*新たなCourseTakingApplicationの作成*/ | ||
private suspend fun createCourseTakingApplication( | ||
courseTakingApplicationId: CourseTakingApplicationId, | ||
studentId: StudentId, | ||
courseId: CourseId | ||
): CourseTakingApplication { | ||
return CourseTakingApplication( | ||
courseTakingApplicationId, | ||
studentId, | ||
coursesRepository.findById(courseId), | ||
State.UNCONFIRMED | ||
) | ||
} | ||
|
||
} |