From 02b88624b00902630b1d03588a78fffdeddf9cb6 Mon Sep 17 00:00:00 2001 From: Ziedelth Date: Tue, 31 Oct 2023 16:15:41 +0100 Subject: [PATCH] Add PUT endpoint for updating episodes --- .../ziedelth/controllers/EpisodeController.kt | 54 +++++++++++++++++++ .../kotlin/fr/ziedelth/entities/Episode.kt | 4 +- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/fr/ziedelth/controllers/EpisodeController.kt b/src/main/kotlin/fr/ziedelth/controllers/EpisodeController.kt index d3918d9..fcea06d 100644 --- a/src/main/kotlin/fr/ziedelth/controllers/EpisodeController.kt +++ b/src/main/kotlin/fr/ziedelth/controllers/EpisodeController.kt @@ -201,4 +201,58 @@ class EpisodeController : AttachmentController("/episodes") { } } } + + @APIRoute + private fun Route.update() { + put { + Logger.info("PUT $prefix") + if (isUnauthorized().await()) return@put + + try { + val episode = call.receive() + var savedEpisode = episodeRepository.find(episode.uuid) + + if (savedEpisode == null) { + call.respond(HttpStatusCode.NotFound, "Episode not found") + return@put + } + + if (episode.episodeType?.uuid != null) { + val foundEpisodeType = episodeTypeRepository.find(episode.episodeType!!.uuid) + + if (foundEpisodeType == null) { + call.respond(HttpStatusCode.NotFound, "Episode type not found") + return@put + } + + savedEpisode.episodeType = foundEpisodeType + } + + if (episode.langType?.uuid != null) { + val foundLangType = langTypeRepository.find(episode.langType!!.uuid) + + if (foundLangType == null) { + call.respond(HttpStatusCode.NotFound, "Lang type not found") + return@put + } + + savedEpisode.langType = foundLangType + } + + if (episode.season != null) { + savedEpisode.season = episode.season + } + + if (episode.duration != -1L) { + savedEpisode.duration = episode.duration + } + + savedEpisode = episodeRepository.save(savedEpisode) + episodeService.invalidateAll() + call.respond(HttpStatusCode.OK, savedEpisode) + } catch (e: Exception) { + printError(call, e) + } + } + } } diff --git a/src/main/kotlin/fr/ziedelth/entities/Episode.kt b/src/main/kotlin/fr/ziedelth/entities/Episode.kt index 309d805..2129998 100644 --- a/src/main/kotlin/fr/ziedelth/entities/Episode.kt +++ b/src/main/kotlin/fr/ziedelth/entities/Episode.kt @@ -65,7 +65,7 @@ class Episode( @Column(nullable = false) val releaseDate: String = Calendar.getInstance().toISO8601(), @Column(nullable = false) - val season: Int? = null, + var season: Int? = null, @Column(nullable = false) var number: Int? = null, @Column(nullable = true) @@ -75,7 +75,7 @@ class Episode( @Column(nullable = false, columnDefinition = "TEXT") val image: String? = null, @Column(nullable = false) - val duration: Long = -1 + var duration: Long = -1 ) : Serializable { fun isNotValid(): Boolean = platform.isNullOrNotValid() || anime.isNullOrNotValid() || episodeType.isNullOrNotValid() || langType.isNullOrNotValid() || hash.isNullOrBlank() || (