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 #153 from Z-Jais/master
Browse files Browse the repository at this point in the history
Update & fix bugs
  • Loading branch information
Ziedelth authored Sep 17, 2023
2 parents 3cb069c + 5c6601c commit 2a8be09
Show file tree
Hide file tree
Showing 50 changed files with 443 additions and 1,256 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pull_requests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Set up JDK 11
uses: actions/setup-java@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Set up JDK 11
uses: actions/setup-java@v3
Expand Down
12 changes: 6 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,6 @@
<artifactId>ktor-server-netty-jvm</artifactId>
<version>${ktor_version}</version>
</dependency>
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-server-websockets-jvm</artifactId>
<version>${ktor_version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
Expand Down Expand Up @@ -142,6 +137,11 @@
<artifactId>firebase-admin</artifactId>
<version>9.2.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>32.0.0-jre</version>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -229,7 +229,7 @@
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.9.1.2184</version>
<version>3.10.0.2594</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
Expand Down
2 changes: 0 additions & 2 deletions src/main/kotlin/fr/ziedelth/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import fr.ziedelth.plugins.configureHTTP
import fr.ziedelth.plugins.configureRouting
import fr.ziedelth.utils.Database
import fr.ziedelth.utils.ImageCache
import fr.ziedelth.utils.RequestCache
import fr.ziedelth.utils.plugins.PluginManager
import io.ktor.server.application.*
import io.ktor.server.engine.*
Expand Down Expand Up @@ -46,7 +45,6 @@ fun main(args: Array<String>) {
ListenerManager()
} else if (line == "invalid-cache") {
ImageCache.invalidCache(database)
RequestCache.clear()
}
}
}.start()
Expand Down
7 changes: 7 additions & 0 deletions src/main/kotlin/fr/ziedelth/caches/AnimeCacheKey.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fr.ziedelth.caches

import java.util.*

open class AnimeCacheKey(
open val anime: UUID
)
18 changes: 18 additions & 0 deletions src/main/kotlin/fr/ziedelth/caches/CountryCacheKey.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package fr.ziedelth.caches

open class CountryCacheKey(
open val tag: String
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is CountryCacheKey) return false

if (tag != other.tag) return false

return true
}

override fun hashCode(): Int {
return tag.hashCode()
}
}
6 changes: 6 additions & 0 deletions src/main/kotlin/fr/ziedelth/caches/DayCountryCacheKey.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package fr.ziedelth.caches

data class DayCountryCacheKey(
val day: Int,
override val tag: String,
) : CountryCacheKey(tag)
9 changes: 9 additions & 0 deletions src/main/kotlin/fr/ziedelth/caches/PaginationAnimeCacheKey.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package fr.ziedelth.caches

import java.util.*

data class PaginationAnimeCacheKey(
val page: Int,
val limit: Int,
override val anime: UUID,
) : AnimeCacheKey(anime)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fr.ziedelth.caches

data class PaginationCountryCacheKey(
val page: Int,
val limit: Int,
override val tag: String,
) : CountryCacheKey(tag)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package fr.ziedelth.caches

import java.util.*

data class PaginationSimulcastCountryCacheKey(
val page: Int,
val limit: Int,
val simulcast: UUID,
override val tag: String,
) : CountryCacheKey(tag)
62 changes: 10 additions & 52 deletions src/main/kotlin/fr/ziedelth/controllers/AbstractController.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package fr.ziedelth.controllers

import com.google.gson.Gson
import fr.ziedelth.repositories.IPageRepository
import fr.ziedelth.utils.Constant
import fr.ziedelth.utils.Decoder
import fr.ziedelth.utils.ImageCache
import fr.ziedelth.utils.RequestCache
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 io.ktor.util.pipeline.*
Expand All @@ -19,10 +16,19 @@ const val UNKNOWN_MESSAGE_ERROR = "Unknown error"
const val MISSING_PARAMETERS_MESSAGE_ERROR = "Missing parameters"

open class IController<T : Serializable>(val prefix: String) {
data class FilterData(
val animes: List<UUID> = listOf(),
val episodes: List<UUID> = listOf(),
val episodeTypes: List<UUID> = listOf(),
val langTypes: List<UUID> = listOf(),
)

val entityName: String =
((javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<*>).simpleName
val uuidRequest: UUID = UUID.randomUUID()

fun decode(watchlist: String) = Constant.gson.fromJson(Decoder.fromGzip(watchlist), FilterData::class.java)

fun PipelineContext<Unit, ApplicationCall>.getPageAndLimit(): Pair<Int, Int> {
val page = call.parameters["page"]!!.toIntOrNull() ?: throw IllegalArgumentException("Page is not valid")
val limit = call.parameters["limit"]!!.toIntOrNull() ?: throw IllegalArgumentException("Limit is not valid")
Expand All @@ -38,54 +44,6 @@ open class IController<T : Serializable>(val prefix: String) {
call.respond(HttpStatusCode.InternalServerError, e.message ?: UNKNOWN_MESSAGE_ERROR)
}

protected fun Route.getWithPage(iPageRepository: IPageRepository<T>) {
get("/country/{country}/page/{page}/limit/{limit}") {
try {
val country = call.parameters["country"]!!
val (page, limit) = getPageAndLimit()
println("GET $prefix/country/$country/page/$page/limit/$limit")
val request = RequestCache.get(uuidRequest, country, page, limit)

if (request == null || request.isExpired()) {
val list = iPageRepository.getByPage(country, page, limit)
request?.update(list) ?: RequestCache.put(uuidRequest, country, page, limit, value = list)
}

call.respond(RequestCache.get(uuidRequest, country, page, limit)!!.value!!)
} catch (e: Exception) {
printError(call, e)
}
}
}

protected fun Route.getAnimeWithPage(iPageRepository: IPageRepository<T>) {
get("/anime/{uuid}/page/{page}/limit/{limit}") {
try {
val animeUuid = call.parameters["uuid"]!!
val (page, limit) = getPageAndLimit()
println("GET $prefix/anime/$animeUuid/page/$page/limit/$limit")
call.respond(iPageRepository.getByPageWithAnime(UUID.fromString(animeUuid), page, limit))
} catch (e: Exception) {
printError(call, e)
}
}
}

protected fun Route.getWatchlistWithPage(iPageRepository: IPageRepository<T>) {
post("/watchlist/page/{page}/limit/{limit}") {
try {
val watchlist = call.receive<String>()
val (page, limit) = getPageAndLimit()
println("POST $prefix/watchlist/page/$page/limit/$limit")
val dataFromGzip =
Gson().fromJson(Decoder.fromGzip(watchlist), Array<String>::class.java).map { UUID.fromString(it) }
call.respond(iPageRepository.getByPageWithList(dataFromGzip, page, limit))
} catch (e: Exception) {
printError(call, e)
}
}
}

fun Route.getAttachment() {
get("/attachment/{uuid}") {
val string = call.parameters["uuid"]!!
Expand Down
Loading

0 comments on commit 2a8be09

Please sign in to comment.