From 08caaa60fc16bc61da6ff646e084d924e34ed1c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9luchu?= Date: Thu, 16 Jan 2025 11:50:27 +0100 Subject: [PATCH] Fixed save data to MongoDB and retrieve information --- .../features/anime/mappers/AnimeMappers.kt | 2 +- .../anime/services/DirectoryService.kt | 36 ++++++++++++------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/main/kotlin/com/jeluchu/features/anime/mappers/AnimeMappers.kt b/src/main/kotlin/com/jeluchu/features/anime/mappers/AnimeMappers.kt index 8d95353..cda67aa 100644 --- a/src/main/kotlin/com/jeluchu/features/anime/mappers/AnimeMappers.kt +++ b/src/main/kotlin/com/jeluchu/features/anime/mappers/AnimeMappers.kt @@ -278,7 +278,7 @@ fun documentToCharacterTopEntity(doc: Document) = CharacterTopEntity( ) fun documentToAnimeTypeEntity(doc: Document) = AnimeTypeEntity( - score = doc.getString("score"), + score = doc.getStringSafe("score"), malId = doc.getIntSafe("malId"), year = doc.getIntSafe("year"), season = doc.getStringSafe("season"), diff --git a/src/main/kotlin/com/jeluchu/features/anime/services/DirectoryService.kt b/src/main/kotlin/com/jeluchu/features/anime/services/DirectoryService.kt index a9aa189..7c76281 100644 --- a/src/main/kotlin/com/jeluchu/features/anime/services/DirectoryService.kt +++ b/src/main/kotlin/com/jeluchu/features/anime/services/DirectoryService.kt @@ -39,7 +39,7 @@ class DirectoryService( 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 needsUpdate = timers.needsUpdate( @@ -53,8 +53,6 @@ class DirectoryService( val animes = directory .find(Filters.eq("type", param.uppercase())) - .skip(skipCount) - .limit(size) .toList() val animeTypes = animes.map { documentToAnimeTypeEntity(it) } @@ -62,10 +60,18 @@ class DirectoryService( if (documents.isNotEmpty()) collection.insertMany(documents) timers.update(timerKey) + val animeTypeDb = collection + .find() + .skip(skipCount) + .limit(size) + .toList() + + val animeTypeEntity = animeTypeDb.map { documentToAnimeTypeEntity(it) } + val response = PaginationResponse( page = page, - data = animeTypes, - size = animeTypes.size + data = animeTypeEntity, + size = animeTypeEntity.size ) 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) 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 needsUpdate = timers.needsUpdate( @@ -112,22 +118,26 @@ class DirectoryService( .find( Filters.and( Filters.eq("year", year), - Filters.eq("season", season) + Filters.eq("season", season.lowercase()) ) - ) - .skip(skipCount) - .limit(size) - .toList() + ).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) + val animeSeasonDb = collection + .find() + .skip(skipCount) + .limit(size) + .toList() + + val animeSeason = animeSeasonDb.map { documentToAnimeTypeEntity(it) } val response = PaginationResponse( page = page, - data = animeTypes, - size = animeTypes.size + data = animeSeason, + size = animeSeason.size ) call.respond(HttpStatusCode.OK, Json.encodeToString(response))