-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include filter directory by types and last episodes endpoint added
- Loading branch information
Showing
10 changed files
with
199 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/kotlin/com/jeluchu/core/models/animeflv/lastepisodes/EpisodeEntity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.jeluchu.core.models.animeflv.lastepisodes | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class EpisodeEntity( | ||
var number: Int, | ||
var image: String, | ||
var title: String | ||
) |
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/com/jeluchu/core/models/animeflv/lastepisodes/LastEpisodeData.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.jeluchu.core.models.animeflv.lastepisodes | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class LastEpisodeData( | ||
val cover: String?, | ||
val number: Int?, | ||
val title: String?, | ||
val url: String? | ||
) { | ||
companion object { | ||
fun LastEpisodeData.toEpisodeEntity() = EpisodeEntity( | ||
number = number ?: 0, | ||
image = cover.orEmpty(), | ||
title = title.orEmpty() | ||
) | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/com/jeluchu/core/models/animeflv/lastepisodes/LastEpisodes.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.jeluchu.core.models.animeflv.lastepisodes | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class LastEpisodes( | ||
val data: List<LastEpisodeData>?, | ||
val success: Boolean? | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/kotlin/com/jeluchu/features/anime/models/directory/AnimeTypeEntity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.jeluchu.features.anime.models.directory | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class AnimeTypeEntity( | ||
val malId: Int? = 0, | ||
val type: String? = "", | ||
val episodes: Int? = 0, | ||
val title: String? = "", | ||
val image: String? = "", | ||
val score: String? = "" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/main/kotlin/com/jeluchu/features/anime/services/DirectoryService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.jeluchu.features.anime.services | ||
|
||
import com.jeluchu.core.enums.TimeUnit | ||
import com.jeluchu.core.enums.parseAnimeType | ||
import com.jeluchu.core.extensions.needsUpdate | ||
import com.jeluchu.core.extensions.update | ||
import com.jeluchu.core.messages.ErrorMessages | ||
import com.jeluchu.core.models.ErrorResponse | ||
import com.jeluchu.core.utils.Collections | ||
import com.jeluchu.core.utils.TimerKey | ||
import com.jeluchu.features.anime.mappers.documentToAnimeTypeEntity | ||
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 DirectoryService( | ||
private val database: MongoDatabase | ||
) { | ||
private val timers = database.getCollection(Collections.TIMERS) | ||
private val directory = database.getCollection(Collections.ANIME_DETAILS) | ||
|
||
suspend fun getAnimeByType(call: RoutingCall) { | ||
val param = call.parameters["type"] ?: throw IllegalArgumentException(ErrorMessages.InvalidAnimeType.message) | ||
if (parseAnimeType(param) == null) call.respond( | ||
HttpStatusCode.BadRequest, | ||
ErrorResponse(ErrorMessages.InvalidAnimeType.message) | ||
) | ||
|
||
val timerKey = "${TimerKey.ANIME_TYPE}${param.lowercase()}" | ||
val needsUpdate = timers.needsUpdate( | ||
amount = 30, | ||
key = timerKey, | ||
unit = TimeUnit.DAY, | ||
) | ||
|
||
if (needsUpdate) { | ||
val collection = database.getCollection(timerKey) | ||
collection.deleteMany(Document()) | ||
|
||
val animes = directory.find(Filters.eq("type", param.uppercase())).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) | ||
|
||
call.respond(HttpStatusCode.OK, Json.encodeToString(animeTypes)) | ||
} else { | ||
val elements = directory.find().toList() | ||
call.respond(HttpStatusCode.OK, elements.documentAnimeTypeMapper()) | ||
} | ||
} | ||
|
||
private fun List<Document>.documentAnimeTypeMapper(): String { | ||
val directory = map { documentToAnimeTypeEntity(it) } | ||
return Json.encodeToString(directory) | ||
} | ||
} |