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

Commit

Permalink
Fix -1 number
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziedelth committed Nov 22, 2022
1 parent f361081 commit facb5c4
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/main/kotlin/fr/ziedelth/controllers/EpisodeController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class EpisodeController(
throw Exception("Episode is not valid")
}

if (episode.number == -1) {
episode.number = episodeRepository.getLastNumber(episode) + 1
}

val tmpSimulcast =
Simulcast.getSimulcast(episode.releaseDate.split("-")[0].toInt(), episode.releaseDate.split("-")[1].toInt())
val simulcast =
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/fr/ziedelth/entities/Episode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Episode(
@Column(nullable = false)
val season: Int? = null,
@Column(nullable = false)
val number: Int? = null,
var number: Int? = null,
@Column(nullable = true)
val title: String? = null,
@Column(nullable = false, columnDefinition = "TEXT")
Expand Down
17 changes: 17 additions & 0 deletions src/main/kotlin/fr/ziedelth/repositories/EpisodeRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,21 @@ class EpisodeRepository(session: () -> Session = { Database.getSession() }) : Ab
"list" to list
)
}

fun getLastNumber(episode: Episode): Int {
val session = getSession.invoke()
val query = session.createQuery(
"SELECT number FROM Episode WHERE anime.uuid = :uuid AND platform = :platform AND season = :season AND episodeType.uuid = :episodeType AND langType.uuid = :langType ORDER BY number DESC",
Int::class.java
)
query.maxResults = 1
query.setParameter("uuid", episode.anime?.uuid)
query.setParameter("platform", episode.platform)
query.setParameter("season", episode.season)
query.setParameter("episodeType", episode.episodeType?.uuid)
query.setParameter("langType", episode.langType?.uuid)
val number = query.uniqueResult() ?: 0
session.close()
return number
}
}
4 changes: 2 additions & 2 deletions src/test/kotlin/fr/ziedelth/AbstractAPITest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ internal abstract class AbstractAPITest {
animes.forEach {
val episodes = (1..10).map { episode ->
val platform = platforms.random()
val episodeType = episodeTypes.random()
val langType = langTypes.random()
val episodeType = episodeTypes.first()
val langType = langTypes.first()

Episode(
platform = platform,
Expand Down
18 changes: 15 additions & 3 deletions src/test/kotlin/fr/ziedelth/controllers/EpisodeControllerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ internal class EpisodeControllerTest : AbstractAPITest() {
}

val platform = platformRepository.getAll().first()
val anime = animeRepository.getAll().first()
val episodeType = episodeTypeRepository.getAll().first()
val anime = animeRepository.getAll().last()
val episodeType = episodeTypeRepository.getAll().last()
val langType = langTypeRepository.getAll().first()

val response = client.post("/episodes/multiple") {
Expand Down Expand Up @@ -190,14 +190,26 @@ internal class EpisodeControllerTest : AbstractAPITest() {
url = "https://www.google.com",
image = "https://www.google.com",
hash = "azertyuiop",
),
Episode(
anime = anime,
platform = platform,
episodeType = episodeType,
langType = langType,
number = -1,
season = 1,
url = "https://www.google.com",
image = "https://www.google.com",
hash = "awzsxedcrfvtgbyhn",
)
)
)
}

expect(HttpStatusCode.Created) { response.status }
val json = Gson().fromJson(response.bodyAsText(), Array<Episode>::class.java)
expect(2) { json.size }
expect(3) { json.size }
expect(3) { json[2].number }
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/test/kotlin/fr/ziedelth/repositories/EpisodeRepositoryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package fr.ziedelth.repositories
import fr.ziedelth.AbstractAPITest
import fr.ziedelth.plugins.animeRepository
import fr.ziedelth.plugins.episodeRepository
import fr.ziedelth.plugins.episodeTypeRepository
import fr.ziedelth.plugins.langTypeRepository
import org.junit.jupiter.api.Test
import kotlin.test.expect

Expand Down Expand Up @@ -34,4 +36,15 @@ internal class EpisodeRepositoryTest : AbstractAPITest() {
val page2 = episodeRepository.getByPageWithList(animes, 2, 2)
expect(2) { page2.size }
}

@Test
fun getLastNumber() {
val episode = episodeRepository.getAll().first()
val episodeType = episodeTypeRepository.getAll().first()
val langType = langTypeRepository.getAll().last()
episode.episodeType = episodeType
episode.langType = langType
val lastNumber = episodeRepository.getLastNumber(episode)
expect(0) { lastNumber }
}
}

0 comments on commit facb5c4

Please sign in to comment.