Aggiunti repository + servizi x reports in LUX UI
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
|
||||
|
||||
namespace EgwCoreLib.Lux.Data.Repository.Report
|
||||
{
|
||||
public abstract class BaseRepRepository : IBaseRepRepository
|
||||
{
|
||||
#region Protected Fields
|
||||
|
||||
protected readonly IDbContextFactory<ReportContext> _ctxFactory;
|
||||
|
||||
#endregion Protected Fields
|
||||
|
||||
#region Protected Constructors
|
||||
|
||||
protected BaseRepRepository(IDbContextFactory<ReportContext> ctxFactory)
|
||||
=> _ctxFactory = ctxFactory;
|
||||
|
||||
#endregion Protected Constructors
|
||||
|
||||
#region Protected Methods
|
||||
|
||||
/// <summary>
|
||||
/// Creazione dbcontext per singola transazione
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected async Task<ReportContext> CreateContextAsync()
|
||||
=> await _ctxFactory.CreateDbContextAsync();
|
||||
|
||||
#endregion Protected Methods
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
|
||||
|
||||
namespace EgwCoreLib.Lux.Data.Repository.Report
|
||||
{
|
||||
public interface IBaseRepRepository
|
||||
{
|
||||
//Task<DataLayerContext> CreateContextAsync();
|
||||
//Task<bool> SaveChangesAsync(DataLayerContext ctx);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
namespace EgwCoreLib.Lux.Data.Repository.Report
|
||||
{
|
||||
public interface IReportRepository
|
||||
{
|
||||
#region Public Methods
|
||||
|
||||
/// <summary>
|
||||
/// Inserisce un nuovo record Report nel database.
|
||||
/// </summary>
|
||||
/// <param name="entity">Record da inserire</param>
|
||||
Task<bool> AddAsync(ReportModel entity);
|
||||
|
||||
/// <summary>
|
||||
/// Recupera l'elenco completo dei Report
|
||||
/// </summary>
|
||||
Task<List<ReportModel>> GetAllAsync();
|
||||
|
||||
/// <summary>
|
||||
/// Recupera un Report specifico per ID.
|
||||
/// </summary>
|
||||
/// <param name="currID">ID da recuperare</param>
|
||||
Task<ReportModel?> GetByIdAsync(int currID);
|
||||
|
||||
/// <summary>
|
||||
/// Aggiorna un record Report esistente nel database.
|
||||
/// </summary>
|
||||
/// <param name="entity">Record aggiornato</param>
|
||||
Task<bool> UpdateAsync(ReportModel entity);
|
||||
|
||||
#endregion Public Methods
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
namespace EgwCoreLib.Lux.Data.Repository.Report
|
||||
{
|
||||
public class ReportRepository : BaseRepRepository, IReportRepository
|
||||
{
|
||||
#region Public Constructors
|
||||
|
||||
public ReportRepository(IDbContextFactory<ReportContext> ctxFactory) : base(ctxFactory)
|
||||
{
|
||||
}
|
||||
|
||||
#endregion Public Constructors
|
||||
|
||||
#region Public Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<bool> AddAsync(ReportModel entity)
|
||||
{
|
||||
await using var dbCtx = await CreateContextAsync();
|
||||
// per ora disabilito salvataggio effettivo in prod
|
||||
#if DEBUG
|
||||
await dbCtx.DbSetReports.AddAsync(entity);
|
||||
#endif
|
||||
return await dbCtx.SaveChangesAsync() > 0;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<List<ReportModel>> GetAllAsync()
|
||||
{
|
||||
await using var dbCtx = await CreateContextAsync();
|
||||
return await dbCtx.DbSetReports
|
||||
.AsNoTracking()
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<ReportModel?> GetByIdAsync(int currID)
|
||||
{
|
||||
await using var dbCtx = await CreateContextAsync();
|
||||
return await dbCtx.DbSetReports.FirstOrDefaultAsync(x => x.ReportID == currID);
|
||||
}
|
||||
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<bool> UpdateAsync(ReportModel entity)
|
||||
{
|
||||
await using var dbCtx = await CreateContextAsync();
|
||||
// Recuperiamo l'entità tracciata dal context
|
||||
var trackedEntity = await dbCtx.DbSetReports.FirstOrDefaultAsync(x => x.ReportID == entity.ReportID);
|
||||
|
||||
if (trackedEntity != null)
|
||||
{
|
||||
// Aggiorna i valori dell'entità tracciata con quelli della nuova
|
||||
dbCtx.Entry(trackedEntity).CurrentValues.SetValues(entity);
|
||||
}
|
||||
else
|
||||
{
|
||||
dbCtx.DbSetReports.Update(entity);
|
||||
}
|
||||
return await dbCtx.SaveChangesAsync() > 0;
|
||||
}
|
||||
|
||||
#endregion Public Methods
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user