diff --git a/src/main/kotlin/fr/ziedelth/controllers/CountryController.kt b/src/main/kotlin/fr/ziedelth/controllers/CountryController.kt index f8a4945..125c50e 100644 --- a/src/main/kotlin/fr/ziedelth/controllers/CountryController.kt +++ b/src/main/kotlin/fr/ziedelth/controllers/CountryController.kt @@ -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("/countries") { +class CountryController(private val service: CountryService) : IController("/countries") { fun getRoutes(routing: Routing) { routing.route(prefix) { getAll() @@ -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()) } } @@ -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) } diff --git a/src/main/kotlin/fr/ziedelth/controllers/EpisodeController.kt b/src/main/kotlin/fr/ziedelth/controllers/EpisodeController.kt index 4468092..b637951 100644 --- a/src/main/kotlin/fr/ziedelth/controllers/EpisodeController.kt +++ b/src/main/kotlin/fr/ziedelth/controllers/EpisodeController.kt @@ -67,12 +67,13 @@ class EpisodeController( private suspend fun filterWatchlistByPageAndLimit( pipelineContext: PipelineContext, - episodeController: EpisodeController + episodeController: EpisodeController, + routePrefix: String, ) { try { val watchlist = pipelineContext.call.receive() 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)) @@ -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") } } @@ -125,6 +126,12 @@ class EpisodeController( try { val episodes = call.receive>().filter { !service.repository.exists("hash", it.hash!!) } + + if (episodes.isEmpty()) { + call.respond(HttpStatusCode.NoContent, "All requested episodes already exists!") + return@post + } + val savedEpisodes = mutableListOf() episodes.forEach { diff --git a/src/main/kotlin/fr/ziedelth/controllers/EpisodeTypeController.kt b/src/main/kotlin/fr/ziedelth/controllers/EpisodeTypeController.kt index a264eac..165708c 100644 --- a/src/main/kotlin/fr/ziedelth/controllers/EpisodeTypeController.kt +++ b/src/main/kotlin/fr/ziedelth/controllers/EpisodeTypeController.kt @@ -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("/episodetypes") { fun getRoutes(routing: Routing) { routing.route(prefix) { @@ -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()) } } @@ -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) } diff --git a/src/main/kotlin/fr/ziedelth/controllers/LangTypeController.kt b/src/main/kotlin/fr/ziedelth/controllers/LangTypeController.kt index 42822e3..b03980b 100644 --- a/src/main/kotlin/fr/ziedelth/controllers/LangTypeController.kt +++ b/src/main/kotlin/fr/ziedelth/controllers/LangTypeController.kt @@ -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("/langtypes") { +class LangTypeController(private val service: LangTypeService) : IController("/langtypes") { fun getRoutes(routing: Routing) { routing.route(prefix) { getAll() @@ -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()) } } @@ -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) } diff --git a/src/main/kotlin/fr/ziedelth/plugins/Routing.kt b/src/main/kotlin/fr/ziedelth/plugins/Routing.kt index 642054e..51458cb 100644 --- a/src/main/kotlin/fr/ziedelth/plugins/Routing.kt +++ b/src/main/kotlin/fr/ziedelth/plugins/Routing.kt @@ -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.* @@ -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, diff --git a/src/main/kotlin/fr/ziedelth/repositories/EpisodeRepository.kt b/src/main/kotlin/fr/ziedelth/repositories/EpisodeRepository.kt index 41f3637..76568bc 100644 --- a/src/main/kotlin/fr/ziedelth/repositories/EpisodeRepository.kt +++ b/src/main/kotlin/fr/ziedelth/repositories/EpisodeRepository.kt @@ -23,7 +23,7 @@ class EpisodeRepository(database: Database) : AbstractRepository(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 ) } diff --git a/src/main/kotlin/fr/ziedelth/services/CountryService.kt b/src/main/kotlin/fr/ziedelth/services/CountryService.kt new file mode 100644 index 0000000..8b76621 --- /dev/null +++ b/src/main/kotlin/fr/ziedelth/services/CountryService.kt @@ -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>() { + override fun load(key: String): List { + println("Updating country cache") + return repository.getAll() + } + }) + + fun invalidateAll() { + println("Invalidate all country cache") + loadingCache.invalidateAll() + } + + fun getAll(): List = loadingCache.getUnchecked("") +} \ No newline at end of file diff --git a/src/main/kotlin/fr/ziedelth/services/EpisodeTypeService.kt b/src/main/kotlin/fr/ziedelth/services/EpisodeTypeService.kt new file mode 100644 index 0000000..cbfd938 --- /dev/null +++ b/src/main/kotlin/fr/ziedelth/services/EpisodeTypeService.kt @@ -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>() { + override fun load(key: String): List { + println("Updating episode type cache") + return repository.getAll() + } + }) + + fun invalidateAll() { + println("Invalidate all episode type cache") + loadingCache.invalidateAll() + } + + fun getAll(): List = loadingCache.getUnchecked("") +} \ No newline at end of file diff --git a/src/main/kotlin/fr/ziedelth/services/LangTypeService.kt b/src/main/kotlin/fr/ziedelth/services/LangTypeService.kt new file mode 100644 index 0000000..6b47dfb --- /dev/null +++ b/src/main/kotlin/fr/ziedelth/services/LangTypeService.kt @@ -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>() { + override fun load(key: String): List { + println("Updating lang type cache") + return repository.getAll() + } + }) + + fun invalidateAll() { + println("Invalidate all lang type cache") + loadingCache.invalidateAll() + } + + fun getAll(): List = loadingCache.getUnchecked("") +} \ No newline at end of file diff --git a/src/test/kotlin/fr/ziedelth/plugins/RoutingTest.kt b/src/test/kotlin/fr/ziedelth/plugins/RoutingTest.kt index 82170fe..9c45684 100644 --- a/src/test/kotlin/fr/ziedelth/plugins/RoutingTest.kt +++ b/src/test/kotlin/fr/ziedelth/plugins/RoutingTest.kt @@ -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.* @@ -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,