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

Commit

Permalink
Improve episode update by adding comparison method in Simulcast
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziedelth committed Sep 30, 2023
1 parent 82c0317 commit 64ed7f9
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 8 deletions.
30 changes: 23 additions & 7 deletions src/main/kotlin/fr/ziedelth/controllers/EpisodeController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -125,21 +125,37 @@ class EpisodeController : AttachmentController<Episode>("/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)
Expand Down
12 changes: 12 additions & 0 deletions src/main/kotlin/fr/ziedelth/entities/Simulcast.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
95 changes: 94 additions & 1 deletion src/test/kotlin/fr/ziedelth/controllers/EpisodeControllerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,16 @@ 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(
listOf(
Episode(
anime = anime,
platform = platform,
releaseDate = date,
episodeType = episodeType,
langType = langType,
number = 1,
Expand All @@ -144,6 +147,7 @@ internal class EpisodeControllerTest : AbstractAPITest() {
Episode(
anime = anime,
platform = platform,
releaseDate = date,
episodeType = episodeType,
langType = langType,
number = 2,
Expand All @@ -155,6 +159,7 @@ internal class EpisodeControllerTest : AbstractAPITest() {
Episode(
anime = anime,
platform = platform,
releaseDate = date,
episodeType = episodeType,
langType = langType,
number = -1,
Expand Down Expand Up @@ -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<Episode>::class.java)
expect(2) { json.size }
Expand All @@ -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<Episode>::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 {
Expand Down

0 comments on commit 64ed7f9

Please sign in to comment.