mirror of https://github.com/aruppi/aruppi-api.git
parent
03842ab1f9
commit
b0fd2e84ad
@ -0,0 +1,27 @@
|
|||||||
|
package com.jeluchu.core.models
|
||||||
|
|
||||||
|
import com.jeluchu.core.extensions.getDocumentSafe
|
||||||
|
import com.jeluchu.core.extensions.getIntSafe
|
||||||
|
import com.jeluchu.core.extensions.getStringSafe
|
||||||
|
import com.jeluchu.features.anime.mappers.documentToMultipleLanguageLists
|
||||||
|
import com.jeluchu.features.anime.models.anime.MultipleLanguageLists
|
||||||
|
import com.jeluchu.features.anime.models.tags.TagsAnimeEntity
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
import org.bson.Document
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class SimpleAnimeEntity(
|
||||||
|
val malId: Int,
|
||||||
|
val type: String,
|
||||||
|
val title: String,
|
||||||
|
val image: String,
|
||||||
|
val score: String
|
||||||
|
)
|
||||||
|
|
||||||
|
fun documentToSimpleAnimeEntity(doc: Document) = SimpleAnimeEntity(
|
||||||
|
malId = doc.getIntSafe("malId"),
|
||||||
|
title = doc.getStringSafe("title"),
|
||||||
|
type = doc.getStringSafe("type"),
|
||||||
|
score = doc.getStringSafe("score"),
|
||||||
|
image = doc.getStringSafe("poster")
|
||||||
|
)
|
@ -1,13 +0,0 @@
|
|||||||
package com.jeluchu.features.anime.mappers
|
|
||||||
|
|
||||||
import com.jeluchu.core.extensions.getIntSafe
|
|
||||||
import com.jeluchu.core.extensions.getStringSafe
|
|
||||||
import com.jeluchu.features.anime.models.seasons.SeasonAnimeEntity
|
|
||||||
import org.bson.Document
|
|
||||||
|
|
||||||
fun documentToSeasonEntity(doc: Document) = SeasonAnimeEntity(
|
|
||||||
score = doc.getStringSafe("score"),
|
|
||||||
malId = doc.getIntSafe("malId"),
|
|
||||||
image = doc.getStringSafe("poster"),
|
|
||||||
title = doc.getStringSafe("title")
|
|
||||||
)
|
|
@ -1,11 +0,0 @@
|
|||||||
package com.jeluchu.features.anime.models.seasons
|
|
||||||
|
|
||||||
import kotlinx.serialization.Serializable
|
|
||||||
|
|
||||||
@Serializable
|
|
||||||
data class SeasonAnimeEntity(
|
|
||||||
val malId: Int,
|
|
||||||
val title: String,
|
|
||||||
val image: String,
|
|
||||||
val score: String,
|
|
||||||
)
|
|
@ -0,0 +1,44 @@
|
|||||||
|
package com.jeluchu.features.anime.services
|
||||||
|
|
||||||
|
import com.jeluchu.core.models.documentToSimpleAnimeEntity
|
||||||
|
import com.jeluchu.core.utils.Collections
|
||||||
|
import com.mongodb.client.MongoCollection
|
||||||
|
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 TagsService(
|
||||||
|
private val database: MongoDatabase,
|
||||||
|
private val directory: MongoCollection<Document> = database.getCollection(Collections.ANIME_DIRECTORY)
|
||||||
|
) {
|
||||||
|
suspend fun getAnimeByAnyTag(call: RoutingCall) {
|
||||||
|
val tagsParam = call.request.queryParameters["tags"].orEmpty()
|
||||||
|
|
||||||
|
val tags = if (tagsParam.isNotEmpty()) {
|
||||||
|
tagsParam.split(",").map { it.trim() }
|
||||||
|
} else emptyList()
|
||||||
|
|
||||||
|
if (tags.isEmpty()) {
|
||||||
|
call.respond(HttpStatusCode.BadRequest, "No tags provided")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
val query = directory.find(
|
||||||
|
Filters.or(
|
||||||
|
Filters.`in`("tags.es", tags),
|
||||||
|
Filters.`in`("tags.en", tags),
|
||||||
|
Filters.ne("type", "MUSIC"),
|
||||||
|
Filters.ne("type", "PV"),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.toList()
|
||||||
|
.map { documentToSimpleAnimeEntity(it) }
|
||||||
|
|
||||||
|
call.respond(HttpStatusCode.OK, Json.encodeToString(query))
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue