Files
lux/EgwCoreLib.Lux.Data/Repository/Cost/ResourceRepository.cs
T
2026-03-23 17:17:31 +01:00

68 lines
2.3 KiB
C#

using EgwCoreLib.Lux.Data.DbModel.Cost;
using Microsoft.EntityFrameworkCore;
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
public async Task<bool> AddAsync(ResourceModel entity)
{
await using var dbCtx = await CreateContextAsync();
await dbCtx.DbSetResource.AddAsync(entity);
return await dbCtx.SaveChangesAsync() > 0;
}
public async Task<bool> DeleteAsync(ResourceModel entity)
{
await using var dbCtx = await CreateContextAsync();
dbCtx.DbSetResource.Remove(entity);
return await dbCtx.SaveChangesAsync() > 0;
}
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();
}
public async Task<ResourceModel?> GetByIdAsync(int recId)
{
await using var dbCtx = await CreateContextAsync();
return await dbCtx.DbSetResource.FirstOrDefaultAsync(x => x.ResourceID == recId);
}
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
}
}