From 80dc8fbe6541741bfc2db89e6005a22ef4c591e7 Mon Sep 17 00:00:00 2001 From: Glax Date: Thu, 25 Apr 2024 23:50:06 +0200 Subject: [PATCH] Resolves #176 Return 409 conflict if job already exists. --- Tranga/Jobs/JobBoss.cs | 4 +++- Tranga/Server/v2Jobs.cs | 15 +++++++++++---- docs/API_Calls_v2.md | 2 ++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/Tranga/Jobs/JobBoss.cs b/Tranga/Jobs/JobBoss.cs index 72d0c52..0ef6996 100644 --- a/Tranga/Jobs/JobBoss.cs +++ b/Tranga/Jobs/JobBoss.cs @@ -17,17 +17,19 @@ public class JobBoss : GlobalBase Log($"Next job in {jobs.MinBy(job => job.nextExecution)?.nextExecution.Subtract(DateTime.Now)} {jobs.MinBy(job => job.nextExecution)?.id}"); } - public void AddJob(Job job) + public bool AddJob(Job job) { if (ContainsJobLike(job)) { Log($"Already Contains Job {job}"); + return false; } else { Log($"Added {job}"); this.jobs.Add(job); UpdateJobFile(job); + return true; } } diff --git a/Tranga/Server/v2Jobs.cs b/Tranga/Server/v2Jobs.cs index 6581fb1..244347a 100644 --- a/Tranga/Server/v2Jobs.cs +++ b/Tranga/Server/v2Jobs.cs @@ -60,15 +60,22 @@ public partial class Server !TimeSpan.TryParse(intervalStr, out TimeSpan interval)) return new ValueTuple(HttpStatusCode.InternalServerError, "'interval' Parameter missing, or is not in correct format."); requestParameters.TryGetValue("language", out string? language); - _parent.jobBoss.AddJob(new DownloadNewChapters(this, ((Manga)manga).mangaConnector, ((Manga)manga).internalId, true, interval, language)); - return new ValueTuple(HttpStatusCode.OK, null); + return _parent.jobBoss.AddJob(new DownloadNewChapters(this, ((Manga)manga).mangaConnector, + ((Manga)manga).internalId, true, interval, language)) switch + { + true => new ValueTuple(HttpStatusCode.OK, null), + false => new ValueTuple(HttpStatusCode.Conflict, "Job already exists."), + }; case Job.JobType.UpdateMetaDataJob: if(!requestParameters.TryGetValue("internalId", out mangaId) || !_parent.TryGetPublicationById(mangaId, out manga) || manga is null) return new ValueTuple(HttpStatusCode.NotFound, "InternalId Parameter missing, or is not a valid ID."); - _parent.jobBoss.AddJob(new UpdateMetadata(this, ((Manga)manga).internalId)); - return new ValueTuple(HttpStatusCode.OK, null); + return _parent.jobBoss.AddJob(new UpdateMetadata(this, ((Manga)manga).internalId)) switch + { + true => new ValueTuple(HttpStatusCode.OK, null), + false => new ValueTuple(HttpStatusCode.Conflict, "Job already exists."), + }; case Job.JobType.DownloadNewChaptersJob: //TODO case Job.JobType.DownloadChapterJob: //TODO default: return new ValueTuple(HttpStatusCode.MethodNotAllowed, $"JobType {Enum.GetName(jobType)} is not supported."); diff --git a/docs/API_Calls_v2.md b/docs/API_Calls_v2.md index 3948cf9..fbbdfbc 100644 --- a/docs/API_Calls_v2.md +++ b/docs/API_Calls_v2.md @@ -276,7 +276,9 @@ Creates a Job. | StatusCode | Meaning | |------------|------------------------------------------| + | 200 | Job created. | | 404 | Parameter missing or could not be found. | + | 409 | Job already exists | | 500 | Error parsing interval |