92 lines
3.2 KiB
C#
92 lines
3.2 KiB
C#
namespace EgwCoreLib.Lux.Data.Repository.Supplier
|
|
{
|
|
public class SupplierRepository : BaseRepository, ISupplierRepository
|
|
{
|
|
#region Public Constructors
|
|
|
|
public SupplierRepository(IDbContextFactory<DataLayerContext> ctxFactory) : base(ctxFactory)
|
|
{
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
/// <inheritdoc />
|
|
public async Task<bool> AddAsync(SupplierModel entity)
|
|
{
|
|
await using var dbCtx = await CreateContextAsync();
|
|
await dbCtx.DbSetSupplier.AddAsync(entity);
|
|
return await dbCtx.SaveChangesAsync() > 0;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<bool> DeleteAsync(SupplierModel entity)
|
|
{
|
|
await using var dbCtx = await CreateContextAsync();
|
|
dbCtx.DbSetSupplier.Remove(entity);
|
|
return await dbCtx.SaveChangesAsync() > 0;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<List<SupplierModel>> GetAllAsync()
|
|
{
|
|
await using var dbCtx = await CreateContextAsync();
|
|
return await dbCtx.DbSetSupplier
|
|
.AsNoTracking()
|
|
.Include(s => s.QualifNav)
|
|
.ToListAsync();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<SupplierModel?> GetByIdAsync(int recId)
|
|
{
|
|
await using var dbCtx = await CreateContextAsync();
|
|
return await dbCtx.DbSetSupplier
|
|
.Where(x => x.SupplierID == recId)
|
|
.Include(s => s.QualifNav)
|
|
.FirstOrDefaultAsync();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<List<SupplierModel>> GetFiltAsync(string codGroup)
|
|
{
|
|
await using var dbCtx = await CreateContextAsync();
|
|
return await dbCtx.DbSetSupplier
|
|
.AsNoTracking()
|
|
.Include(s => s.QualifNav)
|
|
.Where(s => s.QualifNav.Any(q => q.CodGroup == codGroup))
|
|
.ToListAsync();
|
|
}
|
|
public async Task<List<SupplierModel>> GetFiltAsync(List<string> codGroups)
|
|
{
|
|
await using var dbCtx = await CreateContextAsync();
|
|
|
|
return await dbCtx.DbSetSupplier
|
|
.AsNoTracking()
|
|
.Where(s => s.QualifNav.Any(q => codGroups.Contains(q.CodGroup)))
|
|
.ToListAsync();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<bool> UpdateAsync(SupplierModel entity)
|
|
{
|
|
await using var dbCtx = await CreateContextAsync();
|
|
// Recuperiamo l'entità tracciata dal context
|
|
var trackedEntity = await dbCtx.DbSetSupplier.FirstOrDefaultAsync(x => x.SupplierID == entity.SupplierID);
|
|
|
|
if (trackedEntity != null)
|
|
{
|
|
// Aggiorna i valori dell'entità tracciata con quelli della nuova
|
|
dbCtx.Entry(trackedEntity).CurrentValues.SetValues(entity);
|
|
}
|
|
else
|
|
{
|
|
dbCtx.DbSetSupplier.Update(entity);
|
|
}
|
|
return await dbCtx.SaveChangesAsync() > 0;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |