Skip to content

Commit

Permalink
仕様修正
Browse files Browse the repository at this point in the history
- ApplicationにCourseを持たせる
- 申請コンテキストではApplicationListを扱う
- 申請、キャンセルの操作は更新後のApplicationListを返す
  • Loading branch information
eve00 committed Jan 6, 2024
1 parent d96c162 commit 1c47dad
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ interface CourseTakingApplicationsRepository {
suspend fun findByCourseId(courseId: CourseId): List<CourseTakingApplication>
suspend fun findByCourseTakingApplicationId(courseTakingApplicationId: CourseTakingApplicationId): CourseTakingApplication
suspend fun save(courseTakingApplication: CourseTakingApplication)
suspend fun delete(courseTakingApplication: CourseTakingApplication)

suspend fun delete(courseTakingApplicationId: CourseTakingApplicationId)
}

class CourseTakingApplicationsRepositoryImpl(): CourseTakingApplicationsRepository {
Expand All @@ -29,7 +29,7 @@ class CourseTakingApplicationsRepositoryImpl(): CourseTakingApplicationsReposito
TODO("Not yet implemented")
}

override suspend fun delete(courseTakingApplication: CourseTakingApplication) {
override suspend fun delete(courseTakingApplicationId: CourseTakingApplicationId) {
TODO("Not yet implemented")
}

Expand Down
19 changes: 12 additions & 7 deletions src/main/kotlin/domain/entity/Course.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ typealias CourseId = Identifier<Course, String>


class Course(
val id: CourseId,
val name: String,
val term: String,
val dowAndPeriod: DowAndPeriod,
val max: Int,

private val id: CourseId,
private val name: String,
private val term: String,
private val dowAndPeriod: DowAndPeriod,
private val max: Int,
private val credit: Int
) {
private var _max = max
fun getId(): CourseId {
return id
}
Expand All @@ -30,7 +31,11 @@ class Course(
}

fun getMax(): Int {
return _max
return max
}

fun getCredit():Int {
return credit
}
}

Expand Down
44 changes: 44 additions & 0 deletions src/main/kotlin/domain/entity/CourseTakingApplicationList.kt
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
}
}

11 changes: 7 additions & 4 deletions src/main/kotlin/domain/service/CourseTakingApplicationService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package domain.service
import domain.entity.*

interface CourseTakingApplicationService {
suspend fun applyCourseTaking(courseTakingApplicationId: CourseTakingApplicationId, studentId:StudentId, courseId: CourseId)
suspend fun cancelCourseTaking(courseTakingApplicationId: CourseTakingApplicationId)
suspend fun getCourseTakingApplications(studentId: StudentId): List<CourseTakingApplication>
}

suspend fun applyCourseTaking(
courseTakingApplicationId: CourseTakingApplicationId, studentId: StudentId, courseId: CourseId
) : CourseTakingApplicationList
suspend fun cancelCourseTaking(studentId: StudentId, courseTakingApplicationId: CourseTakingApplicationId) : CourseTakingApplicationList
suspend fun getCourseTakingApplications(studentId: StudentId): CourseTakingApplicationList
}
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
)
}

}

0 comments on commit 1c47dad

Please sign in to comment.