Files
lux/EgwCoreLib.Lux.Data/Repository/Cost/ResourceRepository.cs
T
Samuele E. Locatelli (W11-AI) 42b30e1941 Continuo con Config e Cost
2026-03-25 15:54:30 +01:00

70 lines
2.3 KiB
C#

namespace EgwCoreLib.Lux.Data.Repository.Cost
{
public class ResourceRepository : BaseRepository, IResourceRepository
{
#region Public Constructors
public ResourceRepository(IDbContextFactory<DataLayerContext> ctxFactory) : base(ctxFactory)
{
}
#endregion Public Constructors
#region Public Methods
/// <inheritdoc />
public async Task<bool> AddAsync(ResourceModel entity)
{
await using var dbCtx = await CreateContextAsync();
await dbCtx.DbSetResource.AddAsync(entity);
return await dbCtx.SaveChangesAsync() > 0;
}
/// <inheritdoc />
public async Task<bool> DeleteAsync(ResourceModel entity)
{
await using var dbCtx = await CreateContextAsync();
dbCtx.DbSetResource.Remove(entity);
return await dbCtx.SaveChangesAsync() > 0;
}
/// <inheritdoc />
public async Task<List<ResourceModel>> GetAllAsync()
{
await using var dbCtx = await CreateContextAsync();
return await dbCtx.DbSetResource
.Include(d => d.DriverNav)
.Include(j => j.JobStepNav)
.AsNoTracking()
.ToListAsync();
}
/// <inheritdoc />
public async Task<ResourceModel?> GetByIdAsync(int recId)
{
await using var dbCtx = await CreateContextAsync();
return await dbCtx.DbSetResource.FirstOrDefaultAsync(x => x.ResourceID == recId);
}
/// <inheritdoc />
public async Task<bool> UpdateAsync(ResourceModel entity)
{
await using var dbCtx = await CreateContextAsync();
// Recuperiamo l'entità tracciata dal context
var trackedEntity = await dbCtx.DbSetResource.FirstOrDefaultAsync(x => x.ResourceID == entity.ResourceID);
if (trackedEntity != null)
{
// Aggiorna i valori dell'entità tracciata con quelli della nuova
dbCtx.Entry(trackedEntity).CurrentValues.SetValues(entity);
}
else
{
dbCtx.DbSetResource.Update(entity);
}
return await dbCtx.SaveChangesAsync() > 0;
}
#endregion Public Methods
}
}