From a5f3ae55289f68333cf9d3a3c90e4ad6f5bb6823 Mon Sep 17 00:00:00 2001 From: eve00 <64850924+eve00@users.noreply.github.com> Date: Fri, 22 Dec 2023 18:30:02 +0900 Subject: [PATCH] =?UTF-8?q?=E4=B8=8D=E8=A6=81=E3=83=A2=E3=82=B8=E3=83=A5?= =?UTF-8?q?=E3=83=BC=E3=83=AB=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 1 - src/main/kotlin/auth/Authenticator.kt | 8 --- .../data/repository/CoursesRepository.kt | 1 - .../domain/entity/{User.kt => Student.kt} | 11 +--- .../domain/service/CourseManagementService.kt | 4 +- .../kotlin/domain/service/CourseService.kt | 15 ----- .../impl/CourseRegistrationServiceImpl.kt | 8 +-- .../domain/service/impl/CourseServiceImpl.kt | 23 ------- .../impl/FirstServedManagementServiceImpl.kt | 8 +-- src/main/kotlin/webServer/Main.kt | 32 +-------- src/main/kotlin/webServer/Routes.kt | 65 +++++-------------- 11 files changed, 31 insertions(+), 145 deletions(-) delete mode 100644 src/main/kotlin/auth/Authenticator.kt rename src/main/kotlin/domain/entity/{User.kt => Student.kt} (85%) delete mode 100644 src/main/kotlin/domain/service/CourseService.kt delete mode 100644 src/main/kotlin/domain/service/impl/CourseServiceImpl.kt diff --git a/build.gradle.kts b/build.gradle.kts index e129a9e..83f2022 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -22,7 +22,6 @@ dependencies { testImplementation ("org.http4k:http4k-client-jetty:4.48.0.0") - testImplementation( "org.jsoup:jsoup:1.12.1") } tasks.test { diff --git a/src/main/kotlin/auth/Authenticator.kt b/src/main/kotlin/auth/Authenticator.kt deleted file mode 100644 index 69ad88e..0000000 --- a/src/main/kotlin/auth/Authenticator.kt +++ /dev/null @@ -1,8 +0,0 @@ -package auth - -interface Authenticator { - - fun authenticateAs(): Boolean - - fun authorize():Boolean -} \ No newline at end of file diff --git a/src/main/kotlin/data/repository/CoursesRepository.kt b/src/main/kotlin/data/repository/CoursesRepository.kt index c60cd16..65b6939 100644 --- a/src/main/kotlin/data/repository/CoursesRepository.kt +++ b/src/main/kotlin/data/repository/CoursesRepository.kt @@ -8,5 +8,4 @@ interface CoursesRepository { suspend fun findAll() : List suspend fun findById(courseId: CourseId) : Course suspend fun findByFaculty(faculty: Faculty) : List - suspend fun save(course:Course) } \ No newline at end of file diff --git a/src/main/kotlin/domain/entity/User.kt b/src/main/kotlin/domain/entity/Student.kt similarity index 85% rename from src/main/kotlin/domain/entity/User.kt rename to src/main/kotlin/domain/entity/Student.kt index cb5dbf8..1d512f5 100644 --- a/src/main/kotlin/domain/entity/User.kt +++ b/src/main/kotlin/domain/entity/Student.kt @@ -7,13 +7,12 @@ typealias StudentId = Identifier -sealed class User() -class Student( +data class Student( private val id: StudentId, private val name: String, private val grade: Int, private val faculty: Faculty, -): User() { +) { fun getId(): StudentId { return id @@ -24,8 +23,4 @@ class Student( fun getFaculty(): Faculty { return faculty } -} - - - -class Teacher(): User() \ No newline at end of file +} \ No newline at end of file diff --git a/src/main/kotlin/domain/service/CourseManagementService.kt b/src/main/kotlin/domain/service/CourseManagementService.kt index 310a354..36b2256 100644 --- a/src/main/kotlin/domain/service/CourseManagementService.kt +++ b/src/main/kotlin/domain/service/CourseManagementService.kt @@ -7,5 +7,5 @@ interface CourseRegistrationService { suspend fun drawingAndRegisterMembers(courseId: CourseId) - suspend fun registerMembers(courseId:CourseId) -} \ No newline at end of file + suspend fun registerMembers(courseId: CourseId) +} diff --git a/src/main/kotlin/domain/service/CourseService.kt b/src/main/kotlin/domain/service/CourseService.kt deleted file mode 100644 index 3af0534..0000000 --- a/src/main/kotlin/domain/service/CourseService.kt +++ /dev/null @@ -1,15 +0,0 @@ -package domain.service - -import domain.entity.Course -import domain.entity.CourseId -import domain.entity.StudentId - - -/* -* repository以上のものいらんかな -* */ -interface CourseService { - suspend fun getCourses(): List - suspend fun getCourse(courseId: CourseId):Course - } - diff --git a/src/main/kotlin/domain/service/impl/CourseRegistrationServiceImpl.kt b/src/main/kotlin/domain/service/impl/CourseRegistrationServiceImpl.kt index 827e379..d906d14 100644 --- a/src/main/kotlin/domain/service/impl/CourseRegistrationServiceImpl.kt +++ b/src/main/kotlin/domain/service/impl/CourseRegistrationServiceImpl.kt @@ -2,24 +2,22 @@ package domain.service.impl import data.repository.CourseMembersRepository import data.repository.CourseTakingApplicationsRepository +import data.repository.CoursesRepository import data.repository.StudentsRepository import domain.entity.CourseId import domain.entity.CourseTakingApplication import domain.entity.State import domain.service.CourseRegistrationService -import domain.service.CourseService -import domain.service.CourseTakingApplicationService class CourseRegistrationServiceImpl( - private val courseService: CourseService, - private val courseTakingApplicationService: CourseTakingApplicationService, private val courseTakingApplicationsRepository: CourseTakingApplicationsRepository, private val courseMembersRepository: CourseMembersRepository, + private val coursesRepository: CoursesRepository, private val studentsRepository: StudentsRepository ) : CourseRegistrationService { override suspend fun drawingAndRegisterMembers(courseId: CourseId) { /*抽選*/ - val max = courseService.getCourse(courseId).getMax() + val max = coursesRepository.findById(courseId).getMax() val drawedCourseTakingApplications = getCourseTakingApplications(courseId).shuffled() .subList(0, max - 1) drawedCourseTakingApplications.forEach { diff --git a/src/main/kotlin/domain/service/impl/CourseServiceImpl.kt b/src/main/kotlin/domain/service/impl/CourseServiceImpl.kt deleted file mode 100644 index a991422..0000000 --- a/src/main/kotlin/domain/service/impl/CourseServiceImpl.kt +++ /dev/null @@ -1,23 +0,0 @@ -package domain.service.impl - -import data.repository.CoursesRepository -import data.repository.StudentsRepository -import domain.entity.Course -import domain.entity.CourseId -import domain.entity.StudentId -import domain.entity.common.Faculty -import domain.service.CourseService - -class CourseServiceImpl( - private val courseRepository: CoursesRepository, -) : CourseService { - override suspend fun getCourses(): List { - return courseRepository.findAll() - } - - override suspend fun getCourse(courseId: CourseId): Course { - return courseRepository.findById(courseId) - } - - -} \ No newline at end of file diff --git a/src/main/kotlin/domain/service/impl/FirstServedManagementServiceImpl.kt b/src/main/kotlin/domain/service/impl/FirstServedManagementServiceImpl.kt index bee78b6..3a273e4 100644 --- a/src/main/kotlin/domain/service/impl/FirstServedManagementServiceImpl.kt +++ b/src/main/kotlin/domain/service/impl/FirstServedManagementServiceImpl.kt @@ -1,21 +1,21 @@ package domain.service.impl import data.repository.CourseTakingApplicationsRepository +import data.repository.CoursesRepository import domain.entity.Course import domain.entity.CourseId -import domain.service.CourseService import domain.service.FirstServedManagementService class FirstServedManagementServiceImpl( val repository: CourseTakingApplicationsRepository, - val courseService: CourseService + val coursesRepository: CoursesRepository ) : FirstServedManagementService { override suspend fun checkCanTake(courseId: CourseId): Boolean { - return repository.findByCourseId(courseId).size < courseService.getCourse(courseId).getMax() + return repository.findByCourseId(courseId).size < coursesRepository.findById(courseId).getMax() } override suspend fun getCoursesCanTake(): List { - return courseService.getCourses() + return coursesRepository.findAll() .filter { course -> repository.findByCourseId(course.getId()).size < course.getMax() } diff --git a/src/main/kotlin/webServer/Main.kt b/src/main/kotlin/webServer/Main.kt index df23613..ebb90ca 100644 --- a/src/main/kotlin/webServer/Main.kt +++ b/src/main/kotlin/webServer/Main.kt @@ -2,41 +2,15 @@ package webServer fun main(args: Array) { -/* - val hub = CourseTakingApplicationHub( commandHandler) + /* CourseTakingAndRegistration( + + ).asServer(Jetty(8080)).start()*/ - CourseTaking(hub).asServer(Jetty(8080)).start() -*/ println("Server started at http://localhost:8080") } -/*TODO: 1. Simple Type*/ -/*値オブジェクトに仕様を語らせる*/ - -/*TODO: 2. implement steps*/ - - -/*TODO: 3. create Events*/ - -/*TODO: 4. composing the pipeline*/ - -/*TODO: 5. injecting dependencies*/ - -/* -API: ApplyCourseTakingApi - -Request -> UnValidatedCourseTaking -> ValidatedCourseTaking -> Result -> Response - -validateCourseTaking... その学生の情報から申請が有効なのかを検証する - -Event:CourseTakingApplied 履修登録が申請された - - -*/ - - diff --git a/src/main/kotlin/webServer/Routes.kt b/src/main/kotlin/webServer/Routes.kt index 2a161fd..0b0d62b 100644 --- a/src/main/kotlin/webServer/Routes.kt +++ b/src/main/kotlin/webServer/Routes.kt @@ -1,9 +1,8 @@ package webServer +import data.repository.CoursesRepository import domain.entity.* -import domain.entity.CourseTakingApplication import domain.service.CourseRegistrationService -import domain.service.CourseService import domain.service.CourseTakingApplicationService import kotlinx.coroutines.* import org.http4k.core.* @@ -11,7 +10,6 @@ import org.http4k.routing.bind import org.http4k.routing.path import org.http4k.routing.routes import java.util.* -import org.http4k.core.ContentType.Companion.APPLICATION_JSON import org.http4k.core.Status import org.http4k.core.Status.Companion.OK @@ -19,40 +17,37 @@ import org.http4k.core.Status.Companion.OK * TODO: * 抽選、先着管理、登録、科目取得 * */ -class CourseTaking( +class CourseTakingAndRegistration( private val courseTakingApplicationService: CourseTakingApplicationService, private val courseRegistrationService: CourseRegistrationService, - private val courseService: CourseService + private val coursesRepository: CoursesRepository ) : HttpHandler { override fun invoke(request: Request): Response = httpHandler(request) val httpHandler = routes( - "/ping" bind Method.GET to { Response(Status.OK) }, /*QUERY*/ "/course" bind Method.GET to ::getCourses, - "/course" bind Method.GET to ::getCourses, - "/application" bind Method.GET to ::getApplications, + "/application/{studentId}" bind Method.GET to ::getApplications, /*courseTaking*/ - "/application" bind Method.POST to ::applyCourseTaking, - "/application" bind Method.DELETE to ::cancelCourseTaking, + "/application/createApplication" bind Method.POST to ::applyCourseTaking, + "/application/cancelApplication" bind Method.DELETE to ::cancelCourseTaking, /*courseRegistration*/ - "/course/{courseId}" bind Method.GET to ::drawAndRegisterCourseMembers, - "/course/{courseId}" bind Method.POST to ::registerCourseMembers, + "/course/{courseId}/drawAndRegisterMembers" bind Method.POST to ::drawAndRegisterCourseMembers, + "/course/{courseId}/registerMembers" bind Method.POST to ::registerCourseMembers, ) - @OptIn(ExperimentalCoroutinesApi::class) + private fun getCourses(request: Request): Response { val result = CoroutineScope(Dispatchers.IO).async { runCatching { - courseService.getCourses() + coursesRepository.findAll() } } /*responseを返す*/ return if (result.getCompleted().isSuccess) { - JsonData("Success").toOKResponse() + Response(OK) } else { - /*TODO: エラーハンドリング*/ Response(Status.BAD_REQUEST) } } @@ -60,11 +55,10 @@ class CourseTaking( /* * {studentId} * */ - @OptIn(ExperimentalCoroutinesApi::class) private fun getApplications(request: Request): Response { - /*requestからuserを取得*/ val result = CoroutineScope(Dispatchers.IO).async { runCatching { + /*requestからstudentIdを取得*/ val studentId: StudentId = StudentId(request.path("studentId") ?: "") courseTakingApplicationService.getCourseTakingApplications(studentId) @@ -75,7 +69,6 @@ class CourseTaking( return if (result.getCompleted().isSuccess) { Response(OK) } else { - /*TODO: エラーハンドリング*/ Response(Status.BAD_REQUEST) } } @@ -83,7 +76,6 @@ class CourseTaking( /* * {studentId, courseId, applicationFormat} * */ - @OptIn(ExperimentalCoroutinesApi::class) private fun applyCourseTaking(request: Request): Response { val result = CoroutineScope(Dispatchers.IO).async { runCatching { @@ -92,14 +84,16 @@ class CourseTaking( val courseId: CourseId = CourseId(request.path("courseId") ?: "") val applicationFormat: String = request.path("applicationFormat") ?: "" + /*申請*/ val courseTakingApplicationId = CourseTakingApplicationId(UUID.randomUUID().toString()) when (applicationFormat) { + /*事前申請*/ "advanced" -> courseTakingApplicationService.applyCourseTaking( courseTakingApplicationId, studentId, courseId ) - + /*先着申請*/ "first-served" -> courseTakingApplicationService.applyCourseTakingBasedOnFirstserved( courseTakingApplicationId, studentId, @@ -110,20 +104,16 @@ class CourseTaking( } /*responseを返す*/ - return if (result.getCompleted().isSuccess) { Response(OK) } else { - /*TODO: エラーハンドリング*/ Response(Status.BAD_REQUEST) } - } /* * {studentId, courseTakingApplicationId} * */ - @OptIn(ExperimentalCoroutinesApi::class) private fun cancelCourseTaking(request: Request): Response { val result = CoroutineScope(Dispatchers.IO).async { runCatching { @@ -132,6 +122,7 @@ class CourseTaking( val courseTakingApplicationId: CourseTakingApplicationId = CourseTakingApplicationId(request.path("courseTakingApplicationId") ?: "") + /*申請のキャンセル*/ courseTakingApplicationService.cancelCourseTaking( courseTakingApplicationId, ) @@ -139,18 +130,14 @@ class CourseTaking( } /*responseを返す*/ - return if (result.getCompleted().isSuccess) { Response(OK) } else { - /*TODO: エラーハンドリング*/ Response(Status.BAD_REQUEST) } } - @OptIn(ExperimentalCoroutinesApi::class) private fun drawAndRegisterCourseMembers(request: Request): Response { - val result = CoroutineScope(Dispatchers.IO).async { runCatching { /*requestからcourseIdを取得*/ @@ -160,12 +147,10 @@ class CourseTaking( } } - /*結果を返す*/ return if (result.getCompleted().isSuccess) { Response(OK) } else { - /*TODO: エラーハンドリング*/ Response(Status.BAD_REQUEST) } @@ -186,25 +171,7 @@ class CourseTaking( return if (result.getCompleted().isSuccess) { Response(OK) } else { - /*TODO: エラーハンドリング*/ Response(Status.BAD_REQUEST) } } - - - data class JsonData(val raw: String) { - fun toOKResponse(): Response { - return Response(OK).body(raw).header("Content-Type", APPLICATION_JSON.toHeaderValue()) - } - } - - fun convertApplicationListToJson(list: List): JsonData { - /*TODO: serialize list*/ - return JsonData("nothing") - } - - fun toResponse(data: JsonData): Response = - Response(Status.OK).body(data.raw) - - }