Skip to content

Commit

Permalink
Merge pull request #117 from Kusitms-29th-ASAP/feature/tranlate/timet…
Browse files Browse the repository at this point in the history
…able

feat: 시간표 도메인 번역 추가
  • Loading branch information
tlarbals824 authored Aug 26, 2024
2 parents 02fcfb7 + 268140d commit f76d7c5
Show file tree
Hide file tree
Showing 21 changed files with 439 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.asap.asapbackend.domain.child.domain.service.ChildReader
import com.asap.asapbackend.domain.classroom.domain.service.ClassroomReader
import com.asap.asapbackend.domain.timetable.application.dto.GetThisWeekTimetable
import com.asap.asapbackend.domain.timetable.application.dto.GetTodayTimetable
import com.asap.asapbackend.domain.timetable.domain.service.SubjectReader
import com.asap.asapbackend.domain.timetable.domain.service.TimetableReader
import com.asap.asapbackend.global.security.getCurrentUserId
import org.springframework.stereotype.Service
Expand All @@ -14,15 +15,18 @@ import org.springframework.transaction.annotation.Transactional
class TimetableService(
private val classroomReader: ClassroomReader,
private val childReader: ChildReader,
private val timetableReader: TimetableReader
private val timetableReader: TimetableReader,
private val subjectReader: SubjectReader
) {

fun getTodayTimetable(): GetTodayTimetable.Response {
val userId = getCurrentUserId()
val studentId = childReader.findPrimaryChild(userId).id
val classroomId = classroomReader.findByStudent(studentId).id
val todayTimetables = timetableReader.findTodayTimetableByClassroomId(classroomId)
val subjectMap = subjectReader.findSubjectMapByClassroomId(classroomId)
val timetables = todayTimetables.map {
GetTodayTimetable.Timetable(it.time, it.subject.name)
GetTodayTimetable.Timetable(it.time, subjectMap[it.subject.id]?.name ?: "")
}
return GetTodayTimetable.Response(timetables)
}
Expand All @@ -32,9 +36,10 @@ class TimetableService(
val studentId = childReader.findPrimaryChild(userId).id
val classroomId = classroomReader.findByStudent(studentId).id
val weekTimetables = timetableReader.findThisWeekTimetableByClassroomId(classroomId)
val subjectMap = subjectReader.findSubjectMapByClassroomId(classroomId)
val weekDataList = weekTimetables.mapValues { (_, timetables) ->
timetables.map { timetable ->
GetThisWeekTimetable.Timetable(timetable?.time, timetable?.subject?.name)
GetThisWeekTimetable.Timetable(timetable.time, subjectMap[timetable.subject.id]?.name ?: "")
}
}
return GetThisWeekTimetable.Response(weekDataList)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class GetThisWeekTimetable {
val timetables : Map<DayOfWeek,List<Timetable>>
)
data class Timetable(
val time: Int?,
val subject: String?
val time: Int,
val subject: String
)
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
package com.asap.asapbackend.domain.timetable.domain.model

import com.asap.asapbackend.domain.classroom.domain.model.Classroom
import com.asap.asapbackend.global.domain.BaseDateEntity
import jakarta.persistence.Entity
import jakarta.persistence.FetchType
import jakarta.persistence.ManyToOne
import java.time.LocalDateTime

@Entity
class Subject(
classroom: Classroom,
id: Long = 0,
classroomId: Long,
name: String,
semester: String
) : BaseDateEntity() {
semester: String,
createdAt: LocalDateTime = LocalDateTime.now(),
updatedAt: LocalDateTime = LocalDateTime.now()
) {
val id: Long = id

val name : String = name

val semester : String = semester

@ManyToOne(fetch = FetchType.LAZY)
val classroom: Classroom = classroom
val classroomId: Long = classroomId

fun isSameSubject(subject: Subject): Boolean {
return this.name == subject.name && this.semester == subject.semester && this.classroom.id == subject.classroom.id
val createdAt: LocalDateTime = createdAt
val updatedAt: LocalDateTime = updatedAt


fun isSameSubject(
name: String,
semester: String,
classroomId: Long
): Boolean {
return this.name == name && this.semester == semester && this.classroomId == classroomId
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package com.asap.asapbackend.domain.timetable.domain.model

import com.asap.asapbackend.global.domain.BaseDateEntity
import jakarta.persistence.Entity
import jakarta.persistence.FetchType
import jakarta.persistence.ManyToOne
import java.time.LocalDate
import java.time.LocalDateTime

@Entity
class Timetable(
id: Long = 0,
subject: Subject,
day: LocalDate,
time: Int
) : BaseDateEntity() {
time: Int,
createdAt: LocalDateTime = LocalDateTime.now(),
updatedAt: LocalDateTime = LocalDateTime.now()
) {
val id: Long = id

@ManyToOne(fetch = FetchType.LAZY)
val subject: Subject = subject

val day: LocalDate = day

val time: Int = time

val createdAt: LocalDateTime = createdAt
val updatedAt: LocalDateTime = updatedAt
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package com.asap.asapbackend.domain.timetable.domain.repository

import com.asap.asapbackend.domain.classroom.domain.model.Classroom
import com.asap.asapbackend.domain.timetable.domain.model.Subject
import org.springframework.data.jpa.repository.JpaRepository

interface SubjectRepository : JpaRepository<Subject, Long> {
interface SubjectRepository{
fun findByClassroomIn(classroom: List<Classroom>) : List<Subject>
fun saveAll(subjects: List<Subject>): List<Subject>
fun findOriginalSubjectsByClassroomIn(classroom: List<Classroom>) : List<Subject>

fun findAllByClassroomId(classroomId: Long): List<Subject>
fun save(subject: Subject): Subject
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.asap.asapbackend.domain.timetable.domain.repository

import com.asap.asapbackend.domain.timetable.domain.model.Timetable
import org.springframework.data.jpa.repository.JpaRepository
import java.time.LocalDate

interface TimetableRepository : JpaRepository<Timetable, Long> {
fun findBySubjectClassroomIdAndDayOrderByTime(classroomId: Long, day: LocalDate) : List<Timetable>
interface TimetableRepository{
fun findByClassroomIdAndDayOrderByTime(classroomId: Long, day: LocalDate) : List<Timetable>

fun insertBatch(timetables: Collection<Timetable>)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.asap.asapbackend.domain.timetable.domain.service

import com.asap.asapbackend.domain.timetable.domain.model.Subject
import com.asap.asapbackend.domain.timetable.domain.repository.SubjectRepository
import org.springframework.stereotype.Service

@Service
class SubjectReader(
private val subjectRepository: SubjectRepository
) {

fun findSubjectMapByClassroomId(classroomId: Long): Map<Long, Subject> {
return subjectRepository.findAllByClassroomId(classroomId).associateBy { it.id }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,78 @@ package com.asap.asapbackend.domain.timetable.domain.service
import com.asap.asapbackend.batch.timetable.TimetableInfoProvider
import com.asap.asapbackend.domain.classroom.domain.model.Classroom
import com.asap.asapbackend.domain.classroom.domain.repository.ClassroomRepository
import com.asap.asapbackend.domain.school.domain.model.School
import com.asap.asapbackend.domain.timetable.domain.model.Subject
import com.asap.asapbackend.domain.timetable.domain.model.Timetable
import com.asap.asapbackend.domain.timetable.domain.repository.SubjectRepository
import com.asap.asapbackend.domain.timetable.domain.repository.TimetableJdbcRepository
import com.asap.asapbackend.domain.timetable.domain.repository.TimetableRepository
import com.asap.asapbackend.domain.timetable.event.MultiSubjectCreateEvent
import org.springframework.context.ApplicationEventPublisher
import org.springframework.stereotype.Service


@Service
class TimetableAppender(
private val classroomRepository: ClassroomRepository,
private val subjectRepository: SubjectRepository,
private val timetableJdbcRepository: TimetableJdbcRepository
private val timetableRepository: TimetableRepository,
private val applicationEventPublisher: ApplicationEventPublisher
) {

fun addSubjectAndTimetable(timetables: List<TimetableInfoProvider.TimetableResponse>) {
val existClassroomMap = classroomRepository.findBySchoolIn(timetables.map { it.school })
.groupBy { it.school }
val timetableList = mutableSetOf<Timetable>()
val subjectList = subjectRepository.findByClassroomIn(existClassroomMap.values.flatten())
.groupByTo(mutableMapOf()) { it.classroom }

val subjectList = subjectRepository.findOriginalSubjectsByClassroomIn(existClassroomMap.values.flatten())
.groupByTo(mutableMapOf()) { it.classroomId }

val savedTimetables = mutableSetOf<Timetable>()

handleTimetables(timetables, existClassroomMap, subjectList) { subject, timetableResponse ->
savedTimetables.add(
Timetable(
subject = subject,
day = timetableResponse.day,
time = timetableResponse.time
)
)
}

timetableRepository.insertBatch(savedTimetables)

applicationEventPublisher.publishEvent(MultiSubjectCreateEvent(subjectList.values.flatten().toSet()))
}

private fun handleTimetables(
timetables: List<TimetableInfoProvider.TimetableResponse>,
existClassroomMap: Map<School, List<Classroom>>,
subjectList: MutableMap<Long, MutableList<Subject>>,
subjectHandler: (Subject, TimetableInfoProvider.TimetableResponse) -> Unit = { _, _ -> }
) {
timetables.forEach {
if (it.name == null) return@forEach

val classroom = existClassroomMap[it.school]?.find { classroom ->
classroom.isSameClassroom(Classroom(it.grade, it.className, it.school))
val classroom = existClassroomMap[it.school]?.find { subject ->
subject.isSameClassroom(Classroom(it.grade, it.className, it.school))
} ?: return@forEach


val existSubject = subjectList[classroom]?.find { subject ->
subject.isSameSubject(Subject(classroom, it.name, it.semester))
} ?: Subject(classroom, it.name, it.semester).also {
subjectList.getOrPut(classroom) { mutableListOf() }.add(it)
val subject = subjectList[classroom.id]?.let { findSubjectList ->
findSubjectList.find { subject ->
subject.isSameSubject(it.name, it.semester, classroom.id)
}
} ?: run {
val savedSubject = subjectRepository.save(
Subject(
name = it.name,
semester = it.semester,
classroomId = classroom.id
)
)
subjectList.getOrPut(classroom.id) { mutableListOf() }.add(savedSubject)
savedSubject
}

val timetable = Timetable(
subject = existSubject,
day = it.day,
time = it.time
)
timetableList.add(timetable)
subjectHandler(subject, it)
}

subjectRepository.saveAll(subjectList.values.flatten())
timetableJdbcRepository.insertBatch(timetableList)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ class TimetableReader(
private val timetableRepository: TimetableRepository
) {
fun findTodayTimetableByClassroomId(classroomId: Long): List<Timetable> {
return timetableRepository.findBySubjectClassroomIdAndDayOrderByTime(classroomId, LocalDate.now())
return timetableRepository.findByClassroomIdAndDayOrderByTime(classroomId, LocalDate.now())
}

fun findThisWeekTimetableByClassroomId(classroomId: Long): Map<DayOfWeek, List<Timetable?>> {
fun findThisWeekTimetableByClassroomId(classroomId: Long): Map<DayOfWeek, List<Timetable>> {
val daysOfWeek = listOf(
DayOfWeek.MONDAY,
DayOfWeek.TUESDAY,
Expand All @@ -23,7 +23,7 @@ class TimetableReader(
DayOfWeek.FRIDAY
)
return daysOfWeek.associateWith { day ->
timetableRepository.findBySubjectClassroomIdAndDayOrderByTime(classroomId, LocalDate.now().with(day))
timetableRepository.findByClassroomIdAndDayOrderByTime(classroomId, LocalDate.now().with(day))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.asap.asapbackend.domain.timetable.event

import com.asap.asapbackend.domain.timetable.domain.model.Subject


data class MultiSubjectCreateEvent(
val subjects: Set<Subject>
) {
}
Loading

0 comments on commit f76d7c5

Please sign in to comment.