From 9fca2d81abbbc8b36c822e40f1189bfb7518f7cf Mon Sep 17 00:00:00 2001 From: Glax Date: Fri, 7 Mar 2025 12:25:02 +0100 Subject: [PATCH] DeleteJob also deletes children --- API/Controllers/JobController.cs | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/API/Controllers/JobController.cs b/API/Controllers/JobController.cs index 1fbba01..8cbae82 100644 --- a/API/Controllers/JobController.cs +++ b/API/Controllers/JobController.cs @@ -174,14 +174,14 @@ public class JobController(PgsqlContext context) : Controller try { Job? ret = context.Jobs.Find(id); - switch (ret is not null) - { - case true: - context.Remove(ret); - context.SaveChanges(); - return Ok(); - case false: return NotFound(); - } + if(ret is null) + return NotFound(); + IQueryable children = GetChildJobs(id); + + context.RemoveRange(children); + context.Remove(ret); + context.SaveChanges(); + return Ok(); } catch (Exception e) { @@ -189,6 +189,15 @@ public class JobController(PgsqlContext context) : Controller } } + private IQueryable GetChildJobs(string parentJobId) + { + IQueryable children = context.Jobs.Where(j => j.ParentJobId == parentJobId); + foreach (Job child in children) + foreach (Job grandChild in GetChildJobs(child.JobId)) + children.Append(grandChild); + return children; + } + /// /// Modify Job with ID ///