From 64ed7f983954ace0efd22dcd5775d63ac7c4c4b5 Mon Sep 17 00:00:00 2001 From: Ziedelth Date: Sat, 30 Sep 2023 20:56:07 +0200 Subject: [PATCH] Improve episode update by adding comparison method in Simulcast --- .../ziedelth/controllers/EpisodeController.kt | 30 ++++-- .../kotlin/fr/ziedelth/entities/Simulcast.kt | 12 +++ .../controllers/EpisodeControllerTest.kt | 95 ++++++++++++++++++- 3 files changed, 129 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/fr/ziedelth/controllers/EpisodeController.kt b/src/main/kotlin/fr/ziedelth/controllers/EpisodeController.kt index 17e21ae..cbd3740 100644 --- a/src/main/kotlin/fr/ziedelth/controllers/EpisodeController.kt +++ b/src/main/kotlin/fr/ziedelth/controllers/EpisodeController.kt @@ -125,21 +125,37 @@ class EpisodeController : AttachmentController("/episodes") { episode.number = episodeRepository.getLastNumber(episode) + 1 } - val releaseDatePlus10Days = CalendarConverter.toUTCCalendar(episode.releaseDate) - releaseDatePlus10Days.add(Calendar.DAY_OF_YEAR, 10) - val tmpSimulcast = Simulcast.getSimulcastFrom(episode.releaseDate) - val tmpNextSimulcast = Simulcast.getSimulcastFrom(releaseDatePlus10Days.toISO8601()) - - if (episode.number == 1 && tmpSimulcast != tmpNextSimulcast) { + val releaseDatePlus15Days = CalendarConverter.toUTCCalendar(episode.releaseDate) + releaseDatePlus15Days.add(Calendar.DAY_OF_YEAR, 15) + val releaseDateMinus15Days = CalendarConverter.toUTCCalendar(episode.releaseDate) + releaseDateMinus15Days.add(Calendar.DAY_OF_YEAR, -15) + val tmpPreviousSimulcast = Simulcast.getSimulcastFrom(releaseDateMinus15Days.toISO8601()) + val tmpCurrentSimulcast = Simulcast.getSimulcastFrom(episode.releaseDate) + val tmpNextSimulcast = Simulcast.getSimulcastFrom(releaseDatePlus15Days.toISO8601()) + + println(tmpPreviousSimulcast) + println(tmpCurrentSimulcast) + println(tmpNextSimulcast) + + if (episode.number!! <= 1 && !tmpCurrentSimulcast.equalsWithoutUUID(tmpNextSimulcast)) { val simulcast = simulcastRepository.findBySeasonAndYear(tmpNextSimulcast.season!!, tmpNextSimulcast.year!!) ?: tmpNextSimulcast + if (episode.anime!!.simulcasts.isEmpty() || episode.anime!!.simulcasts.none { it.uuid == simulcast.uuid }) { + episode.anime!!.simulcasts.add(simulcast) + } + } else if (episode.number!! > 1 && !tmpCurrentSimulcast.equalsWithoutUUID(tmpPreviousSimulcast)) { + val simulcast = + simulcastRepository.findBySeasonAndYear(tmpPreviousSimulcast.season!!, tmpPreviousSimulcast.year!!) + ?: tmpPreviousSimulcast + if (episode.anime!!.simulcasts.isEmpty() || episode.anime!!.simulcasts.none { it.uuid == simulcast.uuid }) { episode.anime!!.simulcasts.add(simulcast) } } else { val simulcast = - simulcastRepository.findBySeasonAndYear(tmpSimulcast.season!!, tmpSimulcast.year!!) ?: tmpSimulcast + simulcastRepository.findBySeasonAndYear(tmpCurrentSimulcast.season!!, tmpCurrentSimulcast.year!!) + ?: tmpCurrentSimulcast if (episode.anime!!.simulcasts.isEmpty() || episode.anime!!.simulcasts.none { it.uuid == simulcast.uuid }) { episode.anime!!.simulcasts.add(simulcast) diff --git a/src/main/kotlin/fr/ziedelth/entities/Simulcast.kt b/src/main/kotlin/fr/ziedelth/entities/Simulcast.kt index ac1af79..1e91ce1 100644 --- a/src/main/kotlin/fr/ziedelth/entities/Simulcast.kt +++ b/src/main/kotlin/fr/ziedelth/entities/Simulcast.kt @@ -39,4 +39,16 @@ class Simulcast( override fun toString(): String { return "Simulcast(uuid=$uuid, season=$season, year=$year)" } + + fun equalsWithoutUUID(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as Simulcast + + if (season != other.season) return false + if (year != other.year) return false + + return true + } } diff --git a/src/test/kotlin/fr/ziedelth/controllers/EpisodeControllerTest.kt b/src/test/kotlin/fr/ziedelth/controllers/EpisodeControllerTest.kt index e5c923f..85e9b77 100644 --- a/src/test/kotlin/fr/ziedelth/controllers/EpisodeControllerTest.kt +++ b/src/test/kotlin/fr/ziedelth/controllers/EpisodeControllerTest.kt @@ -126,6 +126,8 @@ internal class EpisodeControllerTest : AbstractAPITest() { val episodeType = episodeTypeRepository.getAll().last() val langType = langTypeRepository.getAll().first() + val date = "2023-09-10T00:00:00Z" + val response = client.post("/episodes/multiple") { contentType(ContentType.Application.Json) setBody( @@ -133,6 +135,7 @@ internal class EpisodeControllerTest : AbstractAPITest() { Episode( anime = anime, platform = platform, + releaseDate = date, episodeType = episodeType, langType = langType, number = 1, @@ -144,6 +147,7 @@ internal class EpisodeControllerTest : AbstractAPITest() { Episode( anime = anime, platform = platform, + releaseDate = date, episodeType = episodeType, langType = langType, number = 2, @@ -155,6 +159,7 @@ internal class EpisodeControllerTest : AbstractAPITest() { Episode( anime = anime, platform = platform, + releaseDate = date, episodeType = episodeType, langType = langType, number = -1, @@ -232,11 +237,17 @@ internal class EpisodeControllerTest : AbstractAPITest() { expect(2023) { tmpSimulcast.year } val nextDate = CalendarConverter.toUTCCalendar(date) - nextDate.add(Calendar.DAY_OF_YEAR, 10) + nextDate.add(Calendar.DAY_OF_YEAR, 15) val tmpNextSimulcast = Simulcast.getSimulcastFrom(nextDate.toISO8601()) expect("AUTUMN") { tmpNextSimulcast.season } expect(2023) { tmpNextSimulcast.year } + val previousDate = CalendarConverter.toUTCCalendar(date) + previousDate.add(Calendar.DAY_OF_YEAR, -15) + val tmpPreviousSimulcast = Simulcast.getSimulcastFrom(nextDate.toISO8601()) + expect("AUTUMN") { tmpPreviousSimulcast.season } + expect(2023) { tmpPreviousSimulcast.year } + expect(HttpStatusCode.Created) { response.status } val json = Constant.gson.fromJson(response.bodyAsText(), Array::class.java) expect(2) { json.size } @@ -256,6 +267,88 @@ internal class EpisodeControllerTest : AbstractAPITest() { } } + @Test + fun saveForPreviousSimulcast() { + testApplication { + val client = createClient { + install(ContentNegotiation) { + gson() + } + } + + application { + configureHTTP() + configureRoutingTest() + } + + val platform = platformRepository.getAll().first() + val anime = animeRepository.getAll() + val episodeType = episodeTypeRepository.getAll().last() + val langType = langTypeRepository.getAll().first() + + val date = "2023-10-10T00:00:00Z" + + val response = client.post("/episodes/multiple") { + contentType(ContentType.Application.Json) + setBody( + listOf( + Episode( + anime = anime.first(), + releaseDate = date, + platform = platform, + episodeType = episodeType, + langType = langType, + number = 1, + season = 1, + url = "https://www.google.com", + image = "https://www.google.com", + hash = "hash", + ), + Episode( + anime = anime.last(), + releaseDate = date, + platform = platform, + episodeType = episodeType, + langType = langType, + number = 12, + season = 1, + url = "https://www.google.com", + image = "https://www.google.com", + hash = "hash-2", + ), + ) + ) + } + + val tmpSimulcast = Simulcast.getSimulcastFrom(date) + expect("AUTUMN") { tmpSimulcast.season } + expect(2023) { tmpSimulcast.year } + + val previousDate = CalendarConverter.toUTCCalendar(date) + previousDate.add(Calendar.DAY_OF_YEAR, -15) + val tmpPreviousSimulcast = Simulcast.getSimulcastFrom(previousDate.toISO8601()) + expect("SUMMER") { tmpPreviousSimulcast.season } + expect(2023) { tmpPreviousSimulcast.year } + + expect(HttpStatusCode.Created) { response.status } + val json = Constant.gson.fromJson(response.bodyAsText(), Array::class.java) + expect(2) { json.size } + + val simulcasts = json[0].anime?.simulcasts?.toMutableList() + ?.sortedWith(compareBy({ it.year }, { Constant.seasons.indexOf(it.season) })) + println(simulcasts) + + expect(tmpSimulcast.season) { simulcasts?.last()?.season } + expect(tmpSimulcast.year) { simulcasts?.last()?.year } + + val simulcasts2 = json[1].anime?.simulcasts?.toMutableList() + ?.sortedWith(compareBy({ it.year }, { Constant.seasons.indexOf(it.season) })) + println(simulcasts2) + expect(tmpPreviousSimulcast.season) { simulcasts2?.last()?.season } + expect(tmpPreviousSimulcast.year) { simulcasts2?.last()?.year } + } + } + @Test fun saveError() { testApplication {