Aggiunta transaction x Offer, OfferRow, Order e GenVal

This commit is contained in:
Samuele E. Locatelli (W11-AI)
2026-03-18 10:59:04 +01:00
parent 3964eddd45
commit 66e8eae0f1
4 changed files with 317 additions and 193 deletions
@@ -26,29 +26,44 @@ namespace EgwCoreLib.Lux.Data.Repository.Utils
{
await using var dbCtx = await CreateContextAsync();
// 1. Recupero il record da eliminare
var dbResult = await dbCtx.DbSetGenVal
.FirstOrDefaultAsync(x => x.GenValID == rec2del.GenValID);
if (dbResult == null)
return false;
// 2. Recupero i record successivi da shiftare
var list2Move = await dbCtx.DbSetGenVal
.Where(x => x.ClassCod == rec2del.ClassCod && 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;
// 1. Recupero il record da eliminare
var dbResult = await dbCtx.DbSetGenVal
.FirstOrDefaultAsync(x => x.GenValID == rec2del.GenValID);
if (dbResult == null)
return false;
// 2. Recupero i record successivi da shiftare
var list2Move = await dbCtx.DbSetGenVal
.Where(x => x.ClassCod == rec2del.ClassCod && x.Index > dbResult.Index)
.ToListAsync();
foreach (var item in list2Move)
{
item.Index--;
dbCtx.Entry(item).State = EntityState.Modified;
}
// 3. Rimuovo il record
dbCtx.DbSetGenVal.Remove(dbResult);
// 4. Salvo tutto
bool done = await dbCtx.SaveChangesAsync() > 0;
if (done)
tx.Commit();
return done;
}
catch
{
tx.Rollback();
throw;
}
// 3. Rimuovo il record
dbCtx.DbSetGenVal.Remove(dbResult);
// 4. Salvo tutto
return await dbCtx.SaveChangesAsync() > 0;
}
public async Task<GenValueModel?> GetByIdAsync(int Id)
@@ -69,40 +84,56 @@ namespace EgwCoreLib.Lux.Data.Repository.Utils
public async Task<bool> MoveAsync(GenValueModel selRec, bool moveUp)
{
await using var dbCtx = await CreateContextAsync();
// 1. Recupero il record corrente
var currRec = await dbCtx.DbSetGenVal
.FirstOrDefaultAsync(x => x.GenValID == selRec.GenValID);
if (currRec == null)
return false;
// Wrap in transaction for atomicity (multi-row update - swap positions)
await using var tx = dbCtx.Database.BeginTransaction();
try
{
// 1. Recupero il record corrente
var currRec = await dbCtx.DbSetGenVal
.FirstOrDefaultAsync(x => x.GenValID == selRec.GenValID);
// 2. Numero totale record della classe
int numRec = await dbCtx.DbSetGenVal
.CountAsync(x => x.ClassCod == selRec.ClassCod);
if (currRec == null)
return false;
// 3. Calcolo nuova posizione
int newPos = moveUp ? currRec.Index - 1 : currRec.Index + 1;
// 2. Numero totale record della classe
int numRec = await dbCtx.DbSetGenVal
.CountAsync(x => x.ClassCod == selRec.ClassCod);
bool canMove = moveUp ? newPos > 0 : newPos <= numRec;
if (!canMove)
return false;
// 3. Calcolo nuova posizione
int newPos = moveUp ? currRec.Index - 1 : currRec.Index + 1;
// 4. Recupero il record da scambiare
var otherRec = await dbCtx.DbSetGenVal
.FirstOrDefaultAsync(x => x.ClassCod == selRec.ClassCod && x.Index == newPos);
bool canMove = moveUp ? newPos > 0 : newPos <= numRec;
if (!canMove)
return false;
if (otherRec == null)
return false;
// 4. Recupero il record da scambiare
var otherRec = await dbCtx.DbSetGenVal
.FirstOrDefaultAsync(x => x.ClassCod == selRec.ClassCod && x.Index == newPos);
// 5. Swap indici
otherRec.Index = currRec.Index;
currRec.Index = newPos;
if (otherRec == null)
return false;
dbCtx.Entry(otherRec).State = EntityState.Modified;
dbCtx.Entry(currRec).State = EntityState.Modified;
// 5. Swap indici
otherRec.Index = currRec.Index;
currRec.Index = newPos;
// 6. Salvo
return await dbCtx.SaveChangesAsync() > 0;
dbCtx.Entry(otherRec).State = EntityState.Modified;
dbCtx.Entry(currRec).State = EntityState.Modified;
// 6. Salvo
bool done = await dbCtx.SaveChangesAsync() > 0;
if (done)
tx.Commit();
return done;
}
catch
{
tx.Rollback();
throw;
}
}
public async Task<bool> UpdateAsync(GenValueModel entity)