mirror of https://github.com/aruppi/aruppi-api.git
parent
56b8c1108a
commit
5d1b33e1e8
@ -0,0 +1,10 @@
|
||||
package com.jeluchu.core.models.animeflv.lastepisodes
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class EpisodeEntity(
|
||||
var number: Int,
|
||||
var image: String,
|
||||
var title: String
|
||||
)
|
@ -0,0 +1,19 @@
|
||||
package com.jeluchu.core.models.animeflv.lastepisodes
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class LastEpisodeData(
|
||||
val cover: String?,
|
||||
val number: Int?,
|
||||
val title: String?,
|
||||
val url: String?
|
||||
) {
|
||||
companion object {
|
||||
fun LastEpisodeData.toEpisodeEntity() = EpisodeEntity(
|
||||
number = number ?: 0,
|
||||
image = cover.orEmpty(),
|
||||
title = title.orEmpty()
|
||||
)
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.jeluchu.core.models.animeflv.lastepisodes
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class LastEpisodes(
|
||||
val data: List<LastEpisodeData>?,
|
||||
val success: Boolean?
|
||||
)
|
@ -0,0 +1,13 @@
|
||||
package com.jeluchu.features.anime.models.directory
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class AnimeTypeEntity(
|
||||
val malId: Int? = 0,
|
||||
val type: String? = "",
|
||||
val episodes: Int? = 0,
|
||||
val title: String? = "",
|
||||
val image: String? = "",
|
||||
val score: String? = ""
|
||||
)
|
@ -0,0 +1,62 @@
|
||||
package com.jeluchu.features.anime.services
|
||||
|
||||
import com.jeluchu.core.enums.TimeUnit
|
||||
import com.jeluchu.core.enums.parseAnimeType
|
||||
import com.jeluchu.core.extensions.needsUpdate
|
||||
import com.jeluchu.core.extensions.update
|
||||
import com.jeluchu.core.messages.ErrorMessages
|
||||
import com.jeluchu.core.models.ErrorResponse
|
||||
import com.jeluchu.core.utils.Collections
|
||||
import com.jeluchu.core.utils.TimerKey
|
||||
import com.jeluchu.features.anime.mappers.documentToAnimeTypeEntity
|
||||
import com.mongodb.client.MongoDatabase
|
||||
import com.mongodb.client.model.Filters
|
||||
import io.ktor.http.*
|
||||
import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
import kotlinx.serialization.encodeToString
|
||||
import kotlinx.serialization.json.Json
|
||||
import org.bson.Document
|
||||
|
||||
class DirectoryService(
|
||||
private val database: MongoDatabase
|
||||
) {
|
||||
private val timers = database.getCollection(Collections.TIMERS)
|
||||
private val directory = database.getCollection(Collections.ANIME_DETAILS)
|
||||
|
||||
suspend fun getAnimeByType(call: RoutingCall) {
|
||||
val param = call.parameters["type"] ?: throw IllegalArgumentException(ErrorMessages.InvalidAnimeType.message)
|
||||
if (parseAnimeType(param) == null) call.respond(
|
||||
HttpStatusCode.BadRequest,
|
||||
ErrorResponse(ErrorMessages.InvalidAnimeType.message)
|
||||
)
|
||||
|
||||
val timerKey = "${TimerKey.ANIME_TYPE}${param.lowercase()}"
|
||||
val needsUpdate = timers.needsUpdate(
|
||||
amount = 30,
|
||||
key = timerKey,
|
||||
unit = TimeUnit.DAY,
|
||||
)
|
||||
|
||||
if (needsUpdate) {
|
||||
val collection = database.getCollection(timerKey)
|
||||
collection.deleteMany(Document())
|
||||
|
||||
val animes = directory.find(Filters.eq("type", param.uppercase())).toList()
|
||||
val animeTypes = animes.map { documentToAnimeTypeEntity(it) }
|
||||
val documents = animeTypes.map { anime -> Document.parse(Json.encodeToString(anime)) }
|
||||
if (documents.isNotEmpty()) collection.insertMany(documents)
|
||||
timers.update(timerKey)
|
||||
|
||||
call.respond(HttpStatusCode.OK, Json.encodeToString(animeTypes))
|
||||
} else {
|
||||
val elements = directory.find().toList()
|
||||
call.respond(HttpStatusCode.OK, elements.documentAnimeTypeMapper())
|
||||
}
|
||||
}
|
||||
|
||||
private fun List<Document>.documentAnimeTypeMapper(): String {
|
||||
val directory = map { documentToAnimeTypeEntity(it) }
|
||||
return Json.encodeToString(directory)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue