Skip to content

Commit

Permalink
Fixed save data to MongoDB and retrieve information
Browse files Browse the repository at this point in the history
  • Loading branch information
jeluchu committed Jan 16, 2025
1 parent 3760530 commit 08caaa6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -53,19 +53,25 @@ class DirectoryService(

val animes = directory
.find(Filters.eq("type", param.uppercase()))
.skip(skipCount)
.limit(size)
.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 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))
Expand Down Expand Up @@ -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(
Expand All @@ -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))
Expand Down

0 comments on commit 08caaa6

Please sign in to comment.