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 #81 from Z-Jais/80-add-filter-on-watchlist-new-end…
Browse files Browse the repository at this point in the history
…point

80 add filter on watchlist new endpoint
  • Loading branch information
Ziedelth authored Feb 10, 2023
2 parents 8c6cbcf + 4bf1f5d commit ad019e2
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 10 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<properties>
<ktor_version>2.2.3</ktor_version>
<kotlin.code.style>official</kotlin.code.style>
<kotlin_version>1.8.10</kotlin_version>
<kotlin.version>1.8.10</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 @@ -40,7 +40,7 @@
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test-junit5</artifactId>
<version>${kotlin_version}</version>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -157,7 +157,7 @@
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin_version}</version>
<version>${kotlin.version}</version>
<configuration>
<jvmTarget>${kotlin.compiler.jvmTarget}</jvmTarget>
</configuration>
Expand Down
12 changes: 7 additions & 5 deletions src/main/kotlin/fr/ziedelth/Application.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package fr.ziedelth

import io.ktor.server.application.Application
import fr.ziedelth.listeners.ListenerManager
import fr.ziedelth.plugins.configureHTTP
import fr.ziedelth.plugins.configureRouting
import fr.ziedelth.utils.Database
import fr.ziedelth.utils.ImageCache
import fr.ziedelth.utils.plugins.PluginManager
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import jakarta.persistence.Tuple
Expand All @@ -33,18 +33,20 @@ fun main(args: Array<String>) {
// Get all platforms from database
val platforms =
session.createQuery("SELECT uuid, image FROM Platform WHERE image LIKE 'http%'", Tuple::class.java).list()
platforms.forEach { ImageCache.cachingNetworkImage(it[0] as UUID, it[1] as String) }
println("Platforms : ${platforms.size}")

// Get all animes from database
val animes = session.createQuery("SELECT uuid, image FROM Anime", Tuple::class.java).list()
animes.forEach { ImageCache.cachingNetworkImage(it[0] as UUID, it[1] as String) }
println("Animes : ${animes.size}")

// Get all episodes from database
val episodes = session.createQuery("SELECT uuid, image FROM Episode", Tuple::class.java).list()
episodes.forEach { ImageCache.cachingNetworkImage(it[0] as UUID, it[1] as String) }
println("Episodes : ${episodes.size}")

if (!isDebug) {
platforms.forEach { ImageCache.cachingNetworkImage(it[0] as UUID, it[1] as String) }
animes.forEach { ImageCache.cachingNetworkImage(it[0] as UUID, it[1] as String) }
episodes.forEach { ImageCache.cachingNetworkImage(it[0] as UUID, it[1] as String) }
}
} catch (e: Exception) {
e.printStackTrace()
} finally {
Expand Down
34 changes: 34 additions & 0 deletions src/main/kotlin/fr/ziedelth/controllers/EpisodeController.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package fr.ziedelth.controllers

import com.google.gson.Gson
import com.google.gson.JsonObject
import fr.ziedelth.entities.Episode
import fr.ziedelth.entities.Simulcast
import fr.ziedelth.entities.isNullOrNotValid
import fr.ziedelth.events.EpisodesReleaseEvent
import fr.ziedelth.repositories.*
import fr.ziedelth.utils.Decoder
import fr.ziedelth.utils.ImageCache
import fr.ziedelth.utils.plugins.PluginManager
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import java.util.*

class EpisodeController(
private val platformRepository: PlatformRepository,
Expand All @@ -26,11 +30,41 @@ class EpisodeController(
getWithPage(episodeRepository)
getAnimeWithPage(episodeRepository)
getWatchlistWithPage(episodeRepository)
getWatchlistFilter()
getAttachment()
create()
}
}

private fun Route.getWatchlistFilter() {
post("/watchlist_filter/page/{page}/limit/{limit}") {
try {
val watchlist = call.receive<String>()
val (page, limit) = getPageAndLimit()
println("POST $prefix/watchlist_filter/page/$page/limit/$limit")
val dataFromGzip = Gson().fromJson(Decoder.fromGzip(watchlist), JsonObject::class.java)

val animes = dataFromGzip.getAsJsonArray("animes").map { UUID.fromString(it.asString) }
val episodes = dataFromGzip.getAsJsonArray("episodes").map { UUID.fromString(it.asString) }
val episodeTypes = dataFromGzip.getAsJsonArray("episodeTypes").map { UUID.fromString(it.asString) }
val langTypes = dataFromGzip.getAsJsonArray("langTypes").map { UUID.fromString(it.asString) }

call.respond(
episodeRepository.getByPageWithListFilter(
animes,
episodes,
episodeTypes,
langTypes,
page,
limit
)
)
} catch (e: Exception) {
printError(call, e)
}
}
}

private fun merge(episode: Episode) {
episode.platform = platformRepository.find(episode.platform!!.uuid) ?: throw Exception("Platform not found")
episode.anime = animeRepository.find(episode.anime!!.uuid) ?: throw Exception("Anime not found")
Expand Down
36 changes: 34 additions & 2 deletions src/main/kotlin/fr/ziedelth/repositories/EpisodeRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import fr.ziedelth.utils.Database
import org.hibernate.Session
import java.util.*

private const val ORDER =
"ORDER BY releaseDate DESC, anime.name, season DESC, number DESC, episodeType.name, langType.name"

class EpisodeRepository(session: () -> Session = { Database.getSession() }) : AbstractRepository<Episode>(session),
IPageRepository<Episode> {
override fun getByPage(tag: String, page: Int, limit: Int): List<Episode> {
return super.getByPage(
page,
limit,
"FROM Episode WHERE anime.country.tag = :tag ORDER BY releaseDate DESC, anime.name, season DESC, number DESC, episodeType.name, langType.name",
"FROM Episode WHERE anime.country.tag = :tag $ORDER",
"tag" to tag
)
}
Expand All @@ -29,11 +32,40 @@ class EpisodeRepository(session: () -> Session = { Database.getSession() }) : Ab
return super.getByPage(
page,
limit,
"FROM Episode WHERE anime.uuid IN :list ORDER BY releaseDate DESC, anime.name, season DESC, number DESC, episodeType.name, langType.name",
"FROM Episode WHERE anime.uuid IN :list $ORDER",
"list" to list
)
}

fun getByPageWithListFilter(
animes: List<UUID>,
episodes: List<UUID>,
episodeTypes: List<UUID>,
langTypes: List<UUID>,
page: Int,
limit: Int
): List<Episode> {
if (episodes.isEmpty())
return super.getByPage(
page,
limit,
"FROM Episode WHERE anime.uuid IN :animes AND episodeType.uuid IN :episodeTypes AND langType.uuid IN :langTypes $ORDER",
"animes" to animes,
"episodeTypes" to episodeTypes,
"langTypes" to langTypes
)

return super.getByPage(
page,
limit,
"FROM Episode WHERE uuid NOT IN :episodes AND anime.uuid IN :animes AND episodeType.uuid IN :episodeTypes AND langType.uuid IN :langTypes $ORDER",
"animes" to animes,
"episodes" to episodes,
"episodeTypes" to episodeTypes,
"langTypes" to langTypes
)
}

fun getLastNumber(episode: Episode): Int {
val session = getSession.invoke()
val query = session.createQuery(
Expand Down

0 comments on commit ad019e2

Please sign in to comment.