Fixed save data to MongoDB and retrieve information

v5 v5.3.4
Jéluchu 7 months ago
parent 3760530255
commit 08caaa60fc

@ -278,7 +278,7 @@ fun documentToCharacterTopEntity(doc: Document) = CharacterTopEntity(
) )
fun documentToAnimeTypeEntity(doc: Document) = AnimeTypeEntity( fun documentToAnimeTypeEntity(doc: Document) = AnimeTypeEntity(
score = doc.getString("score"), score = doc.getStringSafe("score"),
malId = doc.getIntSafe("malId"), malId = doc.getIntSafe("malId"),
year = doc.getIntSafe("year"), year = doc.getIntSafe("year"),
season = doc.getStringSafe("season"), season = doc.getStringSafe("season"),

@ -39,7 +39,7 @@ class DirectoryService(
ErrorResponse(ErrorMessages.InvalidAnimeType.message) ErrorResponse(ErrorMessages.InvalidAnimeType.message)
) )
val timerKey = "${TimerKey.ANIME_TYPE}${param.lowercase()}_$page" val timerKey = "${TimerKey.ANIME_TYPE}${param.lowercase()}"
val collection = database.getCollection(timerKey) val collection = database.getCollection(timerKey)
val needsUpdate = timers.needsUpdate( val needsUpdate = timers.needsUpdate(
@ -53,8 +53,6 @@ class DirectoryService(
val animes = directory val animes = directory
.find(Filters.eq("type", param.uppercase())) .find(Filters.eq("type", param.uppercase()))
.skip(skipCount)
.limit(size)
.toList() .toList()
val animeTypes = animes.map { documentToAnimeTypeEntity(it) } val animeTypes = animes.map { documentToAnimeTypeEntity(it) }
@ -62,10 +60,18 @@ class DirectoryService(
if (documents.isNotEmpty()) collection.insertMany(documents) if (documents.isNotEmpty()) collection.insertMany(documents)
timers.update(timerKey) timers.update(timerKey)
val animeTypeDb = collection
.find()
.skip(skipCount)
.limit(size)
.toList()
val animeTypeEntity = animeTypeDb.map { documentToAnimeTypeEntity(it) }
val response = PaginationResponse( val response = PaginationResponse(
page = page, page = page,
data = animeTypes, data = animeTypeEntity,
size = animeTypes.size size = animeTypeEntity.size
) )
call.respond(HttpStatusCode.OK, Json.encodeToString(response)) call.respond(HttpStatusCode.OK, Json.encodeToString(response))
@ -96,7 +102,7 @@ class DirectoryService(
if (page < 1 || size < 1) call.respond(HttpStatusCode.BadRequest, ErrorMessages.InvalidSizeAndPage.message) if (page < 1 || size < 1) call.respond(HttpStatusCode.BadRequest, ErrorMessages.InvalidSizeAndPage.message)
val skipCount = (page - 1) * size val skipCount = (page - 1) * size
val timerKey = "${TimerKey.ANIME_TYPE}${year}_${season}_$page" val timerKey = "${TimerKey.ANIME_TYPE}${year}_${season.lowercase()}"
val collection = database.getCollection(timerKey) val collection = database.getCollection(timerKey)
val needsUpdate = timers.needsUpdate( val needsUpdate = timers.needsUpdate(
@ -112,22 +118,26 @@ class DirectoryService(
.find( .find(
Filters.and( Filters.and(
Filters.eq("year", year), Filters.eq("year", year),
Filters.eq("season", season) Filters.eq("season", season.lowercase())
) )
) ).toList()
.skip(skipCount)
.limit(size)
.toList()
val animeTypes = animes.map { documentToAnimeTypeEntity(it) } val animeTypes = animes.map { documentToAnimeTypeEntity(it) }
val documents = animeTypes.map { anime -> Document.parse(Json.encodeToString(anime)) } val documents = animeTypes.map { anime -> Document.parse(Json.encodeToString(anime)) }
if (documents.isNotEmpty()) collection.insertMany(documents) if (documents.isNotEmpty()) collection.insertMany(documents)
timers.update(timerKey) timers.update(timerKey)
val animeSeasonDb = collection
.find()
.skip(skipCount)
.limit(size)
.toList()
val animeSeason = animeSeasonDb.map { documentToAnimeTypeEntity(it) }
val response = PaginationResponse( val response = PaginationResponse(
page = page, page = page,
data = animeTypes, data = animeSeason,
size = animeTypes.size size = animeSeason.size
) )
call.respond(HttpStatusCode.OK, Json.encodeToString(response)) call.respond(HttpStatusCode.OK, Json.encodeToString(response))

Loading…
Cancel
Save