Skip to content
This repository has been archived by the owner on Mar 28, 2024. It is now read-only.

Commit

Permalink
Introduced Services layer and improved episode filtering logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziedelth committed Sep 26, 2023
1 parent a0e83ee commit c219b14
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 33 deletions.
13 changes: 7 additions & 6 deletions src/main/kotlin/fr/ziedelth/controllers/CountryController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package fr.ziedelth.controllers

import fr.ziedelth.entities.Country
import fr.ziedelth.entities.isNullOrNotValid
import fr.ziedelth.repositories.CountryRepository
import fr.ziedelth.services.CountryService
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*

class CountryController(private val countryRepository: CountryRepository) : IController<Country>("/countries") {
class CountryController(private val service: CountryService) : IController<Country>("/countries") {
fun getRoutes(routing: Routing) {
routing.route(prefix) {
getAll()
Expand All @@ -20,7 +20,7 @@ class CountryController(private val countryRepository: CountryRepository) : ICon
fun Route.getAll() {
get {
println("GET $prefix")
call.respond(countryRepository.getAll())
call.respond(service.getAll())
}
}

Expand All @@ -36,17 +36,18 @@ class CountryController(private val countryRepository: CountryRepository) : ICon
return@post
}

if (countryRepository.exists("tag", country.tag)) {
if (service.repository.exists("tag", country.tag)) {
call.respond(HttpStatusCode.Conflict, "$entityName already exists")
return@post
}

if (countryRepository.exists("name", country.name)) {
if (service.repository.exists("name", country.name)) {
call.respond(HttpStatusCode.Conflict, "$entityName already exists")
return@post
}

call.respond(HttpStatusCode.Created, countryRepository.save(country))
call.respond(HttpStatusCode.Created, service.repository.save(country))
service.invalidateAll()
} catch (e: Exception) {
printError(call, e)
}
Expand Down
15 changes: 11 additions & 4 deletions src/main/kotlin/fr/ziedelth/controllers/EpisodeController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,13 @@ class EpisodeController(

private suspend fun filterWatchlistByPageAndLimit(
pipelineContext: PipelineContext<Unit, ApplicationCall>,
episodeController: EpisodeController
episodeController: EpisodeController,
routePrefix: String,
) {
try {
val watchlist = pipelineContext.call.receive<String>()
val (page, limit) = pipelineContext.getPageAndLimit()
println("POST $prefix/watchlist_filter/page/$page/limit/$limit")
println("POST $prefix/${routePrefix}/page/$page/limit/$limit")
val filterData = decode(watchlist)

pipelineContext.call.respond(service.repository.getByPageWithListFilter(filterData, page, limit))
Expand All @@ -83,14 +84,14 @@ class EpisodeController(

private fun Route.getWatchlist() {
post("/watchlist/page/{page}/limit/{limit}") {
filterWatchlistByPageAndLimit(this, this@EpisodeController)
filterWatchlistByPageAndLimit(this, this@EpisodeController, "watchlist")
}
}

@Deprecated(message = "Use /watchlist as replace")
private fun Route.getWatchlistFilter() {
post("/watchlist_filter/page/{page}/limit/{limit}") {
filterWatchlistByPageAndLimit(this, this@EpisodeController)
filterWatchlistByPageAndLimit(this, this@EpisodeController, "watchlist_filter")
}
}

Expand Down Expand Up @@ -125,6 +126,12 @@ class EpisodeController(

try {
val episodes = call.receive<List<Episode>>().filter { !service.repository.exists("hash", it.hash!!) }

if (episodes.isEmpty()) {
call.respond(HttpStatusCode.NoContent, "All requested episodes already exists!")
return@post
}

val savedEpisodes = mutableListOf<Episode>()

episodes.forEach {
Expand Down
11 changes: 6 additions & 5 deletions src/main/kotlin/fr/ziedelth/controllers/EpisodeTypeController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package fr.ziedelth.controllers

import fr.ziedelth.entities.EpisodeType
import fr.ziedelth.entities.isNullOrNotValid
import fr.ziedelth.repositories.EpisodeTypeRepository
import fr.ziedelth.services.EpisodeTypeService
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*

class EpisodeTypeController(private val episodeTypeRepository: EpisodeTypeRepository) :
class EpisodeTypeController(private val service: EpisodeTypeService) :
IController<EpisodeType>("/episodetypes") {
fun getRoutes(routing: Routing) {
routing.route(prefix) {
Expand All @@ -21,7 +21,7 @@ class EpisodeTypeController(private val episodeTypeRepository: EpisodeTypeReposi
fun Route.getAll() {
get {
println("GET $prefix")
call.respond(episodeTypeRepository.getAll())
call.respond(service.getAll())
}
}

Expand All @@ -37,12 +37,13 @@ class EpisodeTypeController(private val episodeTypeRepository: EpisodeTypeReposi
return@post
}

if (episodeTypeRepository.exists("name", episodeType.name)) {
if (service.repository.exists("name", episodeType.name)) {
call.respond(HttpStatusCode.Conflict, "$entityName already exists")
return@post
}

call.respond(HttpStatusCode.Created, episodeTypeRepository.save(episodeType))
call.respond(HttpStatusCode.Created, service.repository.save(episodeType))
service.invalidateAll()
} catch (e: Exception) {
printError(call, e)
}
Expand Down
11 changes: 6 additions & 5 deletions src/main/kotlin/fr/ziedelth/controllers/LangTypeController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package fr.ziedelth.controllers

import fr.ziedelth.entities.LangType
import fr.ziedelth.entities.isNullOrNotValid
import fr.ziedelth.repositories.LangTypeRepository
import fr.ziedelth.services.LangTypeService
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*

class LangTypeController(private val langTypeRepository: LangTypeRepository) : IController<LangType>("/langtypes") {
class LangTypeController(private val service: LangTypeService) : IController<LangType>("/langtypes") {
fun getRoutes(routing: Routing) {
routing.route(prefix) {
getAll()
Expand All @@ -20,7 +20,7 @@ class LangTypeController(private val langTypeRepository: LangTypeRepository) : I
fun Route.getAll() {
get {
println("GET $prefix")
call.respond(langTypeRepository.getAll())
call.respond(service.getAll())
}
}

Expand All @@ -36,12 +36,13 @@ class LangTypeController(private val langTypeRepository: LangTypeRepository) : I
return@post
}

if (langTypeRepository.exists("name", langType.name)) {
if (service.repository.exists("name", langType.name)) {
call.respond(HttpStatusCode.Conflict, "$entityName already exists")
return@post
}

call.respond(HttpStatusCode.Created, langTypeRepository.save(langType))
call.respond(HttpStatusCode.Created, service.repository.save(langType))
service.invalidateAll()
} catch (e: Exception) {
printError(call, e)
}
Expand Down
13 changes: 7 additions & 6 deletions src/main/kotlin/fr/ziedelth/plugins/Routing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package fr.ziedelth.plugins

import fr.ziedelth.controllers.*
import fr.ziedelth.repositories.*
import fr.ziedelth.services.AnimeService
import fr.ziedelth.services.EpisodeService
import fr.ziedelth.services.SimulcastService
import fr.ziedelth.services.*
import fr.ziedelth.utils.Database
import io.ktor.server.application.*
import io.ktor.server.routing.*
Expand All @@ -20,17 +18,20 @@ fun Application.configureRouting(database: Database) {
val langTypeRepository = LangTypeRepository(database)
val episodeRepository = EpisodeRepository(database)

val countryService = CountryService(countryRepository)
val episodeTypeService = EpisodeTypeService(episodeTypeRepository)
val langTypeService = LangTypeService(langTypeRepository)
val simulcastService = SimulcastService(simulcastRepository)
val animeService = AnimeService(animeRepository)
val episodeService = EpisodeService(episodeRepository)

CountryController(countryRepository).getRoutes(this)
CountryController(countryService).getRoutes(this)
PlatformController(platformRepository).getRoutes(this)
SimulcastController(simulcastService).getRoutes(this)
GenreController(genreRepository).getRoutes(this)
AnimeController(countryRepository, animeService, episodeService).getRoutes(this)
EpisodeTypeController(episodeTypeRepository).getRoutes(this)
LangTypeController(langTypeRepository).getRoutes(this)
EpisodeTypeController(episodeTypeService).getRoutes(this)
LangTypeController(langTypeService).getRoutes(this)
EpisodeController(
platformRepository,
animeService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class EpisodeRepository(database: Database) : AbstractRepository<Episode>(databa
return super.getByPage(
page,
limit,
"FROM Episode WHERE anime.uuid = :uuid ORDER BY season DESC, number DESC, episodeType.name, langType.name",
"FROM Episode WHERE anime.uuid = :uuid $ORDER",
"uuid" to uuid
)
}
Expand Down
23 changes: 23 additions & 0 deletions src/main/kotlin/fr/ziedelth/services/CountryService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package fr.ziedelth.services

import com.google.common.cache.CacheBuilder
import com.google.common.cache.CacheLoader
import fr.ziedelth.entities.Country
import fr.ziedelth.repositories.CountryRepository

class CountryService(val repository: CountryRepository) {
private val loadingCache = CacheBuilder.newBuilder()
.build(object : CacheLoader<String, List<Country>>() {
override fun load(key: String): List<Country> {
println("Updating country cache")
return repository.getAll()
}
})

fun invalidateAll() {
println("Invalidate all country cache")
loadingCache.invalidateAll()
}

fun getAll(): List<Country> = loadingCache.getUnchecked("")
}
23 changes: 23 additions & 0 deletions src/main/kotlin/fr/ziedelth/services/EpisodeTypeService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package fr.ziedelth.services

import com.google.common.cache.CacheBuilder
import com.google.common.cache.CacheLoader
import fr.ziedelth.entities.EpisodeType
import fr.ziedelth.repositories.EpisodeTypeRepository

class EpisodeTypeService(val repository: EpisodeTypeRepository) {
private val loadingCache = CacheBuilder.newBuilder()
.build(object : CacheLoader<String, List<EpisodeType>>() {
override fun load(key: String): List<EpisodeType> {
println("Updating episode type cache")
return repository.getAll()
}
})

fun invalidateAll() {
println("Invalidate all episode type cache")
loadingCache.invalidateAll()
}

fun getAll(): List<EpisodeType> = loadingCache.getUnchecked("")
}
23 changes: 23 additions & 0 deletions src/main/kotlin/fr/ziedelth/services/LangTypeService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package fr.ziedelth.services

import com.google.common.cache.CacheBuilder
import com.google.common.cache.CacheLoader
import fr.ziedelth.entities.LangType
import fr.ziedelth.repositories.LangTypeRepository

class LangTypeService(val repository: LangTypeRepository) {
private val loadingCache = CacheBuilder.newBuilder()
.build(object : CacheLoader<String, List<LangType>>() {
override fun load(key: String): List<LangType> {
println("Updating lang type cache")
return repository.getAll()
}
})

fun invalidateAll() {
println("Invalidate all lang type cache")
loadingCache.invalidateAll()
}

fun getAll(): List<LangType> = loadingCache.getUnchecked("")
}
13 changes: 7 additions & 6 deletions src/test/kotlin/fr/ziedelth/plugins/RoutingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package fr.ziedelth.plugins

import fr.ziedelth.controllers.*
import fr.ziedelth.repositories.*
import fr.ziedelth.services.AnimeService
import fr.ziedelth.services.EpisodeService
import fr.ziedelth.services.SimulcastService
import fr.ziedelth.services.*
import fr.ziedelth.utils.DatabaseTest
import io.ktor.server.application.*
import io.ktor.server.routing.*
Expand All @@ -20,19 +18,22 @@ val episodeTypeRepository = EpisodeTypeRepository(databaseTest)
val langTypeRepository = LangTypeRepository(databaseTest)
val episodeRepository = EpisodeRepository(databaseTest)

val countryService = CountryService(countryRepository)
val episodeTypeService = EpisodeTypeService(episodeTypeRepository)
val langTypeService = LangTypeService(langTypeRepository)
val simulcastService = SimulcastService(simulcastRepository)
val animeService = AnimeService(animeRepository)
val episodeService = EpisodeService(episodeRepository)

fun Application.configureRoutingTest() {
routing {
CountryController(countryRepository).getRoutes(this)
CountryController(countryService).getRoutes(this)
PlatformController(platformRepository).getRoutes(this)
SimulcastController(simulcastService).getRoutes(this)
GenreController(genreRepository).getRoutes(this)
AnimeController(countryRepository, animeService, episodeService).getRoutes(this)
EpisodeTypeController(episodeTypeRepository).getRoutes(this)
LangTypeController(langTypeRepository).getRoutes(this)
EpisodeTypeController(episodeTypeService).getRoutes(this)
LangTypeController(langTypeService).getRoutes(this)
EpisodeController(
platformRepository,
animeService,
Expand Down

0 comments on commit c219b14

Please sign in to comment.