Skip to content

Commit

Permalink
Fixed schedule generic endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jeluchu committed Jan 9, 2025
1 parent f948ca6 commit 56b8c11
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
16 changes: 16 additions & 0 deletions src/main/kotlin/com/jeluchu/core/connection/RestClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.ktor.client.engine.cio.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import kotlinx.coroutines.delay
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.json.Json

Expand All @@ -24,4 +25,19 @@ object RestClient {
json.decodeFromString(deserializer, response.bodyAsText())
}.getOrElse { throwable -> throw throwable }
}

suspend fun <T> requestWithDelay(
url: String,
delay: Long = 1000,
deserializer: DeserializationStrategy<T>
): T {
return runCatching {
val response = client.get(url) {
headers { append(HttpHeaders.Accept, ContentType.Application.Json.toString()) }
}

delay(delay)
json.decodeFromString(deserializer, response.bodyAsText())
}.getOrElse { throwable -> throw throwable }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,37 @@ class ScheduleService(
wednesday = getSchedule(Day.WEDNESDAY).data?.map { it.toDayEntity(Day.WEDNESDAY) }.orEmpty()
)

val documentsToInsert = parseTopDataToDocuments(response)
if (documentsToInsert.isNotEmpty()) schedules.insertMany(documentsToInsert)
val elements = parseTopDataToDocuments(response)
if (elements.isNotEmpty()) schedules.insertMany(elements)
timers.update(TimerKey.SCHEDULE)

call.respond(HttpStatusCode.OK, Json.encodeToString(response))
call.respond(HttpStatusCode.OK, elements.documentWeekMapper())
} else {
val elements = schedules.find().toList()
val directory = elements.map { documentToScheduleDayEntity(it) }
val json = Json.encodeToString(directory)
call.respond(HttpStatusCode.OK, json)
call.respond(HttpStatusCode.OK, elements.documentWeekMapper())
}
}

suspend fun getScheduleByDay(call: RoutingCall) {
val param = call.parameters["day"] ?: throw IllegalArgumentException(ErrorMessages.InvalidMalId.message)
if (parseDay(param) == null) call.respond(HttpStatusCode.BadRequest, ErrorResponse(ErrorMessages.InvalidDay.message))
if (parseDay(param) == null) call.respond(
HttpStatusCode.BadRequest,
ErrorResponse(ErrorMessages.InvalidDay.message)
)

val elements = schedules.find(Filters.eq("day", param.lowercase())).toList()
val directory = elements.map { documentToScheduleDayEntity(it) }
val json = Json.encodeToString(directory)
call.respond(HttpStatusCode.OK, json)
}

private suspend fun getSchedule(day: Day) =
RestClient.request(BaseUrls.JIKAN + Endpoints.SCHEDULES + "/" + day, ScheduleEntity.serializer())
private suspend fun getSchedule(day: Day) = RestClient.requestWithDelay(
url = BaseUrls.JIKAN + Endpoints.SCHEDULES + "/" + day,
deserializer = ScheduleEntity.serializer()
)

private fun List<Document>.documentWeekMapper(): String {
val directory = map { documentToScheduleDayEntity(it) }
return Json.encodeToString(directory)
}
}

0 comments on commit 56b8c11

Please sign in to comment.