Implementazione interfaccia + repository x supplier

This commit is contained in:
Samuele Locatelli
2026-04-14 11:40:01 +02:00
parent 9a111d2b8a
commit acf43e8f43
3 changed files with 126 additions and 0 deletions
@@ -54,6 +54,7 @@
<ItemGroup>
<Folder Include="DbModel\Engine\" />
<Folder Include="Migrations\" />
<Folder Include="Services\Supplier\" />
</ItemGroup>
<ItemGroup>
@@ -0,0 +1,45 @@
namespace EgwCoreLib.Lux.Data.Repository.Supplier
{
public interface ISupplierRepository : IBaseRepository
{
#region Public Methods
/// <summary>
/// Inserisce un nuovo record Supplier nel database.
/// </summary>
/// <param name="entity">Record da inserire</param>
Task<bool> AddAsync(SupplierModel entity);
/// <summary>
/// Elimina un record Supplier dal database.
/// </summary>
/// <param name="entity">Record da eliminare</param>
Task<bool> DeleteAsync(SupplierModel entity);
/// <summary>
/// Elenco completo supplier
/// </summary>
/// <returns></returns>
Task<List<SupplierModel>> GetAllAsync();
/// <summary>
/// Recupera un record Supplier specifico per ID.
/// </summary>
/// <param name="recId">ID dell'Supplier da recuperare</param>
Task<SupplierModel?> GetByIdAsync(int recId);
/// <summary>
/// Recupera gli Supplier filtrati per gruppo e tipo classe.
/// </summary>
/// <param name="CodGroup">Codice del gruppo filtro (vuoto = tutti)</param>
Task<List<SupplierModel>> GetFiltAsync(string CodGroup);
/// <summary>
/// Aggiorna un record Supplier esistente nel database.
/// </summary>
/// <param name="entity">Record aggiornato</param>
Task<bool> UpdateAsync(SupplierModel entity);
#endregion Public Methods
}
}
@@ -0,0 +1,80 @@
namespace EgwCoreLib.Lux.Data.Repository.Supplier
{
public class SupplierRepository : BaseRepository, ISupplierRepository
{
#region Public Constructors
public SupplierRepository(IDbContextFactory<DataLayerContext> ctxFactory) : base(ctxFactory)
{
}
#endregion Public Constructors
#region Public Methods
/// <inheritdoc />
public async Task<bool> AddAsync(SupplierModel entity)
{
await using var dbCtx = await CreateContextAsync();
await dbCtx.DbSetSupplier.AddAsync(entity);
return await dbCtx.SaveChangesAsync() > 0;
}
/// <inheritdoc />
public async Task<bool> DeleteAsync(SupplierModel entity)
{
await using var dbCtx = await CreateContextAsync();
dbCtx.DbSetSupplier.Remove(entity);
return await dbCtx.SaveChangesAsync() > 0;
}
/// <inheritdoc />
public async Task<List<SupplierModel>> GetAllAsync()
{
await using var dbCtx = await CreateContextAsync();
return await dbCtx.DbSetSupplier
.AsNoTracking()
.ToListAsync();
}
/// <inheritdoc />
public async Task<SupplierModel?> GetByIdAsync(int recId)
{
await using var dbCtx = await CreateContextAsync();
return await dbCtx.DbSetSupplier
.Where(x => x.SupplierID == recId)
.FirstOrDefaultAsync();
}
/// <inheritdoc />
public async Task<List<SupplierModel>> GetFiltAsync(string CodGroup)
{
// FixMet - ToDo: da implementare secondo CodGroup da relazione n-n da implementare
await using var dbCtx = await CreateContextAsync();
return await dbCtx.DbSetSupplier
.AsNoTracking()
.ToListAsync();
}
/// <inheritdoc />
public async Task<bool> UpdateAsync(SupplierModel entity)
{
await using var dbCtx = await CreateContextAsync();
// Recuperiamo l'entità tracciata dal context
var trackedEntity = await dbCtx.DbSetSupplier.FirstOrDefaultAsync(x => x.SupplierID == entity.SupplierID);
if (trackedEntity != null)
{
// Aggiorna i valori dell'entità tracciata con quelli della nuova
dbCtx.Entry(trackedEntity).CurrentValues.SetValues(entity);
}
else
{
dbCtx.DbSetSupplier.Update(entity);
}
return await dbCtx.SaveChangesAsync() > 0;
}
#endregion Public Methods
}
}