diff --git a/EgwCoreLib.Lux.Data/EgwCoreLib.Lux.Data.csproj b/EgwCoreLib.Lux.Data/EgwCoreLib.Lux.Data.csproj index fed9128f..b84c34a9 100644 --- a/EgwCoreLib.Lux.Data/EgwCoreLib.Lux.Data.csproj +++ b/EgwCoreLib.Lux.Data/EgwCoreLib.Lux.Data.csproj @@ -54,6 +54,7 @@ + diff --git a/EgwCoreLib.Lux.Data/Repository/Supplier/ISupplierRepository.cs b/EgwCoreLib.Lux.Data/Repository/Supplier/ISupplierRepository.cs new file mode 100644 index 00000000..9d28c48f --- /dev/null +++ b/EgwCoreLib.Lux.Data/Repository/Supplier/ISupplierRepository.cs @@ -0,0 +1,45 @@ +namespace EgwCoreLib.Lux.Data.Repository.Supplier +{ + public interface ISupplierRepository : IBaseRepository + { + #region Public Methods + + /// + /// Inserisce un nuovo record Supplier nel database. + /// + /// Record da inserire + Task AddAsync(SupplierModel entity); + + /// + /// Elimina un record Supplier dal database. + /// + /// Record da eliminare + Task DeleteAsync(SupplierModel entity); + + /// + /// Elenco completo supplier + /// + /// + Task> GetAllAsync(); + + /// + /// Recupera un record Supplier specifico per ID. + /// + /// ID dell'Supplier da recuperare + Task GetByIdAsync(int recId); + + /// + /// Recupera gli Supplier filtrati per gruppo e tipo classe. + /// + /// Codice del gruppo filtro (vuoto = tutti) + Task> GetFiltAsync(string CodGroup); + + /// + /// Aggiorna un record Supplier esistente nel database. + /// + /// Record aggiornato + Task UpdateAsync(SupplierModel entity); + + #endregion Public Methods + } +} \ No newline at end of file diff --git a/EgwCoreLib.Lux.Data/Repository/Supplier/SupplierRepository.cs b/EgwCoreLib.Lux.Data/Repository/Supplier/SupplierRepository.cs new file mode 100644 index 00000000..cdacf562 --- /dev/null +++ b/EgwCoreLib.Lux.Data/Repository/Supplier/SupplierRepository.cs @@ -0,0 +1,80 @@ +namespace EgwCoreLib.Lux.Data.Repository.Supplier +{ + public class SupplierRepository : BaseRepository, ISupplierRepository + { + #region Public Constructors + + public SupplierRepository(IDbContextFactory ctxFactory) : base(ctxFactory) + { + } + + #endregion Public Constructors + + #region Public Methods + + /// + public async Task AddAsync(SupplierModel entity) + { + await using var dbCtx = await CreateContextAsync(); + await dbCtx.DbSetSupplier.AddAsync(entity); + return await dbCtx.SaveChangesAsync() > 0; + } + + /// + public async Task DeleteAsync(SupplierModel entity) + { + await using var dbCtx = await CreateContextAsync(); + dbCtx.DbSetSupplier.Remove(entity); + return await dbCtx.SaveChangesAsync() > 0; + } + + /// + public async Task> GetAllAsync() + { + await using var dbCtx = await CreateContextAsync(); + return await dbCtx.DbSetSupplier + .AsNoTracking() + .ToListAsync(); + } + + /// + public async Task GetByIdAsync(int recId) + { + await using var dbCtx = await CreateContextAsync(); + return await dbCtx.DbSetSupplier + .Where(x => x.SupplierID == recId) + .FirstOrDefaultAsync(); + } + + /// + public async Task> 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(); + } + + /// + public async Task 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 + } +} \ No newline at end of file