Update transaction x jobs repository

This commit is contained in:
Samuele E. Locatelli (W11-AI)
2026-03-18 10:25:42 +01:00
parent 0214ebec10
commit d587ee3a45
2 changed files with 178 additions and 100 deletions
@@ -26,25 +26,40 @@ namespace EgwCoreLib.Lux.Data.Repository.Job
{
await using var dbCtx = await CreateContextAsync();
var dbResult = await dbCtx.DbSetJobStep
.FirstOrDefaultAsync(x => x.JobStepID == rec2del.JobStepID);
if (dbResult == null)
return false;
var list2Move = await dbCtx.DbSetJobStep
.Where(x => x.JobID == rec2del.JobID && x.Index > dbResult.Index)
.ToListAsync();
foreach (var item in list2Move)
// Wrap in transaction for atomicity (multi-row update + delete)
await using var tx = dbCtx.Database.BeginTransaction();
try
{
item.Index--;
dbCtx.Entry(item).State = EntityState.Modified;
var dbResult = await dbCtx.DbSetJobStep
.FirstOrDefaultAsync(x => x.JobStepID == rec2del.JobStepID);
if (dbResult == null)
return false;
var list2Move = await dbCtx.DbSetJobStep
.Where(x => x.JobID == rec2del.JobID && x.Index > dbResult.Index)
.ToListAsync();
foreach (var item in list2Move)
{
item.Index--;
dbCtx.Entry(item).State = EntityState.Modified;
}
dbCtx.DbSetJobStep.Remove(dbResult);
bool done = await dbCtx.SaveChangesAsync() > 0;
if (done)
tx.Commit();
return done;
}
catch
{
tx.Rollback();
throw;
}
dbCtx.DbSetJobStep.Remove(dbResult);
return await dbCtx.SaveChangesAsync() > 0;
}
public async Task<JobStepModel?> GetByIdAsync(int recId)
@@ -70,34 +85,50 @@ namespace EgwCoreLib.Lux.Data.Repository.Job
public async Task<bool> MoveAsync(JobStepModel selRec, bool moveUp)
{
await using var dbCtx = await CreateContextAsync();
var currRec = await dbCtx.DbSetJobStep
.FirstOrDefaultAsync(x => x.JobStepID == selRec.JobStepID);
if (currRec == null)
return false;
// Wrap in transaction for atomicity (multi-row update)
await using var tx = dbCtx.Database.BeginTransaction();
try
{
var currRec = await dbCtx.DbSetJobStep
.FirstOrDefaultAsync(x => x.JobStepID == selRec.JobStepID);
int numRec = await dbCtx.DbSetJobStep
.CountAsync(x => x.JobID == selRec.JobID);
if (currRec == null)
return false;
int newPos = moveUp ? currRec.Index - 1 : currRec.Index + 1;
int numRec = await dbCtx.DbSetJobStep
.CountAsync(x => x.JobID == selRec.JobID);
bool canMove = moveUp ? newPos > 0 : newPos <= numRec;
if (!canMove)
return false;
int newPos = moveUp ? currRec.Index - 1 : currRec.Index + 1;
var otherRec = await dbCtx.DbSetJobStep
.FirstOrDefaultAsync(x => x.JobID == selRec.JobID && x.Index == newPos);
bool canMove = moveUp ? newPos > 0 : newPos <= numRec;
if (!canMove)
return false;
if (otherRec == null)
return false;
var otherRec = await dbCtx.DbSetJobStep
.FirstOrDefaultAsync(x => x.JobID == selRec.JobID && x.Index == newPos);
otherRec.Index = currRec.Index;
currRec.Index = newPos;
if (otherRec == null)
return false;
dbCtx.Entry(otherRec).State = EntityState.Modified;
dbCtx.Entry(currRec).State = EntityState.Modified;
otherRec.Index = currRec.Index;
currRec.Index = newPos;
return await dbCtx.SaveChangesAsync() > 0;
dbCtx.Entry(otherRec).State = EntityState.Modified;
dbCtx.Entry(currRec).State = EntityState.Modified;
bool done = await dbCtx.SaveChangesAsync() > 0;
if (done)
tx.Commit();
return done;
}
catch
{
tx.Rollback();
throw;
}
}
public async Task<bool> UpdateAsync(JobStepModel entity)