Inizio porting repository x GenVal...

This commit is contained in:
Samuele Locatelli
2026-03-14 11:51:23 +01:00
parent 2228dbe439
commit fbc88aef65
3 changed files with 112 additions and 27 deletions
@@ -0,0 +1,73 @@
using EgwCoreLib.Lux.Data.DbModel.Utils;
using Microsoft.EntityFrameworkCore;
namespace EgwCoreLib.Lux.Data.Repository.Utils
{
internal class GenValRepository : BaseRepository, IGenValRepository
{
#region Public Constructors
public GenValRepository(DataLayerContext db) : base(db)
{
}
#endregion Public Constructors
#region Public Methods
public void Add(GenValueModel entity) => _db.DbSetGenVal.Add(entity);
public async Task<bool> DeleteAsync(GenValueModel rec2del)
{
// 1. Recupero il record da eliminare
var dbResult = await _db.DbSetGenVal
.FirstOrDefaultAsync(x => x.GenValID == rec2del.GenValID);
if (dbResult == null)
return false;
// 2. Recupero i record successivi da shiftare
var list2Move = await _db.DbSetGenVal
.Where(x => x.ClassCod == rec2del.ClassCod && x.Index > dbResult.Index)
.ToListAsync();
foreach (var item in list2Move)
{
item.Index--;
_db.Entry(item).State = EntityState.Modified;
}
// 3. Rimuovo il record
_db.DbSetGenVal.Remove(dbResult);
// 4. Salvo tutto
return await _db.SaveChangesAsync() > 0;
}
public async Task<List<GenValueModel>> GetFiltAsync(string codClass)
{
return await _db.DbSetGenVal
.Where(x => x.ClassCod == codClass)
.AsNoTracking()
.ToListAsync();
}
public void Update(GenValueModel entity)
{
// Recuperiamo l'entità tracciata dal context
var trackedEntity = _db.DbSetGenVal.Local.FirstOrDefault(x => x.GenValID == entity.GenValID);
if (trackedEntity != null)
{
// Aggiorna i valori dell'entità tracciata con quelli della nuova
_db.Entry(trackedEntity).CurrentValues.SetValues(entity);
}
else
{
_db.DbSetGenVal.Update(entity);
}
}
#endregion Public Methods
}
}
@@ -0,0 +1,19 @@
using EgwCoreLib.Lux.Data.DbModel.Utils;
namespace EgwCoreLib.Lux.Data.Repository.Utils
{
public interface IGenValRepository : IBaseRepository
{
#region Public Methods
void Add(GenValueModel entity);
Task<bool> DeleteAsync(GenValueModel entity);
Task<List<GenValueModel>> GetFiltAsync(string codClass);
void Update(GenValueModel entity);
#endregion Public Methods
}
}