Migrazione gestione risorse in obj separati (Repository/Service)

This commit is contained in:
Samuele Locatelli
2026-03-17 15:23:05 +01:00
parent 3ec1511aec
commit 870cf85e3f
15 changed files with 231 additions and 180 deletions
@@ -0,0 +1,68 @@
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 = dbCtx.DbSetResource.Local.FirstOrDefault(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
}
}