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

Commit

Permalink
Merge pull request #208 from Z-Jais/master
Browse files Browse the repository at this point in the history
Update and add profile
  • Loading branch information
Ziedelth authored Nov 19, 2023
2 parents 2d3ca1c + 2decfd5 commit 6d7b097
Show file tree
Hide file tree
Showing 43 changed files with 1,515 additions and 209 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ out/
/test.http
**/firebase_key.json
**/plugins/
/data/logs/
2 changes: 2 additions & 0 deletions data/hibernate.cfg.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@
<property name="show_sql">false</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.region.factory_class">jcache</property>
</session-factory>
</hibernate-configuration>
56 changes: 54 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
<main.class>fr.ziedelth.ApplicationKt</main.class>
<junit-jupiter.version>5.10.1</junit-jupiter.version>
<hibernate.version>6.3.1.Final</hibernate.version>

<sonar.coverage.exclusions>
**/fr/ziedelth/dtos/**,
Expand Down Expand Up @@ -107,15 +108,66 @@
<artifactId>ktor-server-caching-headers-jvm</artifactId>
<version>${ktor_version}</version>
</dependency>
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-server-auth</artifactId>
<version>${ktor_version}</version>
</dependency>
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-server-auth-jwt</artifactId>
<version>${ktor_version}</version>
</dependency>
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-server-auth-jvm</artifactId>
<version>${ktor_version}</version>
</dependency>
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-server-auth-jwt-jvm</artifactId>
<version>${ktor_version}</version>
</dependency>
<dependency>
<groupId>io.github.smiley4</groupId>
<artifactId>ktor-swagger-ui</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback_version}</version>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.3.1.Final</version>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-jcache</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.10.8</version>
<exclusions>
<exclusion>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>4.0.4</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
Expand Down Expand Up @@ -199,7 +251,7 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<version>3.1.1</version>
<configuration>
<mainClass>${main.class}</mainClass>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,23 @@ const val UNKNOWN_MESSAGE_ERROR = "Unknown error"
const val MISSING_PARAMETERS_MESSAGE_ERROR = "Missing parameters"

open class AbstractController<T : Serializable>(open val prefix: String) {
@Deprecated("Please use JWT Tokens")
data class FilterData(
val animes: List<UUID> = listOf(), // Animes in watchlist
val episodes: List<UUID> = listOf(), // Episodes seen
val episodeTypes: List<UUID> = listOf(), // Episode types wanted to see
val langTypes: List<UUID> = listOf(), // Lang types wanted to see
)

val entityName: String = ((javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<*>).simpleName
val entityName: String =
((javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<*>).simpleName

@Deprecated("Please use JWT Tokens")
fun decode(watchlist: String): FilterData {
if (watchlist.isBlank()) {
return FilterData()
}

val filterData = Constant.gson.fromJson(Decoder.fromGzip(watchlist), FilterData::class.java)
Logger.config("Episodes: ${filterData.episodes.size} - Animes: ${filterData.animes.size}")
return filterData
Expand Down
48 changes: 25 additions & 23 deletions src/main/kotlin/fr/ziedelth/controllers/AnimeController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import fr.ziedelth.services.EpisodeService
import fr.ziedelth.utils.ImageCache
import fr.ziedelth.utils.Logger
import fr.ziedelth.utils.routes.Authorized
import fr.ziedelth.utils.routes.BodyParam
import fr.ziedelth.utils.routes.Path
import fr.ziedelth.utils.routes.Response
import fr.ziedelth.utils.routes.method.Delete
Expand Down Expand Up @@ -79,40 +80,41 @@ class AnimeController : AttachmentController<Anime>("/animes") {

@Path("/missing/page/{page}/limit/{limit}")
@Post
private fun paginationMissing(body: String, page: Int, limit: Int): Response {
return Response.ok(animeRepository.getMissingAnimes(decode(body), page, limit))
@Deprecated("Replaced by JWT at /profiles/missing/...")
private fun paginationMissing(@BodyParam watchlist: String, page: Int, limit: Int): Response {
return Response.ok(animeRepository.getMissingAnimes(decode(watchlist), page, limit))
}

@Path
@Post
@Authorized
private fun save(body: Anime): Response {
val countryUuid = body.country!!.uuid
body.country =
private fun save(@BodyParam anime: Anime): Response {
val countryUuid = anime.country!!.uuid
anime.country =
countryRepository.find(countryUuid) ?: return Response(HttpStatusCode.BadRequest, "Country not found")

val countryTag = body.country!!.tag!!
val countryTag = anime.country!!.tag!!

if (body.isNullOrNotValid()) {
if (anime.isNullOrNotValid()) {
Logger.warning(MISSING_PARAMETERS_MESSAGE_ERROR)
Logger.warning(body.toString())
Logger.warning(anime.toString())
return Response(HttpStatusCode.BadRequest, MISSING_PARAMETERS_MESSAGE_ERROR)
}

if (animeRepository.findOneByName(countryTag, body.name!!)?.country?.uuid == countryUuid) {
if (animeRepository.findOneByName(countryTag, anime.name!!)?.country?.uuid == countryUuid) {
Logger.warning("$entityName already exists")
return Response(HttpStatusCode.Conflict, "$entityName already exists")
}

val hash = body.hash()
val hash = anime.hash()

if (animeRepository.findByHash(countryTag, hash) != null) {
Logger.warning("$entityName already exists")
return Response(HttpStatusCode.Conflict, "$entityName already exists")
}

body.hashes.add(hash)
val savedAnime = animeRepository.save(body)
anime.hashes.add(hash)
val savedAnime = animeRepository.save(anime)
ImageCache.cache(savedAnime.uuid, savedAnime.image!!)
animeService.invalidateAll()
return Response.created(savedAnime)
Expand All @@ -121,24 +123,24 @@ class AnimeController : AttachmentController<Anime>("/animes") {
@Path
@Put
@Authorized
private fun update(body: Anime): Response {
private fun update(@BodyParam anime: Anime): Response {
var savedAnime =
animeRepository.find(body.uuid) ?: return Response(HttpStatusCode.NotFound, ANIME_NOT_FOUND_ERROR)
animeRepository.find(anime.uuid) ?: return Response(HttpStatusCode.NotFound, ANIME_NOT_FOUND_ERROR)

if (!body.name.isNullOrBlank()) {
if (animeRepository.findOneByName(savedAnime.country!!.tag!!, body.name!!) != null) {
if (!anime.name.isNullOrBlank()) {
if (animeRepository.findOneByName(savedAnime.country!!.tag!!, anime.name!!) != null) {
return Response(HttpStatusCode.Conflict, "Another anime with the name exist!")
}

savedAnime.name = body.name
savedAnime.name = anime.name
}

if (!body.description.isNullOrBlank()) {
savedAnime.description = body.description
if (!anime.description.isNullOrBlank()) {
savedAnime.description = anime.description
}

if (body.simulcasts.isNotEmpty()) {
val savedSimulcasts = body.simulcasts.mapNotNull { simulcastRepository.find(it.uuid) }
if (anime.simulcasts.isNotEmpty()) {
val savedSimulcasts = anime.simulcasts.mapNotNull { simulcastRepository.find(it.uuid) }

savedAnime.simulcasts.clear()
savedAnime.simulcasts.addAll(savedSimulcasts)
Expand All @@ -153,9 +155,9 @@ class AnimeController : AttachmentController<Anime>("/animes") {
@Path("/merge")
@Put
@Authorized
private fun merge(body: Array<UUID>): Response {
private fun merge(@BodyParam animeIds: Array<UUID>): Response {
// Get anime
val animes = body.mapNotNull { animeRepository.find(it) }
val animes = animeIds.mapNotNull { animeRepository.find(it) }

if (animes.isEmpty()) {
Logger.warning(ANIME_NOT_FOUND_ERROR)
Expand Down
9 changes: 5 additions & 4 deletions src/main/kotlin/fr/ziedelth/controllers/AyaneController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fr.ziedelth.dtos.AyaneDto
import fr.ziedelth.events.AyaneReleaseEvent
import fr.ziedelth.utils.plugins.PluginManager
import fr.ziedelth.utils.routes.Authorized
import fr.ziedelth.utils.routes.BodyParam
import fr.ziedelth.utils.routes.Path
import fr.ziedelth.utils.routes.Response
import fr.ziedelth.utils.routes.method.Post
Expand All @@ -13,15 +14,15 @@ class AyaneController : AbstractController<AyaneDto>("/ayane") {
@Path
@Post
@Authorized
private fun save(body: AyaneDto): Response {
if (body.message.isBlank() || body.images.isEmpty()) {
private fun save(@BodyParam ayaneDto: AyaneDto): Response {
if (ayaneDto.message.isBlank() || ayaneDto.images.isEmpty()) {
return Response(HttpStatusCode.BadRequest, MISSING_PARAMETERS_MESSAGE_ERROR)
}

Thread {
PluginManager.callEvent(AyaneReleaseEvent(body))
PluginManager.callEvent(AyaneReleaseEvent(ayaneDto))
}.start()

return Response.created(body)
return Response.created(ayaneDto)
}
}
11 changes: 6 additions & 5 deletions src/main/kotlin/fr/ziedelth/controllers/CountryController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import fr.ziedelth.entities.isNullOrNotValid
import fr.ziedelth.repositories.CountryRepository
import fr.ziedelth.services.CountryService
import fr.ziedelth.utils.routes.Authorized
import fr.ziedelth.utils.routes.BodyParam
import fr.ziedelth.utils.routes.Path
import fr.ziedelth.utils.routes.Response
import fr.ziedelth.utils.routes.method.Get
Expand All @@ -28,20 +29,20 @@ class CountryController : AbstractController<Country>("/countries") {
@Path
@Post
@Authorized
private fun save(body: Country): Response {
if (body.isNullOrNotValid()) {
private fun save(@BodyParam country: Country): Response {
if (country.isNullOrNotValid()) {
return Response(HttpStatusCode.BadRequest, MISSING_PARAMETERS_MESSAGE_ERROR)
}

if (countryRepository.exists("tag", body.tag)) {
if (countryRepository.exists("tag", country.tag)) {
return Response(HttpStatusCode.Conflict, "$entityName already exists")
}

if (countryRepository.exists("name", body.name)) {
if (countryRepository.exists("name", country.name)) {
return Response(HttpStatusCode.Conflict, "$entityName already exists")
}

val savedCountry = countryRepository.save(body)
val savedCountry = countryRepository.save(country)
countryService.invalidateAll()
return Response.created(savedCountry)
}
Expand Down
38 changes: 23 additions & 15 deletions src/main/kotlin/fr/ziedelth/controllers/EpisodeController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import fr.ziedelth.utils.ImageCache
import fr.ziedelth.utils.SortType
import fr.ziedelth.utils.plugins.PluginManager
import fr.ziedelth.utils.routes.Authorized
import fr.ziedelth.utils.routes.BodyParam
import fr.ziedelth.utils.routes.Path
import fr.ziedelth.utils.routes.Response
import fr.ziedelth.utils.routes.method.Get
Expand Down Expand Up @@ -65,8 +66,8 @@ class EpisodeController : AttachmentController<Episode>("/episodes") {

@Path("/watchlist/page/{page}/limit/{limit}")
@Post
private fun paginationWatchlist(body: String, page: Int, limit: Int): Response {
return Response.ok(episodeRepository.getByPageWithListFilter(decode(body), page, limit))
private fun paginationWatchlist(@BodyParam watchlist: String, page: Int, limit: Int): Response {
return Response.ok(episodeRepository.getByPageWithListFilter(decode(watchlist), page, limit))
}

private fun merge(episode: Episode) {
Expand Down Expand Up @@ -126,8 +127,8 @@ class EpisodeController : AttachmentController<Episode>("/episodes") {
@Path("/multiple")
@Post
@Authorized
private fun saveMultiple(body: Array<Episode>): Response {
val episodes = body.filter { !episodeRepository.exists("hash", it.hash!!) }
private fun saveMultiple(@BodyParam episodesToSave: Array<Episode>): Response {
val episodes = episodesToSave.filter { !episodeRepository.exists("hash", it.hash!!) }

if (episodes.isEmpty()) {
return Response(HttpStatusCode.NoContent, "All requested episodes already exists!")
Expand Down Expand Up @@ -158,25 +159,32 @@ class EpisodeController : AttachmentController<Episode>("/episodes") {
@Path
@Put
@Authorized
private fun update(body: Episode): Response {
var savedEpisode = episodeRepository.find(body.uuid) ?: return Response(HttpStatusCode.NotFound, "Episode not found")

if (body.episodeType?.uuid != null) {
val foundEpisodeType = episodeTypeRepository.find(body.episodeType!!.uuid) ?: return Response(HttpStatusCode.NotFound, "Episode type not found")
private fun update(@BodyParam episode: Episode): Response {
var savedEpisode =
episodeRepository.find(episode.uuid) ?: return Response(HttpStatusCode.NotFound, "Episode not found")

if (episode.episodeType?.uuid != null) {
val foundEpisodeType = episodeTypeRepository.find(episode.episodeType!!.uuid) ?: return Response(
HttpStatusCode.NotFound,
"Episode type not found"
)
savedEpisode.episodeType = foundEpisodeType
}

if (body.langType?.uuid != null) {
val foundLangType = langTypeRepository.find(body.langType!!.uuid) ?: return Response(HttpStatusCode.NotFound, "Lang type not found")
if (episode.langType?.uuid != null) {
val foundLangType = langTypeRepository.find(episode.langType!!.uuid) ?: return Response(
HttpStatusCode.NotFound,
"Lang type not found"
)
savedEpisode.langType = foundLangType
}

if (body.season != null) {
savedEpisode.season = body.season
if (episode.season != null) {
savedEpisode.season = episode.season
}

if (body.duration != -1L) {
savedEpisode.duration = body.duration
if (episode.duration != -1L) {
savedEpisode.duration = episode.duration
}

savedEpisode = episodeRepository.save(savedEpisode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import fr.ziedelth.entities.isNullOrNotValid
import fr.ziedelth.repositories.EpisodeTypeRepository
import fr.ziedelth.services.EpisodeTypeService
import fr.ziedelth.utils.routes.Authorized
import fr.ziedelth.utils.routes.BodyParam
import fr.ziedelth.utils.routes.Path
import fr.ziedelth.utils.routes.Response
import fr.ziedelth.utils.routes.method.Get
Expand All @@ -28,16 +29,16 @@ class EpisodeTypeController : AbstractController<EpisodeType>("/episodetypes") {
@Path
@Post
@Authorized
private fun save(body: EpisodeType): Response {
if (body.isNullOrNotValid()) {
private fun save(@BodyParam episodeType: EpisodeType): Response {
if (episodeType.isNullOrNotValid()) {
return Response(HttpStatusCode.BadRequest, MISSING_PARAMETERS_MESSAGE_ERROR)
}

if (episodeTypeRepository.exists("name", body.name)) {
if (episodeTypeRepository.exists("name", episodeType.name)) {
return Response(HttpStatusCode.Conflict, "$entityName already exists")
}

val savedEpisodeType = episodeTypeRepository.save(body)
val savedEpisodeType = episodeTypeRepository.save(episodeType)
episodeTypeService.invalidateAll()
return Response.created(savedEpisodeType)
}
Expand Down
Loading

0 comments on commit 6d7b097

Please sign in to comment.