From 6a782d3736535d02844ddbff48cba9f230ff209e Mon Sep 17 00:00:00 2001 From: Ziedelth Date: Mon, 2 Jan 2023 11:39:43 +0100 Subject: [PATCH] Sort simulcasts --- .../controllers/SimulcastController.kt | 12 ++++++--- .../repositories/SimulcastRepository.kt | 25 +++++++++++++------ 2 files changed, 27 insertions(+), 10 deletions(-) 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? {