diff --git a/src/main/kotlin/fr/ziedelth/controllers/AnimeController.kt b/src/main/kotlin/fr/ziedelth/controllers/AnimeController.kt index 37839ec..572e3b1 100644 --- a/src/main/kotlin/fr/ziedelth/controllers/AnimeController.kt +++ b/src/main/kotlin/fr/ziedelth/controllers/AnimeController.kt @@ -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 @@ -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("/animes") { fun Routing.getAnimes() { route(prefix) { search() getWithPage() + getWatchlistWithPage() getAttachment() create() merge() @@ -119,6 +123,46 @@ object AnimeController : IController("/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() + 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::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") diff --git a/src/main/kotlin/fr/ziedelth/controllers/DiaryController.kt b/src/main/kotlin/fr/ziedelth/controllers/DiaryController.kt new file mode 100644 index 0000000..df7c434 --- /dev/null +++ b/src/main/kotlin/fr/ziedelth/controllers/DiaryController.kt @@ -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("/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() + } + } + } + } +} diff --git a/src/main/kotlin/fr/ziedelth/controllers/IController.kt b/src/main/kotlin/fr/ziedelth/controllers/IController.kt index 81c7f31..4b15407 100644 --- a/src/main/kotlin/fr/ziedelth/controllers/IController.kt +++ b/src/main/kotlin/fr/ziedelth/controllers/IController.kt @@ -11,7 +11,7 @@ import java.lang.reflect.ParameterizedType import java.util.* open class IController(val prefix: String) { - private val entityClass: Class = + val entityClass: Class = (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class val entityName: String = entityClass.simpleName val uuidRequest: UUID = UUID.randomUUID() diff --git a/src/main/kotlin/fr/ziedelth/plugins/Routing.kt b/src/main/kotlin/fr/ziedelth/plugins/Routing.kt index 37675da..e1cdfd5 100644 --- a/src/main/kotlin/fr/ziedelth/plugins/Routing.kt +++ b/src/main/kotlin/fr/ziedelth/plugins/Routing.kt @@ -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 @@ -36,5 +37,6 @@ fun Application.configureRouting() { getMangas() getDevices() getRedirection() + getDiary() } }