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

Refine episode creation rules and update date conversion #171

Merged
merged 1 commit into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/main/kotlin/fr/ziedelth/controllers/EpisodeController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,8 @@ class EpisodeController : AttachmentController<Episode>("/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!!)
Expand All @@ -144,7 +143,10 @@ class EpisodeController : AttachmentController<Episode>("/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
Expand Down
13 changes: 13 additions & 0 deletions src/main/kotlin/fr/ziedelth/utils/CalendarConverter.kt
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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())
}
}
13 changes: 12 additions & 1 deletion src/test/kotlin/fr/ziedelth/AbstractAPITest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand All @@ -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()),
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ internal class AnimeControllerTest : AbstractAPITest() {
val jsonNotCached = Constant.gson.fromJson(responseNotCached.bodyAsText(), Array<Anime>::class.java)

expect(HttpStatusCode.OK) { responseNotCached.status }
expect(3) { jsonNotCached.size }
expect(4) { jsonNotCached.size }

// CACHED

Expand All @@ -85,7 +85,7 @@ internal class AnimeControllerTest : AbstractAPITest() {
val jsonCached = Constant.gson.fromJson(responseCached.bodyAsText(), Array<Anime>::class.java)

expect(HttpStatusCode.OK) { responseCached.status }
expect(3) { jsonCached.size }
expect(4) { jsonCached.size }
}
}

Expand Down
30 changes: 24 additions & 6 deletions src/test/kotlin/fr/ziedelth/controllers/EpisodeControllerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -293,7 +293,7 @@ internal class EpisodeControllerTest : AbstractAPITest() {
setBody(
listOf(
Episode(
anime = anime.first(),
anime = animes.first(),
releaseDate = date,
platform = platform,
episodeType = episodeType,
Expand All @@ -305,7 +305,7 @@ internal class EpisodeControllerTest : AbstractAPITest() {
hash = "hash",
),
Episode(
anime = anime.last(),
anime = animes.last(),
releaseDate = date,
platform = platform,
episodeType = episodeType,
Expand All @@ -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",
),
)
)
}
Expand All @@ -332,7 +344,7 @@ internal class EpisodeControllerTest : AbstractAPITest() {

expect(HttpStatusCode.Created) { response.status }
val json = Constant.gson.fromJson(response.bodyAsText(), Array<Episode>::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) }))
Expand All @@ -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 }
}
}

Expand Down