Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
chore(school): remove circular dependency in classes (schoolDomain)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaBrighi committed Jun 18, 2023
1 parent 9376885 commit 604c4cf
Show file tree
Hide file tree
Showing 18 changed files with 84 additions and 342 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.intelligentbackpack.schooldata.adapter

import com.intelligentbackpack.schooldomain.entities.School
import com.intelligentbackpack.schooldata.db.entities.SchoolClass as DBClass
import com.intelligentbackpack.schooldomain.entities.Class as DomainClass

object ClassAdapter {

fun DBClass.fromDBToDomain(school: School): DomainClass {
fun DBClass.fromDBToDomain(): DomainClass {
return DomainClass.create(
name = name,
school = school,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ object LessonAdapter {
)
}

fun DBLesson.fromDBToDomain(professor: Professor, schoolClass: Class): WeekLesson {
fun DBLesson.fromDBToDomain(professor: Professor, schoolClass: Class, subject: String): WeekLesson {
return createWeekLesson(
subject = module,
subject = subject,
module = module,
professor = professor,
studentsClass = schoolClass,
startTime = startTime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ object ProfessorAdapter {
email = email,
name = name,
surname = surname,
professorClasses = emptyMap(),
)
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package com.intelligentbackpack.schooldata.adapter

import com.intelligentbackpack.accessdomain.entities.User
import com.intelligentbackpack.schooldomain.entities.Class
import com.intelligentbackpack.schooldomain.entities.person.Student

object StudentAdapter {

fun User.fromAccessToSchool(schoolClass: Class): Student {
fun User.fromAccessToSchool(): Student {
return Student.create(
email = email,
name = name,
surname = surname,
studentClass = schoolClass,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ import com.intelligentbackpack.schooldomain.entities.person.Student
* A class is a group of students that attend the same subjects taught by the same professors in the same school with the same schedule.
*
* @property name the name of the class
* @property school the school where the class is taught
* @property students the students that attend the class
* @property subjects the subjects taught in the class
* @property professors the professors that teach in the class
* @property professorTeachSubjects the subjects taught by each professor in the class
*/
interface Class {
val name: String
val school: School
val students: Set<Student>
val subjects: Set<Subject>
val professors: Set<Professor>
Expand All @@ -35,23 +33,22 @@ interface Class {
* @param professor the professor to add
* @param subjects the subjects taught by the professor in the class
*/
fun addProfessor(professor: Professor, subjects: Set<Subject>): Pair<Class, Professor>
fun addProfessor(professor: Professor, subjects: Set<Subject>): Class

companion object {

/**
* Creates a class.
*
* @param name the name of the class
* @param school the school where the class is taught
* @return the created class
* @throws IllegalArgumentException if the name is blank
*/
fun create(name: String, school: School): Class {
fun create(name: String): Class {
if (name.isBlank()) {
throw IllegalArgumentException("name cannot be blank")
} else {
return ClassImpl(name, school)
return ClassImpl(name)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,17 @@ object CalendarEventFactory {
} else if (fromDate.isAfter(toDate)) {
throw IllegalArgumentException("fromDate cannot be after toDate")
} else {
if (!professor.professorSubjects.contains(subject)) {
throw IllegalArgumentException("professor must teach subject")
} else {
return WeekLessonImpl(
subject,
module,
professor,
studentsClass,
startTime,
endTime,
day,
fromDate,
toDate,
)
}
return WeekLessonImpl(
subject,
module,
professor,
studentsClass,
startTime,
endTime,
day,
fromDate,
toDate,
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,39 +63,30 @@ internal data class SchoolCalendarImpl(
}

override fun addLessons(lessons: Set<WeekLesson>): SchoolCalendar {
if (lessons.any { lesson ->
lesson.professor.professorClasses.any {
it.school.calendar?.schoolYear != schoolYear
}
}
) {
throw IllegalArgumentException("lessons must be in the same school year")
if (lessons.any { lesson -> this.lessons.any { it == lesson } }) {
throw IllegalArgumentException("lessons must not be in current lessons")
} else {
if (lessons.any { lesson -> this.lessons.any { it == lesson } }) {
throw IllegalArgumentException("lessons must not be in current lessons")
} else {
val checkOverlappingStudents = lessons.any { lesson ->
areLessonsInTimeTableOverlapping(
lessons,
studentsTimeTableLesson[lesson.studentsClass] ?: emptyMap(),
) {
it.studentsClass == lesson.studentsClass
}
}
val checkOverlappingProfessors = lessons.any { lesson ->
areLessonsInTimeTableOverlapping(
lessons,
professorsTimeTableLesson[lesson.professor] ?: emptyMap(),
) {
it.professor == lesson.professor
}
val checkOverlappingStudents = lessons.any { lesson ->
areLessonsInTimeTableOverlapping(
lessons,
studentsTimeTableLesson[lesson.studentsClass] ?: emptyMap(),
) {
it.studentsClass == lesson.studentsClass
}
if (checkOverlappingStudents || checkOverlappingProfessors) {
throw EventOverlappingException()
} else {
return copy(lessons = this.lessons + lessons)
}
val checkOverlappingProfessors = lessons.any { lesson ->
areLessonsInTimeTableOverlapping(
lessons,
professorsTimeTableLesson[lesson.professor] ?: emptyMap(),
) {
it.professor == lesson.professor
}
}
if (checkOverlappingStudents || checkOverlappingProfessors) {
throw EventOverlappingException()
} else {
return copy(lessons = this.lessons + lessons)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.intelligentbackpack.schooldomain.entities.implementation

import com.intelligentbackpack.schooldomain.entities.Class
import com.intelligentbackpack.schooldomain.entities.School
import com.intelligentbackpack.schooldomain.entities.Subject
import com.intelligentbackpack.schooldomain.entities.person.Professor
import com.intelligentbackpack.schooldomain.entities.person.Student
Expand All @@ -10,15 +9,13 @@ import com.intelligentbackpack.schooldomain.entities.person.Student
* A class is a group of students that attend the same subjects taught by the same professors in the same school with the same schedule.
*
* @property name the name of the class
* @property school the school where the class is taught
* @property students the students that attend the class
* @property subjects the subjects taught in the class
* @property professors the professors that teach in the class
* @property professorTeachSubjects the subjects taught by each professor in the class
*/
internal data class ClassImpl(
override val name: String,
override val school: School,
) : Class {
override var students: Set<Student> = setOf()
private set
Expand All @@ -39,12 +36,8 @@ internal data class ClassImpl(
if (students.contains(student)) {
throw IllegalArgumentException("student is already in this class")
} else {
if (student.studentClass != this) {
throw IllegalArgumentException("student is not in this class")
} else {
return copy().apply {
students = students + student
}
return copy().apply {
students = students + student
}
}
}
Expand All @@ -57,32 +50,22 @@ internal data class ClassImpl(
* @param subjects the subjects taught by the professor in the class
* @throws IllegalArgumentException if the professor is not in the class or if the professor doesn't teach the subjects in the class
*/
override fun addProfessor(professor: Professor, subjects: Set<Subject>): Pair<Class, Professor> {
override fun addProfessor(professor: Professor, subjects: Set<Subject>): Class {
if (subjects.isEmpty()) {
throw IllegalArgumentException("subjects cannot be empty")
} else if (professor.professorClasses.any { it.school != school }) {
throw IllegalArgumentException("professor doesn't teach the subjects in this school")
} else {
val newProfessor =
if (!professor.professorClasses.contains(this)) {
professor.addProfessorToClass(this, subjects)
} else if (professor.professorSubjectsInClasses[this]?.containsAll(subjects) == false) {
professor.addProfessorToClass(this, subjects)
} else {
professor
}
val newClass = if (professors.contains(professor)) {
val oldSubjects = professorTeachSubjects[professor]!!
copy().apply {
professorTeachSubjects = professorTeachSubjects + (newProfessor to oldSubjects + subjects)
professorTeachSubjects = professorTeachSubjects + (professor to oldSubjects + subjects)
}
} else {
copy().apply {
professors = professors + professor
professorTeachSubjects = professorTeachSubjects + (newProfessor to subjects)
professorTeachSubjects = professorTeachSubjects + (professor to subjects)
}
}
return Pair(newClass, newProfessor)
return newClass
}
}
}
Original file line number Diff line number Diff line change
@@ -1,88 +1,37 @@
package com.intelligentbackpack.schooldomain.entities.person

import com.intelligentbackpack.schooldomain.entities.Class
import com.intelligentbackpack.schooldomain.entities.Subject

/**
* A professor.
*
* @property name the name of the professor
* @property surname the surname of the professor
* @property email the email of the professor
* @property professorClasses the classes the professor teaches
* @property professorSubjects the subjects the professor teaches
* @property professorSubjectsInClasses the subjects the professor teaches in each class
*/
interface Professor : Person {
val subjects: Set<Subject>
val professorSubjectsInClasses: Map<Class, Set<Subject>>
val professorClasses: Set<Class>
get() = professorSubjectsInClasses.keys

val professorSubjects: Set<Subject>
get() = professorSubjectsInClasses.values.flatten().toSet()

/**
* Adds the professor to a class.
* If the professor is already in the class, the subjects are added to the subjects the professor teaches in the class.
*
* @param professorClass the class to add the professor to
* @param subjects the subjects the professor teaches in the class
* @throws IllegalArgumentException if the subjects are empty
*/
fun addProfessorToClass(professorClass: Class, subjects: Set<Subject>): Professor

companion object {

private data class ProfessorImpl(
override val email: String,
override val name: String,
override val surname: String,
) : Professor {
override val subjects: Set<Subject>
get() = professorSubjectsInClasses.values.flatten().toSet()
override var professorSubjectsInClasses: Map<Class, Set<Subject>> = mapOf()
private set

override fun addProfessorToClass(professorClass: Class, subjects: Set<Subject>): Professor {
if (subjects.isEmpty()) {
throw IllegalArgumentException("subjects cannot be empty")
} else {
return if (professorSubjectsInClasses.containsKey(professorClass)) {
val oldSubjects = professorSubjectsInClasses[professorClass]!!
copy().apply {
professorSubjectsInClasses =
this@ProfessorImpl.professorSubjectsInClasses +
(professorClass to (oldSubjects + subjects))
}
} else {
copy().apply {
professorSubjectsInClasses =
this@ProfessorImpl.professorSubjectsInClasses + (professorClass to subjects)
}
}
}
}
}
) : Professor

/**
* Creates a professor.
*
* @param email the email of the professor
* @param name the name of the professor
* @param surname the surname of the professor
* @param professorClasses the classes the professor teaches
* @return the created professor
* @throws IllegalArgumentException if the email is blank
* @throws IllegalArgumentException if the name is blank
* @throws IllegalArgumentException if the surname is blank
* @throws IllegalArgumentException if the professorClasses are empty
*/
fun create(
email: String,
name: String,
surname: String,
professorClasses: Map<Class, Set<Subject>>,
): Professor {
if (email.isBlank()) {
throw IllegalArgumentException("email cannot be blank")
Expand All @@ -91,15 +40,7 @@ interface Professor : Person {
} else if (surname.isBlank()) {
throw IllegalArgumentException("surname cannot be blank")
} else {
if (professorClasses.any { it.value.isEmpty() }) {
throw IllegalArgumentException("professorClasses cannot be empty")
} else {
return ProfessorImpl(email, name, surname).let {
professorClasses.entries.fold(it as Professor) { professor, (professorClass, subjects) ->
professor.addProfessorToClass(professorClass, subjects)
}
}
}
return ProfessorImpl(email, name, surname)
}
}
}
Expand Down
Loading

0 comments on commit 604c4cf

Please sign in to comment.