Skip to content

Commit

Permalink
chore: fix code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziedelth committed Nov 28, 2024
1 parent 03935d9 commit 0516142
Show file tree
Hide file tree
Showing 35 changed files with 216 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fr.shikkanime.controllers.api
import com.google.inject.Inject
import fr.shikkanime.converters.AbstractConverter
import fr.shikkanime.dtos.*
import fr.shikkanime.dtos.animes.AnimeDto
import fr.shikkanime.dtos.enums.Status
import fr.shikkanime.dtos.weekly.v1.WeeklyAnimesDto
import fr.shikkanime.entities.enums.CountryCode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package fr.shikkanime.controllers.api
import com.google.inject.Inject
import fr.shikkanime.converters.AbstractConverter
import fr.shikkanime.dtos.PageableDto
import fr.shikkanime.dtos.UpdateAllEpisodeMappingDto
import fr.shikkanime.dtos.mappings.UpdateAllEpisodeMappingDto
import fr.shikkanime.dtos.enums.Status
import fr.shikkanime.dtos.mappings.EpisodeMappingDto
import fr.shikkanime.dtos.variants.EpisodeVariantDto
Expand Down
14 changes: 5 additions & 9 deletions src/main/kotlin/fr/shikkanime/controllers/site/SEOController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.google.inject.Inject
import fr.shikkanime.dtos.URLDto
import fr.shikkanime.entities.SortParameter
import fr.shikkanime.entities.enums.CountryCode
import fr.shikkanime.entities.enums.EpisodeType
import fr.shikkanime.entities.enums.Link
import fr.shikkanime.services.caches.EpisodeMappingCacheService
import fr.shikkanime.services.caches.SimulcastCacheService
Expand Down Expand Up @@ -61,21 +60,18 @@ class SEOController {
URLDto("${Constant.baseUrl}/catalog/${it.slug}", it.lastReleaseDateTime!!)
}

