Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beans dsl #77

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions src/main/kotlin/com/krzykrucz/elesson/currentlesson/Database.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package com.krzykrucz.elesson.currentlesson

import arrow.core.Option
import com.krzykrucz.elesson.currentlesson.attendance.Attendance
import com.krzykrucz.elesson.currentlesson.attendance.CheckedAttendanceList
import com.krzykrucz.elesson.currentlesson.attendance.IncompleteAttendanceList
import com.krzykrucz.elesson.currentlesson.preparedness.StudentsUnpreparedForLesson
import com.krzykrucz.elesson.currentlesson.shared.ClassName
import com.krzykrucz.elesson.currentlesson.shared.ClassRegistry
import com.krzykrucz.elesson.currentlesson.shared.FirstName
import com.krzykrucz.elesson.currentlesson.shared.InProgressLesson
import com.krzykrucz.elesson.currentlesson.shared.LessonAfterAttendance
import com.krzykrucz.elesson.currentlesson.shared.LessonHourNumber
import com.krzykrucz.elesson.currentlesson.shared.LessonIdentifier
import com.krzykrucz.elesson.currentlesson.shared.LessonStatus
import com.krzykrucz.elesson.currentlesson.shared.LessonSubject
import com.krzykrucz.elesson.currentlesson.shared.LessonTopic
import com.krzykrucz.elesson.currentlesson.shared.NaturalNumber
import com.krzykrucz.elesson.currentlesson.shared.NonEmptyText
import com.krzykrucz.elesson.currentlesson.shared.NumberInRegister
import com.krzykrucz.elesson.currentlesson.shared.Scheduled
import com.krzykrucz.elesson.currentlesson.shared.SecondName
import com.krzykrucz.elesson.currentlesson.shared.Semester
import com.krzykrucz.elesson.currentlesson.shared.StartedLesson
import com.krzykrucz.elesson.currentlesson.shared.StudentRecord
import com.krzykrucz.elesson.currentlesson.shared.WinterSemester
import java.time.LocalDate
import java.util.concurrent.ConcurrentHashMap
import arrow.core.Option.Companion as Option1


data class PersistentCurrentLesson(
val lessonId: LessonIdentifier,
val classRegistry: ClassRegistry,
val lessonTopic: Option<LessonTopic> = Option.empty(),
val attendance: Attendance = IncompleteAttendanceList(),
val semester: Semester = WinterSemester,
val subject: LessonSubject,
val status: LessonStatus,
val unpreparedStudents: StudentsUnpreparedForLesson = StudentsUnpreparedForLesson()
) {

fun toLessonAfterAttendance(): Option<LessonAfterAttendance> {
if (this.lessonTopic.isEmpty() && this.attendance is CheckedAttendanceList) {
return LessonAfterAttendance(
this.lessonId,
this.attendance,
this.unpreparedStudents
).let(Option1::just)
}
return Option1.empty()
}

fun toStartedLesson(): Option<StartedLesson> {
if (this.attendance is IncompleteAttendanceList) {
return StartedLesson(
this.lessonId,
this.classRegistry,
this.subject
).let(Option1::just)
}
return Option1.empty()
}

fun toLessonInProgress(): Option<InProgressLesson> =
this.lessonTopic
.map { lessonTopic ->
InProgressLesson(
lessonId,
lessonTopic
)
}

}

object Database {

private val lessonId1 =
lessonIdOf("2019-09-09", 1, "1A")

private val classRegistryOf1A = ClassRegistry(
students = listOf(
createStudentRecord(
"Harry",
"Potter",
1
),
createStudentRecord(
"Tom",
"Riddle",
2
)

),
className = classNameOf("1A")
)

val LESSON_DATABASE: ConcurrentHashMap<LessonIdentifier, PersistentCurrentLesson> = ConcurrentHashMap(mutableMapOf(
lessonId1 to PersistentCurrentLesson(
lessonId1,
classRegistryOf1A,
semester = WinterSemester,
subject = LessonSubject(
NonEmptyText(
"Defense from dark arts"
)
),
status = Scheduled
)
))

private fun lessonIdOf(date: String, number: Int, className: String) =
LessonIdentifier(
LocalDate.parse(date),
lessonHourNumberOf(number),
classNameOf(className)
)

private fun lessonHourNumberOf(number: Int) =
LessonHourNumber.of(number).orNull()!!

private fun classNameOf(name: String) =
ClassName(
NonEmptyText.of(
name
)!!
)

private fun createStudentRecord(name: String, surname: String, numberInRegister: Int): StudentRecord =
StudentRecord(
firstName = FirstName(
NonEmptyText.of(
name
)!!
),
secondName = SecondName(
NonEmptyText.of(
surname
)!!
),
numberInRegister = NaturalNumber.of(numberInRegister)
.map(::NumberInRegister)
.orNull()!!
)

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
package com.krzykrucz.elesson.currentlesson

import com.krzykrucz.elesson.currentlesson.attendance.attendanceAdapters
import com.krzykrucz.elesson.currentlesson.finishlesson.finishLessonAdapters
import com.krzykrucz.elesson.currentlesson.lessonprogress.lessonProgressAdapters
import com.krzykrucz.elesson.currentlesson.startlesson.startLessonAdapters
import com.krzykrucz.elesson.currentlesson.topic.topicAdapters
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.context.ApplicationContextInitializer
import org.springframework.context.support.GenericApplicationContext

@SpringBootApplication
class ELessonApplication

fun main(args: Array<String>) {
runApplication<ELessonApplication>(*args)
}

object BeansInitializer : ApplicationContextInitializer<GenericApplicationContext> {
override fun initialize(context: GenericApplicationContext) {
startLessonAdapters.initialize(context)
topicAdapters.initialize(context)
finishLessonAdapters.initialize(context)
lessonProgressAdapters.initialize(context)
attendanceAdapters.initialize(context)
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading