127 lines
4.5 KiB
C#
127 lines
4.5 KiB
C#
namespace EgwCoreLib.Lux.Data.Repository.Supplier
|
|
{
|
|
public class BuyOrderRepository : BaseRepository, IBuyOrderRepository
|
|
{
|
|
#region Public Constructors
|
|
|
|
public BuyOrderRepository(IDbContextFactory<DataLayerContext> ctxFactory) : base(ctxFactory)
|
|
{
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
/// <inheritdoc />
|
|
public async Task<bool> AddAsync(BuyOrderModel entity)
|
|
{
|
|
await using var dbCtx = await CreateContextAsync();
|
|
await dbCtx.DbSetBuyOrder.AddAsync(entity);
|
|
return await dbCtx.SaveChangesAsync() > 0;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<bool> DeleteAsync(BuyOrderModel entity)
|
|
{
|
|
await using var dbCtx = await CreateContextAsync();
|
|
dbCtx.DbSetBuyOrder.Remove(entity);
|
|
return await dbCtx.SaveChangesAsync() > 0;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<List<BuyOrderModel>> GetAllAsync()
|
|
{
|
|
await using var dbCtx = await CreateContextAsync();
|
|
return await dbCtx.DbSetBuyOrder
|
|
.Include(c => c.SupplierNav)
|
|
.Include(o => o.BuyOrderRowNav)
|
|
.ThenInclude(s => s.ItemNav)
|
|
.AsNoTracking()
|
|
.ToListAsync();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<BuyOrderModel?> GetByIdAsync(int recId)
|
|
{
|
|
await using var dbCtx = await CreateContextAsync();
|
|
return await dbCtx.DbSetBuyOrder
|
|
.Where(x => x.BuyOrderID == recId)
|
|
.Include(c => c.SupplierNav)
|
|
.Include(o => o.BuyOrderRowNav)
|
|
.ThenInclude(s => s.ItemNav)
|
|
.FirstOrDefaultAsync();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<List<BuyOrderModel>> GetFiltAsync(DateTime inizio, DateTime fine)
|
|
{
|
|
await using var dbCtx = await CreateContextAsync();
|
|
return await dbCtx.DbSetBuyOrder
|
|
.Where(x => x.Inserted >= inizio && x.Inserted <= fine)
|
|
.Include(c => c.SupplierNav)
|
|
.Include(o => o.BuyOrderRowNav)
|
|
.ThenInclude(s => s.ItemNav)
|
|
.AsNoTracking()
|
|
.ToListAsync();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<List<BuyOrderRowModel>> GetRowsAsync(int recId)
|
|
{
|
|
await using var dbCtx = await CreateContextAsync();
|
|
return await dbCtx.DbSetBuyOrderRow
|
|
.Where(x => x.BuyOrderID == recId)
|
|
.ToListAsync();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<bool> SaveRowsAsync(List<BuyOrderRowModel> rows)
|
|
{
|
|
// Add validation for null or empty list
|
|
if (rows == null || rows.Count == 0) return false;
|
|
|
|
await using var dbCtx = await CreateContextAsync();
|
|
|
|
// Wrap in transaction for atomicity (batch update multiple rows)
|
|
await using var tx = await dbCtx.Database.BeginTransactionAsync();
|
|
try
|
|
{
|
|
foreach (var row in rows)
|
|
dbCtx.Entry(row).State = EntityState.Modified;
|
|
|
|
bool done = await dbCtx.SaveChangesAsync() > 0;
|
|
|
|
if (done)
|
|
await tx.CommitAsync();
|
|
|
|
return done;
|
|
}
|
|
catch
|
|
{
|
|
await tx.RollbackAsync();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<bool> UpdateAsync(BuyOrderModel entity)
|
|
{
|
|
await using var dbCtx = await CreateContextAsync();
|
|
// Recuperiamo l'entità tracciata dal context
|
|
var trackedEntity = await dbCtx.DbSetBuyOrder.FirstOrDefaultAsync(x => x.BuyOrderID == entity.BuyOrderID);
|
|
|
|
if (trackedEntity != null)
|
|
{
|
|
// Aggiorna i valori dell'entità tracciata con quelli della nuova
|
|
dbCtx.Entry(trackedEntity).CurrentValues.SetValues(entity);
|
|
}
|
|
else
|
|
{
|
|
dbCtx.DbSetBuyOrder.Update(entity);
|
|
}
|
|
return await dbCtx.SaveChangesAsync() > 0;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |