Files
2026-04-24 17:42:49 +02:00

66 lines
2.0 KiB
C#

using Lux.Report.Data.DbModel;
namespace Lux.Report.Data.Repository
{
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
}
}