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
{
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<Job> 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<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>
/// Modify Job with ID
/// </summary>