DeleteJob also deletes children

This commit is contained in:
Glax 2025-03-07 12:25:02 +01:00
parent 949c0cc16d
commit 9fca2d81ab

View File

@ -174,14 +174,14 @@ public class JobController(PgsqlContext context) : Controller
try try
{ {
Job? ret = context.Jobs.Find(id); Job? ret = context.Jobs.Find(id);
switch (ret is not null) if(ret is null)
{ return NotFound();
case true: IQueryable<Job> children = GetChildJobs(id);
context.Remove(ret);
context.SaveChanges(); context.RemoveRange(children);
return Ok(); context.Remove(ret);
case false: return NotFound(); context.SaveChanges();
} return Ok();
} }
catch (Exception e) catch (Exception e)
{ {
@ -189,6 +189,15 @@ public class JobController(PgsqlContext context) : Controller
} }
} }
private IQueryable<Job> GetChildJobs(string parentJobId)
{
IQueryable<Job> 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;
}
/// <summary> /// <summary>
/// Modify Job with ID /// Modify Job with ID
/// </summary> /// </summary>