using EgwCoreLib.Lux.Data.DbModel.Utils; using Microsoft.EntityFrameworkCore; namespace EgwCoreLib.Lux.Data.Repository.Utils { public class GenValRepository : BaseRepository, IGenValRepository { #region Public Constructors public GenValRepository(IDbContextFactory ctxFactory) : base(ctxFactory) { } #endregion Public Constructors #region Public Methods public async Task AddAsync(GenValueModel entity) { await using var dbCtx = await CreateContextAsync(); await dbCtx.DbSetGenVal.AddAsync(entity); return await dbCtx.SaveChangesAsync() > 0; } public async Task DeleteAsync(GenValueModel rec2del) { // Add validation for null entity if (rec2del == null) return false; await using var dbCtx = await CreateContextAsync(); // Wrap in transaction for atomicity (multi-row update + delete) await using var tx = await dbCtx.Database.BeginTransactionAsync(); try { // 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; } } public async Task GetByIdAsync(int Id) { await using var dbCtx = await CreateContextAsync(); return await dbCtx.DbSetGenVal.FirstOrDefaultAsync(x => x.GenValID == Id); } public async Task> GetFiltAsync(string codClass) { await using var dbCtx = await CreateContextAsync(); return await dbCtx.DbSetGenVal .Where(x => x.ClassCod == codClass) .AsNoTracking() .ToListAsync(); } public async Task MoveAsync(GenValueModel selRec, bool moveUp) { // Add validation for null entity if (selRec == null) return false; await using var dbCtx = await CreateContextAsync(); // Wrap in transaction for atomicity (multi-row update - swap positions) await using var tx = await dbCtx.Database.BeginTransactionAsync(); try { // 1. Recupero il record corrente var currRec = await dbCtx.DbSetGenVal .FirstOrDefaultAsync(x => x.GenValID == selRec.GenValID); if (currRec == null) return false; // 2. Numero totale record della classe int numRec = await dbCtx.DbSetGenVal .CountAsync(x => x.ClassCod == selRec.ClassCod); // 3. Calcolo nuova posizione int newPos = moveUp ? currRec.Index - 1 : currRec.Index + 1; bool canMove = moveUp ? newPos > 0 : newPos <= numRec; if (!canMove) return false; // 4. Recupero il record da scambiare var otherRec = await dbCtx.DbSetGenVal .FirstOrDefaultAsync(x => x.ClassCod == selRec.ClassCod && x.Index == newPos); if (otherRec == null) return false; // 5. Swap indici otherRec.Index = currRec.Index; currRec.Index = newPos; 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 UpdateAsync(GenValueModel entity) { await using var dbCtx = await CreateContextAsync(); // Recuperiamo l'entità tracciata dal context var trackedEntity = dbCtx.DbSetGenVal.FirstOrDefaultAsync(x => x.GenValID == entity.GenValID); if (trackedEntity != null) { // Aggiorna i valori dell'entità tracciata con quelli della nuova dbCtx.Entry(trackedEntity).CurrentValues.SetValues(entity); } else { dbCtx.DbSetGenVal.Update(entity); } return await dbCtx.SaveChangesAsync() > 0; } #endregion Public Methods } }