73 lines
2.2 KiB
C#
73 lines
2.2 KiB
C#
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
|
|
}
|
|
} |