From 5a0e0e9b2fce38660ea7c005cd07f7054c190cff Mon Sep 17 00:00:00 2001 From: Loretta Date: Tue, 30 Sep 2025 18:18:22 +0200 Subject: [PATCH] improvements, fixes, etc... --- .../Controllers/FruitBankDataController.cs | 17 ++++++++-- .../Domains/DataLayer/FruitBankDbContext.cs | 25 +++++++++++--- .../Services/FruitBankAttributeService.cs | 33 ++++++++++--------- 3 files changed, 53 insertions(+), 22 deletions(-) diff --git a/Nop.Plugin.Misc.AIPlugin/Controllers/FruitBankDataController.cs b/Nop.Plugin.Misc.AIPlugin/Controllers/FruitBankDataController.cs index 4a9ab00..a755561 100644 --- a/Nop.Plugin.Misc.AIPlugin/Controllers/FruitBankDataController.cs +++ b/Nop.Plugin.Misc.AIPlugin/Controllers/FruitBankDataController.cs @@ -1,5 +1,6 @@ using AyCode.Core.Loggers; using AyCode.Services.SignalRs; +using FruitBank.Common.Dtos; using FruitBank.Common.Entities; using FruitBank.Common.Interfaces; using FruitBank.Common.Loggers; @@ -198,7 +199,6 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Controllers public async Task> GetCustomerRolesByCustomerId(int customerId) { _logger.Detail($"GetCustomerRolesByCustomerId invoked; customerId: {customerId}"); - return await ctx.GetCustomerRolesByCustomerId(customerId).ToListAsync(); } @@ -206,8 +206,21 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Controllers public async Task> GetProductDtos() { _logger.Detail($"GetProductDtos invoked"); + return await ctx.GetAllProductDtos(false).ToListAsync(); + } - return await ctx.GetAllProducts().Select(c => new ProductDto(c)).ToListAsync(); + [SignalR(SignalRTags.GetAllMeasuringProductDtos)] + public async Task> GetAllMeasuringProductDtos() + { + _logger.Detail($"GetAllMeasuringProductDtos invoked"); + return await ctx.GetAllMeasuringProductDtos(false).ToListAsync(); + } + + [SignalR(SignalRTags.GetMeasuringProductDtoById)] + public async Task GetMeasuringProductDtoById(int productId) + { + _logger.Detail($"GetMeasuringProductDtoById invoked; productId: {productId}"); + return await ctx.GetMeasuringProductDtoByIdAsync(productId); } [SignalR(SignalRTags.AuthenticateUser)] diff --git a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/FruitBankDbContext.cs b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/FruitBankDbContext.cs index 64b0bf3..736c4a1 100644 --- a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/FruitBankDbContext.cs +++ b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/FruitBankDbContext.cs @@ -20,6 +20,8 @@ using Nop.Plugin.Misc.FruitBankPlugin.Services; using Nop.Services.Catalog; using Nop.Services.Common; using System.Transactions; +using FruitBank.Common.Dtos; +using Mango.Nop.Core.Dtos; namespace Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer; @@ -102,8 +104,20 @@ public class FruitBankDbContext : MgDbContextBase, IPartnerDbSet return query.Distinct(); } - public IQueryable GetAllProducts() - => Products.Table.Where(p => !p.Deleted).OrderBy(o => o.Name); + public IQueryable GetAllProducts(bool includeDeleted) + => Products.Table.Where(p => includeDeleted || !p.Deleted).OrderBy(o => o.Name); + + public IQueryable GetAllProductDtos(bool includeDeleted) + => GetAllProducts(includeDeleted).Select(product => new ProductDto(product)); + + public IAsyncEnumerable GetAllMeasuringProductDtos(bool includeDeleted) + => GetAllProducts(includeDeleted).SelectAwait(async product => new MeasuringProductDto(product, await GetMeasuringAttributeValuesByProductIdAsync(product.Id))); + + public async Task GetMeasuringProductDtoByIdAsync(int productId) + => await Products.Table.Where(product => product.Id == productId).SelectAwait(async product => new MeasuringProductDto(product, await GetMeasuringAttributeValuesByProductIdAsync(product.Id))).FirstOrDefaultAsync(); + + public async Task GetMeasuringAttributeValuesByProductIdAsync(int productId) + => await _fruitBankAttributeService.GetMeasuringAttributeValuesAsync(productId); public Task UpdateMeasuredShippingItemAsync(ShippingItem shippingItem) { @@ -178,12 +192,13 @@ public class FruitBankDbContext : MgDbContextBase, IPartnerDbSet { var measuringValues = new MeasuringAttributeValues { + Id = product.Id, NetWeight = productIdUnchanged ? shippingItem.MeasuredNetWeight - dbShippingItem.MeasuredNetWeight.GetValueOrDefault(0) : shippingItem.MeasuredNetWeight, GrossWeight = productIdUnchanged ? shippingItem.MeasuredGrossWeight - dbShippingItem.MeasuredGrossWeight.GetValueOrDefault(0) : shippingItem.MeasuredGrossWeight, IsMeasurable = shippingItem.IsMeasurable }; - await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync(product.Id, measuringValues, true); + await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync(measuringValues, true); } } @@ -201,8 +216,8 @@ public class FruitBankDbContext : MgDbContextBase, IPartnerDbSet if (!productIsMeasurable) return true; - var measuringValues = new MeasuringAttributeValues(-dbShippingItem.MeasuredNetWeight.GetValueOrDefault(0), -dbShippingItem.MeasuredGrossWeight.GetValueOrDefault(0), dbShippingItem.IsMeasurable); - await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync(product.Id, measuringValues, true); + var measuringValues = new MeasuringAttributeValues(product.Id, -dbShippingItem.MeasuredNetWeight.GetValueOrDefault(0), -dbShippingItem.MeasuredGrossWeight.GetValueOrDefault(0), dbShippingItem.IsMeasurable); + await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync(measuringValues, true); } else Logger.Warning($"product == null; dbShippingItem.ProductId: {dbShippingItem.ProductId}"); diff --git a/Nop.Plugin.Misc.AIPlugin/Services/FruitBankAttributeService.cs b/Nop.Plugin.Misc.AIPlugin/Services/FruitBankAttributeService.cs index 75410a3..7e09818 100644 --- a/Nop.Plugin.Misc.AIPlugin/Services/FruitBankAttributeService.cs +++ b/Nop.Plugin.Misc.AIPlugin/Services/FruitBankAttributeService.cs @@ -1,5 +1,6 @@ #nullable enable using FruitBank.Common.Interfaces; +using FruitBank.Common.Models; using Nop.Core; using Nop.Core.Domain.Catalog; using Nop.Core.Domain.Common; @@ -18,7 +19,7 @@ public class FruitBankAttributeService(IGenericAttributeService genericAttribute { var measuringAttributes = (await genericAttributeService.GetAttributesForEntityAsync(entityId, typeof(TEntity).Name)) .Where(ga => ga.StoreId == storeId && - (ga.Key is nameof(MeasuringAttributeValues.NetWeight) or nameof(MeasuringAttributeValues.GrossWeight) or nameof(MeasuringAttributeValues.IsMeasurable))) + (ga.Key is nameof(IMeasuringAttributeValues.NetWeight) or nameof(IMeasuringAttributeValues.GrossWeight) or nameof(IMeasuringAttributeValues.IsMeasurable))) .ToList(); return measuringAttributes; @@ -34,9 +35,10 @@ public class FruitBankAttributeService(IGenericAttributeService genericAttribute if (measuringAttributes.Count == 0) return null; var measuringAttributeValues = new MeasuringAttributeValues( - CommonHelper.To(measuringAttributes.FirstOrDefault(ga => ga.Key == nameof(MeasuringAttributeValues.NetWeight))), - CommonHelper.To(measuringAttributes.FirstOrDefault(ga => ga.Key == nameof(MeasuringAttributeValues.GrossWeight))), - CommonHelper.To(measuringAttributes.FirstOrDefault(ga => ga.Key == nameof(MeasuringAttributeValues.IsMeasurable)))); + entityId, + CommonHelper.To(measuringAttributes.FirstOrDefault(ga => ga.Key == nameof(IMeasuringAttributeValues.NetWeight))?.Value), + CommonHelper.To(measuringAttributes.FirstOrDefault(ga => ga.Key == nameof(IMeasuringAttributeValues.GrossWeight))?.Value), + CommonHelper.To(measuringAttributes.FirstOrDefault(ga => ga.Key == nameof(IMeasuringAttributeValues.IsMeasurable))?.Value)); return measuringAttributeValues; } @@ -44,30 +46,31 @@ public class FruitBankAttributeService(IGenericAttributeService genericAttribute public Task IsMeasurableEntityAsync(int entityId) => IsMeasurableEntityAsync(entityId, storeContext.GetCurrentStore().Id); public async Task IsMeasurableEntityAsync(int entityId, int storeId) { - var measurableAttribute = await GetGenericAttributeAsync(entityId, nameof(MeasuringAttributeValues.IsMeasurable), storeId); + var measurableAttribute = await GetGenericAttributeAsync(entityId, nameof(IMeasuringAttributeValues.IsMeasurable), storeId); return measurableAttribute != null && CommonHelper.To(measurableAttribute.Value); } - public Task InsertOrUpdateMeasuringAttributeValuesAsync(int entityId, MeasuringAttributeValues measuringAttributeValues, bool cumulativeWeightUpdate) - => InsertOrUpdateMeasuringAttributeValuesAsync(entityId, storeContext.GetCurrentStore().Id, measuringAttributeValues, cumulativeWeightUpdate); + public Task InsertOrUpdateMeasuringAttributeValuesAsync(IMeasuringAttributeValues measuringAttributeValues, bool cumulativeWeightUpdate) + => InsertOrUpdateMeasuringAttributeValuesAsync(measuringAttributeValues, cumulativeWeightUpdate, storeContext.GetCurrentStore().Id); - public async Task InsertOrUpdateMeasuringAttributeValuesAsync(int entityId, int storeId, MeasuringAttributeValues measuringAttributeValues, bool cumulativeWeightUpdate) + public async Task InsertOrUpdateMeasuringAttributeValuesAsync(IMeasuringAttributeValues measuringAttributeValues, bool cumulativeWeightUpdate, int storeId) { - if (!measuringAttributeValues.HasValues()) throw new Exception($"FruitBankAttributeService->InsertOrUpdateMeasuringAttributeValuesAsync; measuringAttributeValues.HasValues() == false; entityId: {entityId}; values: {measuringAttributeValues}"); + if (!measuringAttributeValues.HasValues()) throw new Exception($"FruitBankAttributeService->InsertOrUpdateMeasuringAttributeValuesAsync; measuringAttributeValues.HasValues() == false; keyGroup: {typeof(TEntity).Name} values: {measuringAttributeValues}"); + var entityId = measuringAttributeValues.Id; var measuringAttributes = await GetMeasuringAttributesAsync(entityId, storeId); - var netWeightAttribute = measuringAttributes.FirstOrDefault(ma => ma.Key == nameof(MeasuringAttributeValues.NetWeight)); + var netWeightAttribute = measuringAttributes.FirstOrDefault(ma => ma.Key == nameof(IMeasuringAttributeValues.NetWeight)); if (netWeightAttribute != null) await UpdateMeasuringWeightAttributeValueAsync(netWeightAttribute, measuringAttributeValues.NetWeight!.Value, cumulativeWeightUpdate); - else await InsertGenericAttributeAsync(entityId, nameof(MeasuringAttributeValues.NetWeight), measuringAttributeValues.NetWeight!.Value, storeId); + else await InsertGenericAttributeAsync(entityId, nameof(IMeasuringAttributeValues.NetWeight), measuringAttributeValues.NetWeight!.Value, storeId); - var grossWeightAttribute = measuringAttributes.FirstOrDefault(ma => ma.Key == nameof(MeasuringAttributeValues.GrossWeight)); + var grossWeightAttribute = measuringAttributes.FirstOrDefault(ma => ma.Key == nameof(IMeasuringAttributeValues.GrossWeight)); if (grossWeightAttribute != null) await UpdateMeasuringWeightAttributeValueAsync(grossWeightAttribute, measuringAttributeValues.GrossWeight!.Value, cumulativeWeightUpdate); - else await InsertGenericAttributeAsync(entityId, nameof(MeasuringAttributeValues.GrossWeight), measuringAttributeValues.GrossWeight!.Value, storeId); + else await InsertGenericAttributeAsync(entityId, nameof(IMeasuringAttributeValues.GrossWeight), measuringAttributeValues.GrossWeight!.Value, storeId); - var isMeasurableAttribute = measuringAttributes.FirstOrDefault(ma => ma.Key == nameof(MeasuringAttributeValues.IsMeasurable)); + var isMeasurableAttribute = measuringAttributes.FirstOrDefault(ma => ma.Key == nameof(IMeasuringAttributeValues.IsMeasurable)); if (isMeasurableAttribute != null) await UpdateGenericAttributeAsync(isMeasurableAttribute, measuringAttributeValues.IsMeasurable!.Value); - else await InsertGenericAttributeAsync(entityId, nameof(MeasuringAttributeValues.IsMeasurable), measuringAttributeValues.IsMeasurable!.Value, storeId); + else await InsertGenericAttributeAsync(entityId, nameof(IMeasuringAttributeValues.IsMeasurable), measuringAttributeValues.IsMeasurable!.Value, storeId); } private async Task UpdateMeasuringWeightAttributeValueAsync(GenericAttribute genericAttribute, double newWeightValue, bool cumulativeWeightUpdate)