Skip to content

Commit

Permalink
Fix some liquibase and add AnimeControllerTest.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziedelth committed Jan 4, 2024
1 parent 605043c commit db910dc
Show file tree
Hide file tree
Showing 18 changed files with 274 additions and 45 deletions.
23 changes: 0 additions & 23 deletions src/main/kotlin/fr/shikkanime/controllers/api/AnimeController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,29 +95,6 @@ class AnimeController {
return Response.ok(AbstractConverter.convert(list, AnimeDto::class.java))
}

@Path("/{uuid}")
@Get
@OpenAPI(
"Get anime",
[
OpenAPIResponse(
200,
"Anime found",
AnimeDto::class,
),
OpenAPIResponse(
404,
"Anime not found",
MessageDto::class,
),
]
)
private fun getAnime(@PathParam("uuid") uuid: UUID): Response {
val anime =
animeService.find(uuid) ?: return Response.notFound(MessageDto(MessageDto.Type.ERROR, "Anime not found"))
return Response.ok(AbstractConverter.convert(anime, AnimeDto::class.java))
}

@Path("/{uuid}/image")
@Get
@Cached(maxAgeSeconds = 3600)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.google.inject.Inject
import fr.shikkanime.converters.AbstractConverter
import fr.shikkanime.dtos.AnimeDto
import fr.shikkanime.entities.Anime
import fr.shikkanime.entities.Simulcast
import fr.shikkanime.entities.enums.CountryCode
import fr.shikkanime.services.AnimeService
import java.time.ZonedDateTime
Expand All @@ -13,18 +14,23 @@ class AnimeDtoToAnimeConverter : AbstractConverter<AnimeDto, Anime>() {
private lateinit var animeService: AnimeService

override fun convert(from: AnimeDto): Anime {
val findByUuid = animeService.find(from.uuid)

if (findByUuid != null)
return findByUuid

val findByName = animeService.findByLikeName(CountryCode.FR, from.name)

if (findByName.isNotEmpty()) {
if (findByName.isNotEmpty())
return findByName.first()
}

return Anime(
countryCode = from.countryCode,
name = from.name,
releaseDateTime = ZonedDateTime.parse(from.releaseDateTime),
image = from.image,
description = from.description,
simulcasts = convert(from.simulcasts ?: emptyList(), Simulcast::class.java).toMutableSet()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@ import fr.shikkanime.converters.AbstractConverter
import fr.shikkanime.dtos.AnimeDto
import fr.shikkanime.dtos.SimulcastDto
import fr.shikkanime.entities.Anime
import org.hibernate.Hibernate
import java.time.ZoneId
import java.time.format.DateTimeFormatter

class AnimeToAnimeDtoConverter : AbstractConverter<Anime, AnimeDto>() {
private val utcZone = ZoneId.of("UTC")

override fun convert(from: Anime): AnimeDto {
return AnimeDto(
uuid = from.uuid,
releaseDateTime = from.releaseDateTime.format(DateTimeFormatter.ISO_DATE_TIME),
releaseDateTime = from.releaseDateTime.withZoneSameInstant(utcZone).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME),
image = from.image,
countryCode = from.countryCode!!,
name = from.name!!,
description = from.description,
simulcasts = if (Hibernate.isInitialized(from.simulcasts)) convert(
from.simulcasts,
SimulcastDto::class.java
) else null,
simulcasts = convert(from.simulcasts, SimulcastDto::class.java),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import fr.shikkanime.converters.AbstractConverter
import fr.shikkanime.dtos.AnimeDto
import fr.shikkanime.dtos.EpisodeDto
import fr.shikkanime.entities.Episode
import java.time.ZoneId
import java.time.format.DateTimeFormatter

class EpisodeToEpisodeDtoConverter : AbstractConverter<Episode, EpisodeDto>() {
private val utcZone = ZoneId.of("UTC")

override fun convert(from: Episode): EpisodeDto {
return EpisodeDto(
uuid = from.uuid,
Expand All @@ -15,7 +18,7 @@ class EpisodeToEpisodeDtoConverter : AbstractConverter<Episode, EpisodeDto>() {
episodeType = from.episodeType!!,
langType = from.langType!!,
hash = from.hash!!,
releaseDateTime = from.releaseDateTime.format(DateTimeFormatter.ISO_DATE_TIME),
releaseDateTime = from.releaseDateTime.withZoneSameInstant(utcZone).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME),
season = from.season!!,
number = from.number!!,
title = from.title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package fr.shikkanime.converters.member
import fr.shikkanime.converters.AbstractConverter
import fr.shikkanime.dtos.MemberDto
import fr.shikkanime.entities.Member
import java.time.ZoneId
import java.time.format.DateTimeFormatter

class MemberToMemberDtoConverter : AbstractConverter<Member, MemberDto>() {
private val utcZone = ZoneId.of("UTC")

override fun convert(from: Member): MemberDto {
return MemberDto(
uuid = from.uuid,
creationDateTime = from.creationDateTime.format(DateTimeFormatter.ISO_DATE_TIME),
creationDateTime = from.creationDateTime.withZoneSameInstant(utcZone).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME),
username = from.username!!,
role = from.role,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package fr.shikkanime.converters.simulcast

import com.google.inject.Inject
import fr.shikkanime.converters.AbstractConverter
import fr.shikkanime.dtos.SimulcastDto
import fr.shikkanime.entities.Simulcast
import fr.shikkanime.services.SimulcastService

class SimulcastDtoToSimulcastConverter : AbstractConverter<SimulcastDto, Simulcast>() {
@Inject
private lateinit var simulcastService: SimulcastService

override fun convert(from: SimulcastDto): Simulcast {
val found = simulcastService.find(from.uuid)

if (found != null)
return found

return Simulcast(
season = from.season,
year = from.year,
)
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/fr/shikkanime/entities/Simulcast.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ data class Simulcast(
override val uuid: UUID? = null,
@Column(nullable = false)
val season: String? = null,
@Column(nullable = false)
@Column(nullable = false, name = "year_")
val year: Int? = null,
) : ShikkEntity(uuid) {
override fun equals(other: Any?): Boolean {
Expand Down
21 changes: 12 additions & 9 deletions src/main/kotlin/fr/shikkanime/plugins/HTTP.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.shikkanime.plugins

import fr.shikkanime.utils.Constant
import freemarker.cache.ClassTemplateLoader
import io.github.smiley4.ktorswaggerui.SwaggerUI
import io.ktor.http.*
Expand Down Expand Up @@ -40,15 +41,17 @@ fun Application.configureHTTP() {
install(FreeMarker) {
templateLoader = ClassTemplateLoader(this::class.java.classLoader, "templates")
}
install(SwaggerUI) {
swagger {
swaggerUrl = "api/swagger"
forwardRoot = false
}
info {
title = "Shikkanime API"
version = "1.0"
description = "API for testing and demonstration purposes"
if (Constant.isDev) {
install(SwaggerUI) {
swagger {
swaggerUrl = "api/swagger"
forwardRoot = false
}
info {
title = "Shikkanime API"
version = "1.0"
description = "API for testing and demonstration purposes"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,10 @@ abstract class AbstractRepository<E : ShikkEntity> {
it.remove(entity)
}
}

fun deleteAll() {
inTransaction {
it.createQuery("DELETE FROM ${getEntityClass().simpleName}").executeUpdate()
}
}
}
2 changes: 2 additions & 0 deletions src/main/kotlin/fr/shikkanime/services/AbstractService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ abstract class AbstractService<E : ShikkEntity, R : AbstractRepository<E>> {
fun update(entity: E) = getRepository().update(entity)

fun delete(entity: E) = getRepository().delete(entity)

fun deleteAll() = getRepository().deleteAll()
}
1 change: 1 addition & 0 deletions src/main/kotlin/fr/shikkanime/utils/Constant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ object Constant {

return dataFolder
}
var isDev = System.getenv("ENV") == "dev"

init {
abstractPlatforms.forEach {
Expand Down
24 changes: 24 additions & 0 deletions src/main/resources/db/changelog/2023/12/02-changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
</changeSet>

<changeSet id="${id}-2" author="${author}">
<preConditions onFail="MARK_RAN">
<not>
<tableExists tableName="anime_simulcast"/>
</not>
</preConditions>

<createTable tableName="anime_simulcast">
<column name="anime_uuid"
type="UUID">
Expand All @@ -54,6 +60,12 @@
</changeSet>

<changeSet id="${id}-3" author="${author}">
<preConditions onFail="MARK_RAN">
<not>
<tableExists tableName="simulcast"/>
</not>
</preConditions>

<createTable tableName="simulcast">
<column name="uuid"
type="UUID">
Expand All @@ -73,6 +85,12 @@
</changeSet>

<changeSet id="${id}-4" author="${author}">
<preConditions onFail="MARK_RAN">
<not>
<foreignKeyConstraintExists foreignKeyName="fk_anisim_on_anime"/>
</not>
</preConditions>

<addForeignKeyConstraint baseColumnNames="anime_uuid"
baseTableName="anime_simulcast"
constraintName="fk_anisim_on_anime"
Expand All @@ -81,6 +99,12 @@
</changeSet>

<changeSet id="${id}-5" author="${author}">
<preConditions onFail="MARK_RAN">
<not>
<foreignKeyConstraintExists foreignKeyName="fk_anisim_on_simulcast"/>
</not>
</preConditions>

<addForeignKeyConstraint baseColumnNames="simulcast_uuid"
baseTableName="anime_simulcast"
constraintName="fk_anisim_on_simulcast"
Expand Down
19 changes: 19 additions & 0 deletions src/main/resources/db/changelog/2024/01/01-changelog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.25.xsd"
objectQuotingStrategy="QUOTE_ONLY_RESERVED_WORDS">

<property global="false" name="id" value="1704368085902"/>
<property global="false" name="author" value="Ziedelth"/>

<changeSet id="${id}-1" author="${author}">
<preConditions onFail="MARK_RAN">
<columnExists tableName="simulcast" columnName="year"/>
</preConditions>

<renameColumn tableName="simulcast" oldColumnName="year" newColumnName="year_"/>
</changeSet>
</databaseChangeLog>
1 change: 1 addition & 0 deletions src/main/resources/db/changelog/db.changelog-master.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
objectQuotingStrategy="QUOTE_ONLY_RESERVED_WORDS">
<include file="/db/changelog/2023/12/01-changelog.xml"/>
<include file="/db/changelog/2023/12/02-changelog.xml"/>
<include file="/db/changelog/2024/01/01-changelog.xml"/>
</databaseChangeLog>
Loading

0 comments on commit db910dc

Please sign in to comment.