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

Commit

Permalink
Add diary
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziedelth committed Oct 24, 2022
1 parent cb3dce8 commit 227d542
Showing 1 changed file with 35 additions and 0 deletions.
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()
}
}
}
}
}

0 comments on commit 227d542

Please sign in to comment.