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 #62 from Z-Jais/61-sort-simulcast-by-latest-added
Browse files Browse the repository at this point in the history
Sort simulcasts
  • Loading branch information
Ziedelth authored Jan 2, 2023
2 parents 5718a41 + 6a782d3 commit 3324c3c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
12 changes: 9 additions & 3 deletions src/main/kotlin/fr/ziedelth/controllers/SimulcastController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand All @@ -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)
}
}
}
}
Expand Down
25 changes: 18 additions & 7 deletions src/main/kotlin/fr/ziedelth/repositories/SimulcastRepository.kt
Original file line number Diff line number Diff line change
@@ -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<Simulcast>(session) {
fun getAll(tag: String?): List<Simulcast> {
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>("country").get<String>("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? {
Expand Down

0 comments on commit 3324c3c

Please sign in to comment.