Fixed schedule endpoint

This commit is contained in:
Jéluchu 2025-07-04 14:38:19 +02:00
parent 1566c84e8b
commit 5552904983
3 changed files with 23 additions and 50 deletions

View File

@ -1,44 +1,9 @@
package com.jeluchu.core.utils
import com.jeluchu.features.rankings.models.AnimeTopEntity
import com.jeluchu.features.schedule.models.DayEntity
import com.jeluchu.features.schedule.models.ScheduleData
import kotlinx.serialization.KSerializer
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.bson.Document
fun parseTopDataToDocuments(data: ScheduleData): List<Document> {
val documents = mutableListOf<Document>()
fun processDay(dayList: List<DayEntity>?) {
dayList?.forEach { animeData ->
val animeJsonString = Json.encodeToString(animeData)
val document = Document.parse(animeJsonString)
documents.add(document)
}
}
processDay(data.monday)
processDay(data.tuesday)
processDay(data.wednesday)
processDay(data.thursday)
processDay(data.friday)
processDay(data.saturday)
processDay(data.sunday)
return documents
}
fun parseTopDataToDocuments(data: List<AnimeTopEntity>?): List<Document> {
val documents = mutableListOf<Document>()
data?.forEach { animeData ->
val animeJsonString = Json.encodeToString(animeData)
val document = Document.parse(animeJsonString)
documents.add(document)
}
return documents
}
fun <T> parseDataToDocuments(data: List<T>?, serializer: KSerializer<T>): List<Document> {
val documents = mutableListOf<Document>()
data?.forEach { item ->

View File

@ -368,7 +368,7 @@ class RankingsService(
}.orEmpty().take(11).distinctBy { it.malId }
val documentsToInsert = parseDataToDocuments(response, AnimeTopEntity.serializer())
if (documentsToInsert.isNotEmpty()) animeRankingTopTen .insertMany(documentsToInsert)
if (documentsToInsert.isNotEmpty()) animeRankingTopTen.insertMany(documentsToInsert)
timers.update(timerKey)
val elements = documentsToInsert.map { documentToAnimeTopEntity(it) }

View File

@ -11,6 +11,7 @@ import com.jeluchu.core.models.ErrorResponse
import com.jeluchu.core.models.jikan.anime.AnimeData.Companion.toDayEntity
import com.jeluchu.core.utils.*
import com.jeluchu.features.anime.mappers.documentToScheduleDayEntity
import com.jeluchu.features.schedule.models.DayEntity
import com.jeluchu.features.schedule.models.ScheduleData
import com.jeluchu.features.schedule.models.ScheduleEntity
import com.mongodb.client.MongoDatabase
@ -37,22 +38,20 @@ class ScheduleService(
if (needsUpdate) {
schedules.deleteMany(Document())
val documents = mutableListOf<Document>()
val response = ScheduleData(
sunday = getSchedule(Day.SUNDAY).data?.map { it.toDayEntity(Day.SUNDAY) }.orEmpty(),
friday = getSchedule(Day.FRIDAY).data?.map { it.toDayEntity(Day.FRIDAY) }.orEmpty(),
monday = getSchedule(Day.MONDAY).data?.map { it.toDayEntity(Day.MONDAY) }.orEmpty(),
tuesday = getSchedule(Day.TUESDAY).data?.map { it.toDayEntity(Day.TUESDAY) }.orEmpty(),
thursday = getSchedule(Day.THURSDAY).data?.map { it.toDayEntity(Day.THURSDAY) }.orEmpty(),
saturday = getSchedule(Day.SATURDAY).data?.map { it.toDayEntity(Day.SATURDAY) }.orEmpty(),
wednesday = getSchedule(Day.WEDNESDAY).data?.map { it.toDayEntity(Day.WEDNESDAY) }.orEmpty()
)
Day.entries.forEach { day ->
val animes = getSchedule(day).data?.map { it.toDayEntity(day) }.orEmpty()
val documentsToInsert = parseDataToDocuments(animes, DayEntity.serializer())
if (documentsToInsert.isNotEmpty()) {
documents.addAll(documentsToInsert)
schedules.insertMany(documentsToInsert)
}
}
val elements = parseTopDataToDocuments(response)
if (elements.isNotEmpty()) schedules.insertMany(elements)
timers.update(TimerKey.SCHEDULE)
call.respond(HttpStatusCode.OK, elements.documentWeekMapper())
call.respond(HttpStatusCode.OK, documents.documentWeekMapper())
} else {
val elements = schedules.find().toList()
call.respond(HttpStatusCode.OK, elements.documentWeekMapper())
@ -78,7 +77,16 @@ class ScheduleService(
)
private fun List<Document>.documentWeekMapper(): String {
val directory = map { documentToScheduleDayEntity(it) }
return Json.encodeToString(directory)
val elements = map { documentToScheduleDayEntity(it) }
return Json.encodeToString(ScheduleData(
monday = elements.filter { it.day == Day.MONDAY.name.lowercase() },
tuesday = elements.filter { it.day == Day.TUESDAY.name.lowercase() },
wednesday = elements.filter { it.day == Day.WEDNESDAY.name.lowercase() },
thursday = elements.filter { it.day == Day.THURSDAY.name.lowercase() },
friday = elements.filter { it.day == Day.FRIDAY.name.lowercase() },
saturday = elements.filter { it.day == Day.SATURDAY.name.lowercase() },
sunday = elements.filter { it.day == Day.SUNDAY.name.lowercase() }
))
}
}