diff --git a/pom.xml b/pom.xml index 34ff74b..54c7623 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ 2.2.1 official - 1.7.21 + 1.8.0 11 1.4.5 UTF-8 @@ -130,7 +130,7 @@ org.openpnp opencv - 4.5.5-1 + 4.6.0-0 org.pf4j diff --git a/src/main/kotlin/fr/ziedelth/controllers/SimulcastController.kt b/src/main/kotlin/fr/ziedelth/controllers/SimulcastController.kt index 3a71116..d005f97 100644 --- a/src/main/kotlin/fr/ziedelth/controllers/SimulcastController.kt +++ b/src/main/kotlin/fr/ziedelth/controllers/SimulcastController.kt @@ -2,6 +2,7 @@ package fr.ziedelth.controllers import fr.ziedelth.entities.Simulcast import fr.ziedelth.repositories.SimulcastRepository +import io.ktor.http.* import io.ktor.server.application.* import io.ktor.server.response.* import io.ktor.server.routing.* @@ -11,9 +12,14 @@ class SimulcastController(private val simulcastRepository: SimulcastRepository) fun getRoutes(routing: Routing) { routing.route(prefix) { get("/country/{country}") { - val country = call.parameters["country"]!! - println("GET $prefix/country/$country") - call.respond(simulcastRepository.getAll(country)) + try { + val country = call.parameters["country"]!! + println("GET $prefix/country/$country") + call.respond(simulcastRepository.getAll(country)) + } catch (e: Exception) { + e.printStackTrace() + call.respond(HttpStatusCode.InternalServerError, e) + } } } } diff --git a/src/main/kotlin/fr/ziedelth/repositories/SimulcastRepository.kt b/src/main/kotlin/fr/ziedelth/repositories/SimulcastRepository.kt index 51d407c..863dcf4 100644 --- a/src/main/kotlin/fr/ziedelth/repositories/SimulcastRepository.kt +++ b/src/main/kotlin/fr/ziedelth/repositories/SimulcastRepository.kt @@ -1,20 +1,31 @@ package fr.ziedelth.repositories +import fr.ziedelth.entities.Anime +import fr.ziedelth.entities.Country import fr.ziedelth.entities.Simulcast import fr.ziedelth.utils.Database import org.hibernate.Session class SimulcastRepository(session: () -> Session = { Database.getSession() }) : AbstractRepository(session) { fun getAll(tag: String?): List { + val start = System.currentTimeMillis() + val session = getSession.invoke() - val query = session.createQuery( - "SELECT DISTINCT simulcasts FROM Anime WHERE country.tag = :tag", - Simulcast::class.java - ) - query.setParameter("tag", tag) - val list = query.list() + val criteriaBuilder = session.criteriaBuilder + val criteriaQuery = criteriaBuilder.createQuery(Simulcast::class.java) + val root = criteriaQuery.from(Anime::class.java) + criteriaQuery.select(root.get("simulcasts")).distinct(true) + criteriaQuery.where(criteriaBuilder.equal(root.get("country").get("tag"), tag)) + val list = session.createQuery(criteriaQuery).list() session.close() - return list + + // Sort by year and season started by "Winter", "Spring", "Summer", "Autumn" + val seasons = listOf("WINTER", "SPRING", "SUMMER", "AUTUMN") + val sorted = list.sortedWith(compareBy({ it.year }, { seasons.indexOf(it.season) })) + + val end = System.currentTimeMillis() + println("SimulcastRepository.getAll() took ${end - start}ms") + return sorted } fun findBySeasonAndYear(season: String, year: Int): Simulcast? {