Files
lux/EgwCoreLib.Lux.Data/Repository/Items/SellingItemRepository.cs
T
2026-03-23 17:17:31 +01:00

80 lines
2.9 KiB
C#

using EgwCoreLib.Lux.Data.DbModel.Items;
using EgwMultiEngineManager.Data;
using Microsoft.EntityFrameworkCore;
using static EgwCoreLib.Lux.Core.Enums;
namespace EgwCoreLib.Lux.Data.Repository.Items
{
public class SellingItemRepository : BaseRepository, ISellingItemRepository
{
#region Public Constructors
public SellingItemRepository(IDbContextFactory<DataLayerContext> ctxFactory) : base(ctxFactory)
{
}
#endregion Public Constructors
#region Public Methods
public async Task<bool> AddAsync(SellingItemModel entity)
{
await using var dbCtx = await CreateContextAsync();
await dbCtx.DbSetSellItem.AddAsync(entity);
return await dbCtx.SaveChangesAsync() > 0;
}
public async Task<bool> DeleteAsync(SellingItemModel entity)
{
await using var dbCtx = await CreateContextAsync();
dbCtx.DbSetSellItem.Remove(entity);
return await dbCtx.SaveChangesAsync() > 0;
}
public async Task<List<SellingItemModel>> GetByEnvirAsync(Constants.EXECENVIRONMENTS envir)
{
await using var dbCtx = await CreateContextAsync();
return await dbCtx.DbSetSellItem
.Where(x => x.Envir == envir)
.AsNoTracking()
.ToListAsync();
}
public async Task<SellingItemModel?> GetByIdAsync(int recId)
{
await using var dbCtx = await CreateContextAsync();
return await dbCtx.DbSetSellItem
.Where(x => x.SellingItemID == recId)
.FirstOrDefaultAsync();
}
public async Task<List<SellingItemModel>> GetFiltAsync(Constants.EXECENVIRONMENTS envir, ItemSourceType sourceType)
{
await using var dbCtx = await CreateContextAsync();
return await dbCtx.DbSetSellItem
.Where(x => (x.Envir == envir || envir == Constants.EXECENVIRONMENTS.NULL) && (sourceType == ItemSourceType.ND || x.SourceType == sourceType))
.AsNoTracking()
.ToListAsync();
}
public async Task<bool> UpdateAsync(SellingItemModel entity)
{
await using var dbCtx = await CreateContextAsync();
// Recuperiamo l'entità tracciata dal context
var trackedEntity = await dbCtx.DbSetSellItem.FirstOrDefaultAsync(x => x.SellingItemID == entity.SellingItemID);
if (trackedEntity != null)
{
// Aggiorna i valori dell'entità tracciata con quelli della nuova
dbCtx.Entry(trackedEntity).CurrentValues.SetValues(entity);
}
else
{
dbCtx.DbSetSellItem.Update(entity);
}
return await dbCtx.SaveChangesAsync() > 0;
}
#endregion Public Methods
}
}