Skip to content
This repository has been archived by the owner on Mar 28, 2024. It is now read-only.

Commit

Permalink
Merge pull request #23 from Z-Jais/master
Browse files Browse the repository at this point in the history
Add anime watchlist and diary
  • Loading branch information
Ziedelth authored Oct 24, 2022
2 parents e37bb9e + 227d542 commit 93947c3
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
44 changes: 44 additions & 0 deletions src/main/kotlin/fr/ziedelth/controllers/AnimeController.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.ziedelth.controllers

import com.google.gson.Gson
import fr.ziedelth.entities.Anime
import fr.ziedelth.entities.isNullOrNotValid
import fr.ziedelth.utils.Database
Expand All @@ -10,13 +11,16 @@ import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import java.io.ByteArrayInputStream
import java.util.*
import java.util.zip.GZIPInputStream

object AnimeController : IController<Anime>("/animes") {
fun Routing.getAnimes() {
route(prefix) {
search()
getWithPage()
getWatchlistWithPage()
getAttachment()
create()
merge()
Expand Down Expand Up @@ -119,6 +123,46 @@ object AnimeController : IController<Anime>("/animes") {
}
}

private fun base64(string: String): ByteArray = Base64.getDecoder().decode(string)

private fun fromGzip(string: String): String {
val gzip = GZIPInputStream(ByteArrayInputStream(base64(string)))
val compressed = gzip.readBytes()
gzip.close()
return String(compressed)
}

private fun Route.getWatchlistWithPage() {
post("/watchlist/page/{page}/limit/{limit}") {
val watchlist = call.receive<String>()
val page = call.parameters["page"]?.toInt() ?: return@post call.respond(HttpStatusCode.BadRequest)
val limit = call.parameters["limit"]?.toInt() ?: return@post call.respond(HttpStatusCode.BadRequest)
if (page < 1 || limit < 1) return@post call.respond(HttpStatusCode.BadRequest)
if (limit > 30) return@post call.respond(HttpStatusCode.BadRequest)
println("GET $prefix/watchlist/page/$page/limit/$limit")
val session = Database.getSession()

try {
val dataFromGzip =
Gson().fromJson(fromGzip(watchlist), Array<String>::class.java).map { UUID.fromString(it) }

val query = session.createQuery(
"FROM Anime WHERE uuid IN :list ORDER BY name",
Anime::class.java
)
query.setParameter("list", dataFromGzip)
query.firstResult = (limit * page) - limit
query.maxResults = limit
call.respond(query.list())
} catch (e: Exception) {
e.printStackTrace()
call.respond(HttpStatusCode.InternalServerError, e.message ?: "Unknown error")
} finally {
session.close()
}
}
}

private fun Route.create() {
post {
println("POST $prefix")
Expand Down
35 changes: 35 additions & 0 deletions src/main/kotlin/fr/ziedelth/controllers/DiaryController.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package fr.ziedelth.controllers

import fr.ziedelth.entities.Anime
import fr.ziedelth.utils.Database
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.server.routing.*

object DiaryController : IController<Anime>("/diary") {
fun Routing.getDiary() {
route(prefix) {
get("/country/{country}") {
val country = call.parameters["country"] ?: return@get call.respond(HttpStatusCode.BadRequest)
println("GET $prefix/country/$country")
val session = Database.getSession()

try {
val query = session.createQuery(
"SELECT anime FROM Episode episode WHERE episode.anime.country.tag = :tag AND current_date - to_date(episode.releaseDate, 'YYYY-MM-DDTHH:MI:SS') <= 7 ORDER BY episode.releaseDate DESC",
entityClass
)
query.setParameter("tag", country)
val list = query.list()?.distinctBy { it.uuid }
call.respond(list ?: HttpStatusCode.NotFound)
} catch (e: Exception) {
e.printStackTrace()
call.respond(HttpStatusCode.InternalServerError, e.message ?: "Unknown error")
} finally {
session.close()
}
}
}
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/fr/ziedelth/controllers/IController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import java.lang.reflect.ParameterizedType
import java.util.*

open class IController<T : Serializable>(val prefix: String) {
private val entityClass: Class<T> =
val entityClass: Class<T> =
(javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<T>
val entityName: String = entityClass.simpleName
val uuidRequest: UUID = UUID.randomUUID()
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/fr/ziedelth/plugins/Routing.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.ziedelth.plugins

import fr.ziedelth.controllers.AnimeController.getAnimes
import fr.ziedelth.controllers.DiaryController.getDiary
import fr.ziedelth.controllers.CountryController.getCountries
import fr.ziedelth.controllers.DeviceController.getDevices
import fr.ziedelth.controllers.DeviceRedirectionController.getRedirection
Expand Down Expand Up @@ -36,5 +37,6 @@ fun Application.configureRouting() {
getMangas()
getDevices()
getRedirection()
getDiary()
}
}

0 comments on commit 93947c3

Please sign in to comment.