Skip to content

Commit 1566c84

Browse files
committed
Include Top Ten endpoint
1 parent d7ca296 commit 1566c84

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

src/main/kotlin/com/jeluchu/core/utils/Constants.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ object Routes {
5757
const val MANGA = "/manga"
5858
const val PEOPLE = "/people"
5959
const val SEARCH = "/search"
60+
const val TOP_TEN = "/topTen"
6061
const val GALLERY = "/gallery"
6162
const val SCHEDULE = "/schedule"
6263
const val RADIO_STATIONS = "/radio"
@@ -86,6 +87,7 @@ object TimerKey {
8687

8788
object Collections {
8889
const val TIMERS = "timers"
90+
const val TOP_TEN = "top_ten"
8991
const val NEWS_ES = "news_es"
9092
const val NEWS_EN = "news_en"
9193
const val SCHEDULES = "schedule"
@@ -100,5 +102,6 @@ object Collections {
100102
const val ANIME_DIRECTORY = "anime_directory"
101103
const val CHARACTER_RANKING = "character_ranking"
102104
const val ANIME_PICTURES_QUERY = "anime_pictures_query"
105+
const val ANIME_RANKING_TOP_TEN = "anime_ranking_top_ten"
103106
const val ANIME_PICTURES_RECENT = "anime_pictures_recent"
104107
}

src/main/kotlin/com/jeluchu/features/rankings/routes/RankingsRoutes.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ fun Route.rankingsEndpoints(
1212
) = route(Routes.TOP) {
1313
route(Routes.ANIME) {
1414
getToJson { service.getAnimeRanking(call) }
15+
16+
route(Routes.TOP_TEN) {
17+
getToJson { service.getAnimeTopTenRanking(call) }
18+
}
1519
}
1620
route(Routes.MANGA) {
1721
getToJson { service.getMangaRanking(call) }

src/main/kotlin/com/jeluchu/features/rankings/services/RankingsService.kt

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class RankingsService(
4343
private val mangaRanking = database.getCollection(Collections.MANGA_RANKING)
4444
private val peopleRanking = database.getCollection(Collections.PEOPLE_RANKING)
4545
private val characterRanking = database.getCollection(Collections.CHARACTER_RANKING)
46+
private val animeRankingTopTen = database.getCollection(Collections.ANIME_RANKING_TOP_TEN)
4647

4748
suspend fun getAnimeRanking(call: RoutingCall) {
4849
val filter = call.request.queryParameters["filter"] ?: "airing"
@@ -327,4 +328,65 @@ class RankingsService(
327328
call.respond(HttpStatusCode.OK, Json.encodeToString(response))
328329
}
329330
}
331+
332+
suspend fun getAnimeTopTenRanking(call: RoutingCall) {
333+
val filter = call.request.queryParameters["filter"] ?: "airing"
334+
val type = call.parameters["type"] ?: throw IllegalArgumentException(ErrorMessages.InvalidTopAnimeType.message)
335+
336+
if (parseAnimeType(type) == null) call.respond(HttpStatusCode.BadRequest, ErrorResponse(ErrorMessages.InvalidTopAnimeType.message))
337+
338+
val timerKey = "${Collections.ANIME_RANKING}_${Collections.TOP_TEN}_${type}_${filter}"
339+
340+
val needsUpdate = timers.needsUpdate(
341+
amount = 7,
342+
key = timerKey,
343+
unit = TimeUnit.DAY
344+
)
345+
346+
if (needsUpdate) {
347+
animeRankingTopTen.deleteMany(
348+
Filters.and(
349+
Filters.eq("type", type),
350+
Filters.eq("subtype", filter)
351+
)
352+
)
353+
354+
val params = mutableListOf<String>()
355+
params.add("type=$type")
356+
params.add("filter=$filter")
357+
358+
val response = RestClient.request(
359+
BaseUrls.JIKAN + Endpoints.TOP_ANIME + "?${params.joinToString("&")}",
360+
AnimeSearch.serializer()
361+
).data?.map { anime ->
362+
anime.toAnimeTopEntity(
363+
page = 0,
364+
top = "anime",
365+
type = type,
366+
subType = filter
367+
)
368+
}.orEmpty().take(11).distinctBy { it.malId }
369+
370+
val documentsToInsert = parseDataToDocuments(response, AnimeTopEntity.serializer())
371+
if (documentsToInsert.isNotEmpty()) animeRankingTopTen .insertMany(documentsToInsert)
372+
timers.update(timerKey)
373+
374+
val elements = documentsToInsert.map { documentToAnimeTopEntity(it) }
375+
376+
call.respond(HttpStatusCode.OK, Json.encodeToString(elements))
377+
} else {
378+
val animes = animeRankingTopTen
379+
.find(
380+
Filters.and(
381+
Filters.eq("type", type),
382+
Filters.eq("subtype", filter)
383+
)
384+
)
385+
.toList()
386+
387+
val elements = animes.map { documentToAnimeTopEntity(it) }
388+
389+
call.respond(HttpStatusCode.OK, Json.encodeToString(elements))
390+
}
391+
}
330392
}

0 commit comments

Comments
 (0)