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 #63 from Z-Jais/master
Browse files Browse the repository at this point in the history
1.0.8
  • Loading branch information
Ziedelth authored Jan 2, 2023
2 parents 98a790b + 3324c3c commit a0b686b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<properties>
<ktor_version>2.2.1</ktor_version>
<kotlin.code.style>official</kotlin.code.style>
<kotlin_version>1.7.21</kotlin_version>
<kotlin_version>1.8.0</kotlin_version>
<kotlin.compiler.jvmTarget>11</kotlin.compiler.jvmTarget>
<logback_version>1.4.5</logback_version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -130,7 +130,7 @@
<dependency>
<groupId>org.openpnp</groupId>
<artifactId>opencv</artifactId>
<version>4.5.5-1</version>
<version>4.6.0-0</version>
</dependency>
<dependency>
<groupId>org.pf4j</groupId>
Expand Down
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 a0b686b

Please sign in to comment.