episodeMappingCacheService.findAllSeo()?.groupBy { it[0] as String }?.forEach { (animeSlug, episodes) ->
val seasonMap = episodes.groupBy { it[1] as Int }
val firstSeasonDateTime = seasonMap.values.flatten().maxOf { it[4] as ZonedDateTime }
episodeMappingCacheService.findAllSeo()?.groupBy { it.animeSlug }?.forEach { (animeSlug, episodes) ->
val seasonMap = episodes.groupBy { it.season }
val firstSeasonDateTime = seasonMap.values.flatten().maxOf { it.lastReleaseDateTime }

urls.add(URLDto("${Constant.baseUrl}/animes/$animeSlug", firstSeasonDateTime.formatDateTime()))

seasonMap.forEach { (season, seasonEpisodes) ->
val lastSeasonDateTime = seasonEpisodes.maxOf { it[4] as ZonedDateTime }
val lastSeasonDateTime = seasonEpisodes.maxOf { it.lastReleaseDateTime }
urls.add(URLDto("${Constant.baseUrl}/animes/$animeSlug/season-$season", lastSeasonDateTime.formatDateTime()))

seasonEpisodes.forEach {
val episodeType = it[2] as EpisodeType
val number = it[3] as Int
val episodeDateTime = it[4] as ZonedDateTime
urls.add(URLDto("${Constant.baseUrl}/animes/$animeSlug/season-$season/${episodeType.slug}-$number", episodeDateTime.formatDateTime()))
urls.add(URLDto("${Constant.baseUrl}/animes/$animeSlug/season-$season/${it.episodeType.slug}-${it.number}", it.lastReleaseDateTime.formatDateTime()))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fr.shikkanime.controllers.site

import com.google.inject.Inject
import fr.shikkanime.dtos.AnimeDto
import fr.shikkanime.dtos.animes.AnimeDto
import fr.shikkanime.entities.SortParameter
import fr.shikkanime.entities.enums.*
import fr.shikkanime.services.caches.AnimeCacheService
Expand Down
7 changes: 2 additions & 5 deletions src/main/kotlin/fr/shikkanime/converters/AbstractConverter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract class AbstractConverter<F, T> {
val converters = Constant.reflections.getSubTypesOf(AbstractConverter::class.java)

converters.forEach {
val (from, to) = (it.genericSuperclass as ParameterizedType).actualTypeArguments.map { argument -> argument as Class<*> }
val (from, to) = (it.genericSuperclass as ParameterizedType).actualTypeArguments.filterIsInstance<Class<*>>()
this.converters[Pair(from, to)] = Constant.injector.getInstance(it)
}
}
Expand All @@ -39,10 +39,7 @@ abstract class AbstractConverter<F, T> {
function.isAccessible = true

return try {
val invoke = function.call(abstractConverter, `object`, *args)
?: throw NullPointerException("Can not convert null to \"${to.simpleName}\"")
check(invoke is T) { "Can not convert \"${`object`.javaClass.simpleName}\" to \"${to.simpleName}\"" }
invoke
function.call(abstractConverter, `object`, *args) as? T ?: throw NullPointerException("Can not convert null to \"${to.simpleName}\"")
} catch (e: Exception) {
throw IllegalStateException(
"Can not convert \"${`object`.javaClass.simpleName}\" to \"${to.simpleName}\"",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package fr.shikkanime.converters.anime

import com.google.inject.Inject
import fr.shikkanime.converters.AbstractConverter
import fr.shikkanime.dtos.AnimeDto
import fr.shikkanime.dtos.animes.AnimeDto
import fr.shikkanime.entities.Anime
import fr.shikkanime.entities.Simulcast
import fr.shikkanime.services.AnimeService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package fr.shikkanime.converters.anime

import com.google.inject.Inject
import fr.shikkanime.converters.AbstractConverter
import fr.shikkanime.dtos.AnimeDto
import fr.shikkanime.dtos.animes.AnimeDto
import fr.shikkanime.dtos.AnimePlatformDto
import fr.shikkanime.dtos.SeasonDto
import fr.shikkanime.dtos.SimulcastDto
Expand All @@ -21,7 +21,7 @@ class AnimeToAnimeDtoConverter : AbstractConverter<Anime, AnimeDto>() {
@Converter
fun convert(from: Anime): AnimeDto {
val (audioLocales, seasons) = animeCacheService.findAudioLocalesAndSeasonsByAnimeCache(from)
?: Pair(emptyList(), emptyList())
?: Pair(emptySet(), emptyList())

return AnimeDto(
uuid = from.uuid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package fr.shikkanime.converters.episode_mapping

import com.google.inject.Inject
import fr.shikkanime.converters.AbstractConverter
import fr.shikkanime.dtos.AnimeDto
import fr.shikkanime.dtos.animes.AnimeDto
import fr.shikkanime.dtos.PlatformDto
import fr.shikkanime.dtos.mappings.EpisodeMappingDto
import fr.shikkanime.dtos.variants.EpisodeVariantWithoutMappingDto
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package fr.shikkanime.converters.member

import com.google.inject.Inject
import fr.shikkanime.converters.AbstractConverter
import fr.shikkanime.dtos.AnimeDto
import fr.shikkanime.dtos.MissedAnimeDto
import fr.shikkanime.dtos.animes.AnimeDto
import fr.shikkanime.dtos.animes.MissedAnimeDto
import fr.shikkanime.dtos.PageableDto
import fr.shikkanime.dtos.mappings.EpisodeMappingDto
import fr.shikkanime.dtos.member.RefreshMemberDto
Expand Down
8 changes: 0 additions & 8 deletions src/main/kotlin/fr/shikkanime/dtos/CalendarEpisodeDto.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package fr.shikkanime.dtos.animes

import java.time.ZonedDateTime
import java.util.UUID

data class AnimeAudioLocalesSeasonsDto(
val animeUuid: UUID,
val audioLocale: String,
val season: Int,
val lastReleaseDateTime: ZonedDateTime,
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package fr.shikkanime.dtos
package fr.shikkanime.dtos.animes

import fr.shikkanime.dtos.AnimePlatformDto
import fr.shikkanime.dtos.SeasonDto
import fr.shikkanime.dtos.SimulcastDto
import fr.shikkanime.dtos.enums.Status
import fr.shikkanime.dtos.mappings.EpisodeMappingWithoutAnimeDto
import fr.shikkanime.entities.enums.CountryCode
Expand All @@ -19,7 +22,7 @@ data class AnimeDto(
val banner: String,
val description: String? = null,
val simulcasts: List<SimulcastDto>? = null,
var audioLocales: List<String>? = null,
var audioLocales: Set<String>? = null,
var langTypes: List<LangType>? = null,
var seasons: List<SeasonDto>? = null,
var episodes: List<EpisodeMappingWithoutAnimeDto>? = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.shikkanime.dtos
package fr.shikkanime.dtos.animes

data class MissedAnimeDto(
val anime: AnimeDto,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package fr.shikkanime.dtos.mappings

import fr.shikkanime.dtos.AnimeDto
import fr.shikkanime.dtos.animes.AnimeDto
import fr.shikkanime.dtos.PlatformDto
import fr.shikkanime.dtos.enums.Status
import fr.shikkanime.dtos.variants.EpisodeVariantWithoutMappingDto
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package fr.shikkanime.dtos.mappings

import fr.shikkanime.entities.enums.EpisodeType
import java.time.ZonedDateTime

data class EpisodeMappingSeoDto(
val animeSlug: String,
val season: Int,
val episodeType: EpisodeType,
val number: Int,
val lastReleaseDateTime: ZonedDateTime
)
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package fr.shikkanime.dtos
package fr.shikkanime.dtos.mappings

import fr.shikkanime.entities.enums.EpisodeType
import java.util.*
import java.util.UUID

data class UpdateAllEpisodeMappingDto(
val uuids: List<UUID>,
val episodeType: EpisodeType?,
val season: Int?,
val forceUpdate: Boolean?
)
)
4 changes: 2 additions & 2 deletions src/main/kotlin/fr/shikkanime/dtos/member/RefreshMemberDto.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fr.shikkanime.dtos.member

import fr.shikkanime.dtos.AnimeDto
import fr.shikkanime.dtos.MissedAnimeDto
import fr.shikkanime.dtos.animes.AnimeDto
import fr.shikkanime.dtos.animes.MissedAnimeDto
import fr.shikkanime.dtos.PageableDto
import fr.shikkanime.dtos.mappings.EpisodeMappingDto

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package fr.shikkanime.dtos.variants

import fr.shikkanime.entities.enums.CountryCode
import fr.shikkanime.entities.enums.EpisodeType
import fr.shikkanime.entities.enums.Platform
import java.util.UUID

data class EpisodeVariantIdentifierDto(
val countryCode: CountryCode,
val animeUuid: UUID,
val platform: Platform,
val season: Int,
val episodeType: EpisodeType,
val number: Int,
val audioLocale: String,
val identifier: String,
)
14 changes: 14 additions & 0 deletions src/main/kotlin/fr/shikkanime/dtos/variants/VariantReleaseDto.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package fr.shikkanime.dtos.variants

import fr.shikkanime.entities.Anime
import fr.shikkanime.entities.EpisodeMapping
import fr.shikkanime.entities.enums.Platform
import java.time.ZonedDateTime

data class VariantReleaseDto(
val anime: Anime,
val episodeMapping: EpisodeMapping,
val releaseDateTime: ZonedDateTime,
val platform: Platform,
val audioLocale: String,
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package fr.shikkanime.dtos.weekly.v1

import fr.shikkanime.dtos.AnimeDto
import fr.shikkanime.dtos.animes.AnimeDto
import fr.shikkanime.dtos.PlatformDto
import fr.shikkanime.dtos.mappings.EpisodeMappingWithoutAnimeDto
import fr.shikkanime.entities.enums.EpisodeType
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package fr.shikkanime.dtos.weekly.v2

import fr.shikkanime.dtos.AnimeDto
import fr.shikkanime.dtos.animes.AnimeDto
import fr.shikkanime.dtos.PlatformDto
import fr.shikkanime.dtos.mappings.EpisodeMappingWithoutAnimeDto
import fr.shikkanime.entities.enums.EpisodeType
Expand Down
42 changes: 12 additions & 30 deletions src/main/kotlin/fr/shikkanime/jobs/FetchEpisodesJob.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fr.shikkanime.jobs

import fr.shikkanime.converters.AbstractConverter
import fr.shikkanime.dtos.variants.EpisodeVariantDto
import fr.shikkanime.dtos.variants.EpisodeVariantIdentifierDto
import fr.shikkanime.entities.Anime
import fr.shikkanime.entities.EpisodeMapping
import fr.shikkanime.entities.EpisodeVariant
Expand All @@ -12,10 +13,8 @@ import fr.shikkanime.services.MediaImage
import fr.shikkanime.services.caches.ConfigCacheService
import fr.shikkanime.utils.*
import jakarta.inject.Inject
import jakarta.persistence.Tuple
import java.io.ByteArrayOutputStream
import java.time.ZonedDateTime
import java.util.*
import java.util.logging.Level
import javax.imageio.ImageIO

Expand Down Expand Up @@ -52,20 +51,18 @@ class FetchEpisodesJob : AbstractJob {

if (!isInitialized) {
val variants = episodeVariantService.findAllTypeIdentifier()
val elements = variants.map { it[7] as String }.toSet()
identifiers.addAll(elements)
identifiers.addAll(variants.map { it.identifier })

Constant.abstractPlatforms.forEach {
it.hashCache.addAll(
variants.filter { variant -> (variant[2] as Platform) == it.getPlatform() }
variants.filter { variant -> variant.platform == it.getPlatform() }
.map { variant ->
".{2}-.{4}-(.*)-.{2}-.{2}".toRegex().find(variant[7] as String)!!.groupValues[1]
".{2}-.{4}-(.*)-.{2}-.{2}".toRegex().find(variant.identifier)!!.groupValues[1]
}
)
}

variants.forEach { typeIdentifiers.add(getTypeIdentifier(it)) }

isInitialized = true
}

Expand Down Expand Up @@ -111,40 +108,25 @@ class FetchEpisodesJob : AbstractJob {
sendToNetworks(savedEpisodes)
}

data class TypeIdentifier(
val country: CountryCode,
val anime: UUID,
val season: Int,
val episodeType: EpisodeType,
val number: Int,
val audioLocale: String
)

private fun getTypeIdentifier(input: Any): String {
val (country, anime, season, episodeType, number, audioLocale) = when (input) {
is Tuple -> TypeIdentifier(
input[0] as CountryCode,
input[1] as UUID,
input[4] as Int,
input[3] as EpisodeType,
input[5] as Int,
input[6] as String
)

is EpisodeVariant -> TypeIdentifier(
val episodeVariantIdentifierDto = when (input) {
is EpisodeVariantIdentifierDto -> input
is EpisodeVariant -> EpisodeVariantIdentifierDto(
input.mapping!!.anime!!.countryCode!!,
input.mapping!!.anime!!.uuid!!,
input.platform!!,
input.mapping!!.season!!,
input.mapping!!.episodeType!!,
input.mapping!!.number!!,
input.audioLocale!!
input.audioLocale!!,
input.identifier!!
)

else -> throw IllegalArgumentException("Invalid input type")
}

val langType = LangType.fromAudioLocale(country, audioLocale)
return "${country}_${anime}_${season}_${episodeType}_${number}_${langType}"
val langType = LangType.fromAudioLocale(episodeVariantIdentifierDto.countryCode, episodeVariantIdentifierDto.audioLocale)
return "${episodeVariantIdentifierDto.countryCode}_${episodeVariantIdentifierDto.animeUuid}_${episodeVariantIdentifierDto.season}_${episodeVariantIdentifierDto.episodeType}_${episodeVariantIdentifierDto.number}_${langType}"
}

private fun sendToNetworks(savedEpisodes: List<EpisodeVariant>) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/fr/shikkanime/modules/SEOManager.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package fr.shikkanime.modules

import fr.shikkanime.dtos.AnimeDto
import fr.shikkanime.dtos.animes.AnimeDto
import fr.shikkanime.dtos.mappings.EpisodeMappingDto
import fr.shikkanime.entities.LinkObject
import fr.shikkanime.entities.enums.ConfigPropertyKey
Expand Down
11 changes: 7 additions & 4 deletions src/main/kotlin/fr/shikkanime/repositories/AbstractRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ abstract class AbstractRepository<E : ShikkEntity> {
var total = 0L

if (scrollableResults.first() && scrollableResults.scroll((limit * page) - limit)) {
(0 until limit).forEach {
val get = scrollableResults.get() ?: return@forEach
require(get is C) { "Entity is not of type C" }
(0 until limit).forEach { _ ->
val get = scrollableResults.get() as? C ?: return@forEach
list.add(get)
if (!scrollableResults.next()) return@forEach
}
Expand Down Expand Up @@ -109,7 +108,11 @@ abstract class AbstractRepository<E : ShikkEntity> {

fun deleteAll() {
inTransaction {
it.createQuery("DELETE FROM ${getEntityClass().simpleName}").executeUpdate()
val cb = it.criteriaBuilder
val query = cb.createCriteriaDelete(getEntityClass())
query.from(getEntityClass())

it.createQuery(query).executeUpdate()
}
}
}
Loading

0 comments on commit 0516142

Please sign in to comment.