From 0434383f0b00fb5f4026d950f55888c52f82bf6a Mon Sep 17 00:00:00 2001 From: Ziedelth Date: Sun, 1 Oct 2023 11:26:55 +0200 Subject: [PATCH] Refine episode creation rules and update date conversion --- .../ziedelth/controllers/EpisodeController.kt | 10 ++++--- .../fr/ziedelth/utils/CalendarConverter.kt | 13 ++++++++ .../kotlin/fr/ziedelth/AbstractAPITest.kt | 13 +++++++- .../controllers/AnimeControllerTest.kt | 4 +-- .../controllers/EpisodeControllerTest.kt | 30 +++++++++++++++---- 5 files changed, 57 insertions(+), 13 deletions(-) diff --git a/src/main/kotlin/fr/ziedelth/controllers/EpisodeController.kt b/src/main/kotlin/fr/ziedelth/controllers/EpisodeController.kt index cbd3740..ef24712 100644 --- a/src/main/kotlin/fr/ziedelth/controllers/EpisodeController.kt +++ b/src/main/kotlin/fr/ziedelth/controllers/EpisodeController.kt @@ -133,9 +133,8 @@ class EpisodeController : AttachmentController("/episodes") { val tmpCurrentSimulcast = Simulcast.getSimulcastFrom(episode.releaseDate) val tmpNextSimulcast = Simulcast.getSimulcastFrom(releaseDatePlus15Days.toISO8601()) - println(tmpPreviousSimulcast) - println(tmpCurrentSimulcast) - println(tmpNextSimulcast) + val isAnimeReleaseDateBeforeMinus15Days = CalendarConverter.toUTCLocalDateTime(episode.anime!!.releaseDate) + .isBefore(CalendarConverter.calendarToLocalDateTime(releaseDateMinus15Days)) if (episode.number!! <= 1 && !tmpCurrentSimulcast.equalsWithoutUUID(tmpNextSimulcast)) { val simulcast = simulcastRepository.findBySeasonAndYear(tmpNextSimulcast.season!!, tmpNextSimulcast.year!!) @@ -144,7 +143,10 @@ class EpisodeController : AttachmentController("/episodes") { 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)) { + } else if (episode.number!! > 1 && isAnimeReleaseDateBeforeMinus15Days && !tmpCurrentSimulcast.equalsWithoutUUID( + tmpPreviousSimulcast + ) + ) { val simulcast = simulcastRepository.findBySeasonAndYear(tmpPreviousSimulcast.season!!, tmpPreviousSimulcast.year!!) ?: tmpPreviousSimulcast diff --git a/src/main/kotlin/fr/ziedelth/utils/CalendarConverter.kt b/src/main/kotlin/fr/ziedelth/utils/CalendarConverter.kt index bb73f48..908990c 100644 --- a/src/main/kotlin/fr/ziedelth/utils/CalendarConverter.kt +++ b/src/main/kotlin/fr/ziedelth/utils/CalendarConverter.kt @@ -1,6 +1,9 @@ package fr.ziedelth.utils +import io.ktor.server.util.* +import io.ktor.util.* import java.text.SimpleDateFormat +import java.time.LocalDateTime import java.util.* val DATE_FORMAT_REGEX = "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z\$".toRegex() @@ -23,4 +26,14 @@ object CalendarConverter { calendar.timeZone = timeZone return calendar } + + @OptIn(InternalAPI::class) + fun toUTCLocalDateTime(iso8601String: String): LocalDateTime { + this.utcFormatter.timeZone = timeZone + return this.utcFormatter.parse(iso8601String).toLocalDateTime() + } + + fun calendarToLocalDateTime(calendar: Calendar): LocalDateTime { + return toUTCLocalDateTime(calendar.toISO8601()) + } } diff --git a/src/test/kotlin/fr/ziedelth/AbstractAPITest.kt b/src/test/kotlin/fr/ziedelth/AbstractAPITest.kt index b4d6896..47c0d76 100644 --- a/src/test/kotlin/fr/ziedelth/AbstractAPITest.kt +++ b/src/test/kotlin/fr/ziedelth/AbstractAPITest.kt @@ -41,6 +41,7 @@ internal abstract class AbstractAPITest { country = countries.first(), name = "One Piece", image = "hello", + releaseDate = "2020-01-01T00:00:00Z", hashes = mutableSetOf("hello"), simulcasts = mutableSetOf(simulcasts.first()), genres = mutableSetOf(genres.first(), genres.last()), @@ -49,6 +50,7 @@ internal abstract class AbstractAPITest { country = countries.first(), name = "Naruto", image = "hello", + releaseDate = "2020-01-01T00:00:00Z", hashes = mutableSetOf("hello2"), simulcasts = mutableSetOf(simulcasts.first()), genres = mutableSetOf(genres.first(), genres.last()), @@ -61,7 +63,16 @@ internal abstract class AbstractAPITest { simulcasts = mutableSetOf(simulcasts.first()), genres = mutableSetOf(genres.first(), genres.last()), ) - animeRepository.saveAll(listOf(anime1, anime2, anime3)) + val anime4 = Anime( + country = countries.first(), + name = "Frieren", + image = "hello", + hashes = mutableSetOf("hello3"), + simulcasts = mutableSetOf(simulcasts.first()), + releaseDate = "2023-09-29T15:00:00Z", + genres = mutableSetOf(genres.first(), genres.last()), + ) + animeRepository.saveAll(listOf(anime1, anime2, anime3, anime4)) val animes = animeRepository.getAll() val episodeType1 = EpisodeType(name = "Episode") diff --git a/src/test/kotlin/fr/ziedelth/controllers/AnimeControllerTest.kt b/src/test/kotlin/fr/ziedelth/controllers/AnimeControllerTest.kt index b2a8b86..7075e1f 100644 --- a/src/test/kotlin/fr/ziedelth/controllers/AnimeControllerTest.kt +++ b/src/test/kotlin/fr/ziedelth/controllers/AnimeControllerTest.kt @@ -76,7 +76,7 @@ internal class AnimeControllerTest : AbstractAPITest() { val jsonNotCached = Constant.gson.fromJson(responseNotCached.bodyAsText(), Array::class.java) expect(HttpStatusCode.OK) { responseNotCached.status } - expect(3) { jsonNotCached.size } + expect(4) { jsonNotCached.size } // CACHED @@ -85,7 +85,7 @@ internal class AnimeControllerTest : AbstractAPITest() { val jsonCached = Constant.gson.fromJson(responseCached.bodyAsText(), Array::class.java) expect(HttpStatusCode.OK) { responseCached.status } - expect(3) { jsonCached.size } + expect(4) { jsonCached.size } } } diff --git a/src/test/kotlin/fr/ziedelth/controllers/EpisodeControllerTest.kt b/src/test/kotlin/fr/ziedelth/controllers/EpisodeControllerTest.kt index 85e9b77..ab6f09d 100644 --- a/src/test/kotlin/fr/ziedelth/controllers/EpisodeControllerTest.kt +++ b/src/test/kotlin/fr/ziedelth/controllers/EpisodeControllerTest.kt @@ -282,7 +282,7 @@ internal class EpisodeControllerTest : AbstractAPITest() { } val platform = platformRepository.getAll().first() - val anime = animeRepository.getAll() + val animes = animeRepository.getAll() val episodeType = episodeTypeRepository.getAll().last() val langType = langTypeRepository.getAll().first() @@ -293,7 +293,7 @@ internal class EpisodeControllerTest : AbstractAPITest() { setBody( listOf( Episode( - anime = anime.first(), + anime = animes.first(), releaseDate = date, platform = platform, episodeType = episodeType, @@ -305,7 +305,7 @@ internal class EpisodeControllerTest : AbstractAPITest() { hash = "hash", ), Episode( - anime = anime.last(), + anime = animes.last(), releaseDate = date, platform = platform, episodeType = episodeType, @@ -316,6 +316,18 @@ internal class EpisodeControllerTest : AbstractAPITest() { image = "https://www.google.com", hash = "hash-2", ), + Episode( + anime = animes[1], + releaseDate = date, + platform = platform, + episodeType = episodeType, + langType = langType, + number = 2, + season = 1, + url = "https://www.google.com", + image = "https://www.google.com", + hash = "hash-3", + ), ) ) } @@ -332,7 +344,7 @@ internal class EpisodeControllerTest : AbstractAPITest() { expect(HttpStatusCode.Created) { response.status } val json = Constant.gson.fromJson(response.bodyAsText(), Array::class.java) - expect(2) { json.size } + expect(3) { json.size } val simulcasts = json[0].anime?.simulcasts?.toMutableList() ?.sortedWith(compareBy({ it.year }, { Constant.seasons.indexOf(it.season) })) @@ -344,8 +356,14 @@ internal class EpisodeControllerTest : AbstractAPITest() { 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 } + expect(tmpSimulcast.season) { simulcasts2?.last()?.season } + expect(tmpSimulcast.year) { simulcasts2?.last()?.year } + + val simulcasts3 = json[2].anime?.simulcasts?.toMutableList() + ?.sortedWith(compareBy({ it.year }, { Constant.seasons.indexOf(it.season) })) + println(simulcasts3) + expect(tmpPreviousSimulcast.season) { simulcasts3?.last()?.season } + expect(tmpPreviousSimulcast.year) { simulcasts3?.last()?.year } } }