95 lines
3.3 KiB
C#
95 lines
3.3 KiB
C#
namespace EgwCoreLib.Lux.Data.Repository.Warehouse
|
|
{
|
|
public class MatReqRepository : BaseRepository, IMatReqRepository
|
|
{
|
|
|
|
public MatReqRepository(IDbContextFactory<DataLayerContext> ctxFactory) : base(ctxFactory)
|
|
{
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<bool> AddAsync(MatReqModel entity)
|
|
{
|
|
await using var dbCtx = await CreateContextAsync();
|
|
await dbCtx.DbSetMaterialReq.AddAsync(entity);
|
|
return await dbCtx.SaveChangesAsync() > 0;
|
|
}
|
|
/// <inheritdoc />
|
|
public async Task<bool> AddManyAsync(List<MatReqModel> listEntity)
|
|
{
|
|
await using var dbCtx = await CreateContextAsync();
|
|
await dbCtx.DbSetMaterialReq.AddRangeAsync(listEntity);
|
|
return await dbCtx.SaveChangesAsync() > 0;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<bool> DeleteAsync(MatReqModel entity)
|
|
{
|
|
await using var dbCtx = await CreateContextAsync();
|
|
dbCtx.DbSetMaterialReq.Remove(entity);
|
|
return await dbCtx.SaveChangesAsync() > 0;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<MatReqModel?> GetByIdAsync(int recId)
|
|
{
|
|
await using var dbCtx = await CreateContextAsync();
|
|
return await dbCtx.DbSetMaterialReq
|
|
.Where(x => x.MatReqID == recId)
|
|
.Include(s => s.ItemNav)
|
|
.FirstOrDefaultAsync();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<List<MatReqModel>> GetFiltAsync(int? orderID, int? orderRowID, int? itemID, bool? processed)
|
|
{
|
|
await using var dbCtx = await CreateContextAsync();
|
|
|
|
var query = dbCtx.DbSetMaterialReq
|
|
.AsNoTracking()
|
|
.Include(i => i.ItemNav)
|
|
.Include(s => s.OrderRowNav)
|
|
.ThenInclude(o => o.OrderNav)
|
|
.AsQueryable();
|
|
|
|
if (processed != null)
|
|
query = query.Where(s => s.Processed == processed);
|
|
|
|
if (itemID != null)
|
|
query = query.Where(s => s.ItemID == itemID);
|
|
|
|
if (orderRowID != null)
|
|
{
|
|
query = query.Where(s => s.OrderRowID == orderRowID);
|
|
}
|
|
else
|
|
{
|
|
if (orderID != null)
|
|
query = query.Where(s => s.OrderRowNav.OrderID == orderID);
|
|
}
|
|
|
|
return await query.ToListAsync();
|
|
}
|
|
|
|
|
|
/// <inheritdoc />
|
|
public async Task<bool> UpdateAsync(MatReqModel entity)
|
|
{
|
|
await using var dbCtx = await CreateContextAsync();
|
|
// Recuperiamo l'entità tracciata dal context
|
|
var trackedEntity = await dbCtx.DbSetMaterialReq.FirstOrDefaultAsync(x => x.MatReqID == entity.MatReqID);
|
|
|
|
if (trackedEntity != null)
|
|
{
|
|
// Aggiorna i valori dell'entità tracciata con quelli della nuova
|
|
dbCtx.Entry(trackedEntity).CurrentValues.SetValues(entity);
|
|
}
|
|
else
|
|
{
|
|
dbCtx.DbSetMaterialReq.Update(entity);
|
|
}
|
|
return await dbCtx.SaveChangesAsync() > 0;
|
|
}
|
|
}
|
|
}
|