81 lines
2.8 KiB
C#
81 lines
2.8 KiB
C#
namespace EgwCoreLib.Lux.Data.Repository.Sales
|
|
{
|
|
public class DealerRepository : BaseRepository, IDealerRepository
|
|
{
|
|
#region Public Constructors
|
|
|
|
public DealerRepository(IDbContextFactory<DataLayerContext> ctxFactory) : base(ctxFactory)
|
|
{
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
/// <inheritdoc />
|
|
public async Task<bool> AddAsync(DealerModel entity)
|
|
{
|
|
await using var dbCtx = await CreateContextAsync();
|
|
await dbCtx.DbSetDealer.AddAsync(entity);
|
|
return await dbCtx.SaveChangesAsync() > 0;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<int> CountChildrenAsync(int DealerID)
|
|
{
|
|
await using var dbCtx = await CreateContextAsync();
|
|
return await dbCtx.DbSetOffer.CountAsync(x => x.DealerID == DealerID);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<bool> DeleteAsync(DealerModel entity)
|
|
{
|
|
await using var dbCtx = await CreateContextAsync();
|
|
dbCtx.DbSetDealer.Remove(entity);
|
|
return await dbCtx.SaveChangesAsync() > 0;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<List<DealerModel>> GetAllAsync()
|
|
{
|
|
await using var dbCtx = await CreateContextAsync();
|
|
return await dbCtx.DbSetDealer
|
|
.AsNoTracking()
|
|
.Include(o => o.OfferNav)
|
|
.Include(o => o.OrderNav)
|
|
.ToListAsync();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<DealerModel?> GetByIdAsync(int DealerID)
|
|
{
|
|
await using var dbCtx = await CreateContextAsync();
|
|
return await dbCtx.DbSetDealer
|
|
.Where(x => x.DealerID == DealerID)
|
|
.Include(o => o.OfferNav)
|
|
.Include(o => o.OrderNav)
|
|
.FirstOrDefaultAsync();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<bool> UpdateAsync(DealerModel entity)
|
|
{
|
|
await using var dbCtx = await CreateContextAsync();
|
|
// Recuperiamo l'entità tracciata dal context
|
|
var trackedEntity = await dbCtx.DbSetDealer.FirstOrDefaultAsync(x => x.DealerID == entity.DealerID);
|
|
|
|
if (trackedEntity != null)
|
|
{
|
|
// Aggiorna i valori dell'entità tracciata con quelli della nuova
|
|
dbCtx.Entry(trackedEntity).CurrentValues.SetValues(entity);
|
|
}
|
|
else
|
|
{
|
|
dbCtx.DbSetDealer.Update(entity);
|
|
}
|
|
return await dbCtx.SaveChangesAsync() > 0;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |