diff --git a/src/main/kotlin/fr/ziedelth/controllers/EpisodeController.kt b/src/main/kotlin/fr/ziedelth/controllers/EpisodeController.kt index b97f8aa..92fb644 100644 --- a/src/main/kotlin/fr/ziedelth/controllers/EpisodeController.kt +++ b/src/main/kotlin/fr/ziedelth/controllers/EpisodeController.kt @@ -9,9 +9,11 @@ import fr.ziedelth.repositories.* import fr.ziedelth.services.AnimeService import fr.ziedelth.services.EpisodeService import fr.ziedelth.services.SimulcastService +import fr.ziedelth.utils.CalendarConverter import fr.ziedelth.utils.ImageCache import fr.ziedelth.utils.plugins.PluginManager import fr.ziedelth.utils.routes.APIRoute +import fr.ziedelth.utils.toISO8601 import io.ktor.http.* import io.ktor.server.application.* import io.ktor.server.request.* @@ -123,13 +125,23 @@ class EpisodeController : AttachmentController("/episodes") { episode.number = episodeRepository.getLastNumber(episode) + 1 } - val tmpSimulcast = - Simulcast.getSimulcast(episode.releaseDate.split("-")[0].toInt(), episode.releaseDate.split("-")[1].toInt()) - val simulcast = - simulcastRepository.findBySeasonAndYear(tmpSimulcast.season!!, tmpSimulcast.year!!) ?: tmpSimulcast + 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.anime!!.simulcasts.isEmpty() || episode.anime!!.simulcasts.none { it.uuid == simulcast.uuid }) { - episode.anime!!.simulcasts.add(simulcast) + if (episode.number == 1 && tmpSimulcast != 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 { + val simulcast = simulcastRepository.findBySeasonAndYear(tmpSimulcast.season!!, tmpSimulcast.year!!) ?: tmpSimulcast + + 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 f39a227..ac1af79 100644 --- a/src/main/kotlin/fr/ziedelth/entities/Simulcast.kt +++ b/src/main/kotlin/fr/ziedelth/entities/Simulcast.kt @@ -1,6 +1,8 @@ package fr.ziedelth.entities +import fr.ziedelth.utils.CalendarConverter import fr.ziedelth.utils.Constant +import fr.ziedelth.utils.toISO8601 import jakarta.persistence.* import org.hibernate.annotations.Cache import org.hibernate.annotations.CacheConcurrencyStrategy @@ -25,5 +27,16 @@ class Simulcast( val season = Constant.seasons[(month - 1) / 3] return Simulcast(season = season, year = year) } + + fun getSimulcastFrom(date: String): Simulcast { + val calendar = CalendarConverter.toUTCCalendar(date) + val iso8601 = calendar.toISO8601() + val split = iso8601.split("-") + return getSimulcast(split[0].toInt(), split[1].toInt()) + } + } + + override fun toString(): String { + return "Simulcast(uuid=$uuid, season=$season, year=$year)" } } diff --git a/src/main/kotlin/fr/ziedelth/utils/CalendarConverter.kt b/src/main/kotlin/fr/ziedelth/utils/CalendarConverter.kt index 5ee3fd6..bb73f48 100644 --- a/src/main/kotlin/fr/ziedelth/utils/CalendarConverter.kt +++ b/src/main/kotlin/fr/ziedelth/utils/CalendarConverter.kt @@ -14,4 +14,13 @@ object CalendarConverter { this.utcFormatter.timeZone = timeZone return this.utcFormatter.format(Date.from(iso8601calendar?.toInstant())) } + + fun toUTCCalendar(iso8601String: String): Calendar { + this.utcFormatter.timeZone = timeZone + val calendar = Calendar.getInstance() + val date = this.utcFormatter.parse(iso8601String) + calendar.time = date + calendar.timeZone = timeZone + return calendar + } } diff --git a/src/test/kotlin/fr/ziedelth/controllers/EpisodeControllerTest.kt b/src/test/kotlin/fr/ziedelth/controllers/EpisodeControllerTest.kt index b0db16a..4790fd2 100644 --- a/src/test/kotlin/fr/ziedelth/controllers/EpisodeControllerTest.kt +++ b/src/test/kotlin/fr/ziedelth/controllers/EpisodeControllerTest.kt @@ -4,7 +4,9 @@ import fr.ziedelth.AbstractAPITest import fr.ziedelth.entities.* import fr.ziedelth.entities.Platform import fr.ziedelth.plugins.* +import fr.ziedelth.utils.CalendarConverter import fr.ziedelth.utils.Constant +import fr.ziedelth.utils.toISO8601 import io.ktor.client.plugins.contentnegotiation.* import io.ktor.client.request.* import io.ktor.client.statement.* @@ -172,6 +174,86 @@ internal class EpisodeControllerTest : AbstractAPITest() { } } + @Test + fun saveForNextSimulcast() { + 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-09-28T00: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("SUMMER") { tmpSimulcast.season } + expect(2023) { tmpSimulcast.year } + + val nextDate = CalendarConverter.toUTCCalendar(date) + nextDate.add(Calendar.DAY_OF_YEAR, 10) + val tmpNextSimulcast = Simulcast.getSimulcastFrom(nextDate.toISO8601()) + expect("AUTUMN") { tmpNextSimulcast.season } + expect(2023) { tmpNextSimulcast.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(tmpNextSimulcast.season) { simulcasts?.last()?.season } + expect(tmpNextSimulcast.year) { simulcasts?.last()?.year } + + val simulcasts2 = json[1].anime?.simulcasts?.toMutableList()?.sortedWith(compareBy({ it.year }, { Constant.seasons.indexOf(it.season) })) + println(simulcasts2) + expect(tmpSimulcast.season) { simulcasts2?.last()?.season } + expect(tmpSimulcast.year) { simulcasts2?.last()?.year } + } + } + @Test fun saveError() { testApplication {