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 #195 from Z-Jais/master
Browse files Browse the repository at this point in the history
Add PUT endpoint for updating episodes
  • Loading branch information
Ziedelth authored Oct 31, 2023
2 parents 70e63cf + 02b8862 commit bd8b441
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
54 changes: 54 additions & 0 deletions src/main/kotlin/fr/ziedelth/controllers/EpisodeController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,58 @@ class EpisodeController : AttachmentController<Episode>("/episodes") {
}
}
}

@APIRoute
private fun Route.update() {
put {
Logger.info("PUT $prefix")
if (isUnauthorized().await()) return@put

try {
val episode = call.receive<Episode>()
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)
}
}
}
}
4 changes: 2 additions & 2 deletions src/main/kotlin/fr/ziedelth/entities/Episode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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() || (
Expand Down

0 comments on commit bd8b441

Please sign in to comment.