From 9f30e52713e3931057f19d4234a547466b45574b Mon Sep 17 00:00:00 2001 From: glax Date: Thu, 31 Aug 2023 13:12:03 +0200 Subject: [PATCH] Added new API-Calls: POST: Jobs/StartNow DELETE: Jobs --- Tranga/Jobs/JobBoss.cs | 21 ++++++++++++++++++++- Tranga/Server.cs | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/Tranga/Jobs/JobBoss.cs b/Tranga/Jobs/JobBoss.cs index 384b899..8ae2a06 100644 --- a/Tranga/Jobs/JobBoss.cs +++ b/Tranga/Jobs/JobBoss.cs @@ -63,13 +63,32 @@ public class JobBoss : GlobalBase return GetJobsLike(mangaConnector?.name, publication?.internalId, chapter?.chapterNumber); } + public Job? GetJobById(string jobId) + { + if (this.jobs.FirstOrDefault(jjob => jjob.id == jobId) is { } job) + return job; + return null; + } + + public bool TryGetJobById(string jobId, out Job? job) + { + if (this.jobs.FirstOrDefault(jjob => jjob.id == jobId) is { } ret) + { + job = ret; + return true; + } + + job = null; + return false; + } + private bool QueueContainsJob(Job job) { mangaConnectorJobQueue.TryAdd(job.mangaConnector, new Queue()); return mangaConnectorJobQueue[job.mangaConnector].Contains(job); } - private void AddJobToQueue(Job job) + public void AddJobToQueue(Job job) { Log($"Adding Job to Queue. {job}"); mangaConnectorJobQueue.TryAdd(job.mangaConnector, new Queue()); diff --git a/Tranga/Server.cs b/Tranga/Server.cs index d223562..1ebbaea 100644 --- a/Tranga/Server.cs +++ b/Tranga/Server.cs @@ -225,6 +225,16 @@ public class Server : GlobalBase _parent._jobBoss.AddJob(new DownloadNewChapters(this, connector, manga, false)); SendResponse(HttpStatusCode.Accepted, response); break; + case "Jobs/StartNow": + if (!requestVariables.TryGetValue("jobId", out string? jobId) || + !_parent._jobBoss.TryGetJobById(jobId, out Job? job)) + { + SendResponse(HttpStatusCode.BadRequest, response); + break; + } + _parent._jobBoss.AddJobToQueue(job!); + SendResponse(HttpStatusCode.Accepted, response); + break; case "Settings/UpdateDownloadLocation": if (!requestVariables.TryGetValue("downloadLocation", out string? downloadLocation) || !requestVariables.TryGetValue("moveFiles", out string? moveFilesStr) || @@ -329,6 +339,16 @@ public class Server : GlobalBase string path = Regex.Match(request.Url!.LocalPath, @"[A-z0-9]+(\/[A-z0-9]+)*").Value; switch (path) { + case "Jobs": + if (!requestVariables.TryGetValue("jobID", out string? jobId) || + !_parent._jobBoss.TryGetJobById(jobId, out Job? job)) + { + SendResponse(HttpStatusCode.BadRequest, response); + break; + } + _parent._jobBoss.RemoveJob(job!); + SendResponse(HttpStatusCode.Accepted, response); + break; case "Jobs/DownloadChapter": if(!requestVariables.TryGetValue("connector", out connectorName) || !requestVariables.TryGetValue("internalId", out internalId) ||