@@ -11,6 +11,7 @@ import com.jeluchu.core.models.ErrorResponse
1111import com.jeluchu.core.models.jikan.anime.AnimeData.Companion.toDayEntity
1212import com.jeluchu.core.utils.*
1313import com.jeluchu.features.anime.mappers.documentToScheduleDayEntity
14+ import com.jeluchu.features.schedule.models.DayEntity
1415import com.jeluchu.features.schedule.models.ScheduleData
1516import com.jeluchu.features.schedule.models.ScheduleEntity
1617import com.mongodb.client.MongoDatabase
@@ -37,22 +38,20 @@ class ScheduleService(
3738
3839 if (needsUpdate) {
3940 schedules.deleteMany(Document ())
41+ val documents = mutableListOf<Document >()
4042
41- val response = ScheduleData (
42- sunday = getSchedule(Day .SUNDAY ).data?.map { it.toDayEntity(Day .SUNDAY ) }.orEmpty(),
43- friday = getSchedule(Day .FRIDAY ).data?.map { it.toDayEntity(Day .FRIDAY ) }.orEmpty(),
44- monday = getSchedule(Day .MONDAY ).data?.map { it.toDayEntity(Day .MONDAY ) }.orEmpty(),
45- tuesday = getSchedule(Day .TUESDAY ).data?.map { it.toDayEntity(Day .TUESDAY ) }.orEmpty(),
46- thursday = getSchedule(Day .THURSDAY ).data?.map { it.toDayEntity(Day .THURSDAY ) }.orEmpty(),
47- saturday = getSchedule(Day .SATURDAY ).data?.map { it.toDayEntity(Day .SATURDAY ) }.orEmpty(),
48- wednesday = getSchedule(Day .WEDNESDAY ).data?.map { it.toDayEntity(Day .WEDNESDAY ) }.orEmpty()
49- )
43+ Day .entries.forEach { day ->
44+ val animes = getSchedule(day).data?.map { it.toDayEntity(day) }.orEmpty()
45+ val documentsToInsert = parseDataToDocuments(animes, DayEntity .serializer())
46+ if (documentsToInsert.isNotEmpty()) {
47+ documents.addAll(documentsToInsert)
48+ schedules.insertMany(documentsToInsert)
49+ }
50+ }
5051
51- val elements = parseTopDataToDocuments(response)
52- if (elements.isNotEmpty()) schedules.insertMany(elements)
5352 timers.update(TimerKey .SCHEDULE )
5453
55- call.respond(HttpStatusCode .OK , elements .documentWeekMapper())
54+ call.respond(HttpStatusCode .OK , documents .documentWeekMapper())
5655 } else {
5756 val elements = schedules.find().toList()
5857 call.respond(HttpStatusCode .OK , elements.documentWeekMapper())
@@ -78,7 +77,16 @@ class ScheduleService(
7877 )
7978
8079 private fun List<Document>.documentWeekMapper (): String {
81- val directory = map { documentToScheduleDayEntity(it) }
82- return Json .encodeToString(directory)
80+ val elements = map { documentToScheduleDayEntity(it) }
81+
82+ return Json .encodeToString(ScheduleData (
83+ monday = elements.filter { it.day == Day .MONDAY .name.lowercase() },
84+ tuesday = elements.filter { it.day == Day .TUESDAY .name.lowercase() },
85+ wednesday = elements.filter { it.day == Day .WEDNESDAY .name.lowercase() },
86+ thursday = elements.filter { it.day == Day .THURSDAY .name.lowercase() },
87+ friday = elements.filter { it.day == Day .FRIDAY .name.lowercase() },
88+ saturday = elements.filter { it.day == Day .SATURDAY .name.lowercase() },
89+ sunday = elements.filter { it.day == Day .SUNDAY .name.lowercase() }
90+ ))
8391 }
8492}
0 commit comments