diff --git a/Nop.Plugin.Misc.AIPlugin/Components/ProductAttributesViewComponent.cs b/Nop.Plugin.Misc.AIPlugin/Components/ProductAttributesViewComponent.cs index 8958f9c..6425006 100644 --- a/Nop.Plugin.Misc.AIPlugin/Components/ProductAttributesViewComponent.cs +++ b/Nop.Plugin.Misc.AIPlugin/Components/ProductAttributesViewComponent.cs @@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Mvc; using Nop.Core; using Nop.Core.Domain.Catalog; using Nop.Plugin.Misc.FruitBankPlugin.Models; +using Nop.Plugin.Misc.FruitBankPlugin.Services; using Nop.Services.Common; using Nop.Web.Areas.Admin.Models.Catalog; using Nop.Web.Framework.Components; @@ -14,60 +15,34 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Components [ViewComponent(Name = "ProductAttributes")] public class ProductAttributesViewComponent : NopViewComponent { - private const string NET_WEIGHT_KEY = nameof(IMeasuringAttributeValues.NetWeight); - private const string TARE_KEY = nameof(ITare.Tare); - private const string IS_MEASURABLE_KEY = nameof(IMeasuringAttributeValues.IsMeasurable); - - private readonly IGenericAttributeService _genericAttributeService; + private readonly FruitBankAttributeService _fruitBankAttributeService; private readonly IWorkContext _workContext; private readonly IStoreContext _storeContext; - public ProductAttributesViewComponent( - IGenericAttributeService genericAttributeService, - IWorkContext workContext, - IStoreContext storeContext) + public ProductAttributesViewComponent(FruitBankAttributeService fruitBankAttributeService, IWorkContext workContext, IStoreContext storeContext) { - _genericAttributeService = genericAttributeService; _workContext = workContext; _storeContext = storeContext; + _fruitBankAttributeService = fruitBankAttributeService; } public async Task InvokeAsync(string widgetZone, object additionalData) { + if (additionalData is not ProductModel productModel) return Content(""); + var model = new ProductAttributesModel { ProductId = productModel.Id }; - if (additionalData is not ProductModel product) - return Content(""); - - var model = new ProductAttributesModel + if (model.ProductId > 0) { - ProductId = product.Id - }; + var measuringAttributeValues = await _fruitBankAttributeService.GetMeasuringAttributeValuesAsync(model.ProductId); + if (measuringAttributeValues != null) + { + model.IsMeasurable = measuringAttributeValues.IsMeasurable; + model.NetWeight = measuringAttributeValues.NetWeight; + } - //get store scope - var storeScope = await _storeContext.GetCurrentStoreAsync(); - - - if (product.Id > 0) - { - // Load existing values - var dbProduct = new Core.Domain.Catalog.Product { Id = product.Id }; - - //var dbMesaurable = await _genericAttributeService.GetAttributeAsync(dbProduct, IS_MEASURABLE_KEY, storeScope.Id); - //var dbNetWeight = await _genericAttributeService.GetAttributeAsync(dbProduct, NET_WEIGHT_KEY, storeScope.Id); - //var dbIncomingQuantity = await _genericAttributeService.GetAttributeAsync(dbProduct, "IncomingQuantity", storeScope.Id); - - model.IsMeasurable = await _genericAttributeService - .GetAttributeAsync(dbProduct, IS_MEASURABLE_KEY, storeScope.Id); - - model.NetWeight = await _genericAttributeService - .GetAttributeAsync(dbProduct, NET_WEIGHT_KEY, storeScope.Id); - - model.IncomingQuantity = await _genericAttributeService - .GetAttributeAsync(dbProduct, "IncomingQuantity", storeScope.Id); - - model.Tare = await _genericAttributeService - .GetAttributeAsync(dbProduct, TARE_KEY, storeScope.Id); + model.Tare = await _fruitBankAttributeService.GetGenericAttributeValueAsync(model.ProductId, nameof(ITare.Tare)); + model.IncomingQuantity = await _fruitBankAttributeService.GetGenericAttributeValueAsync(model.ProductId, "IncomingQuantity"); } return View("~/Plugins/Misc.FruitBankPlugin/Views/ProductAttributes.cshtml", model); diff --git a/Nop.Plugin.Misc.AIPlugin/Domains/EventConsumers/FruitBankEventConsumer.cs b/Nop.Plugin.Misc.AIPlugin/Domains/EventConsumers/FruitBankEventConsumer.cs index 1829cf2..d234afc 100644 --- a/Nop.Plugin.Misc.AIPlugin/Domains/EventConsumers/FruitBankEventConsumer.cs +++ b/Nop.Plugin.Misc.AIPlugin/Domains/EventConsumers/FruitBankEventConsumer.cs @@ -19,8 +19,8 @@ using System.Globalization; namespace Nop.Plugin.Misc.FruitBankPlugin.Domains.EventConsumers; -public class FruitBankEventConsumer(IHttpContextAccessor httpContextAccessor, FruitBankDbContext ctx, FruitBankAttributeService fruitBankAttributeService, IStoreContext storeContext, IEnumerable logWriters, IGenericAttributeService genericAttributeService) : - MgEventConsumer(httpContextAccessor, logWriters), +public class FruitBankEventConsumer(IHttpContextAccessor httpContextAcc, FruitBankDbContext ctx, FruitBankAttributeService fruitBankAttributeService, IEnumerable logWriters) : + MgEventConsumer(httpContextAcc, logWriters), IConsumer>, IConsumer>, IConsumer>, @@ -36,7 +36,7 @@ public class FruitBankEventConsumer(IHttpContextAccessor httpContextAccessor, Fr { var product = eventMessage.Entity; - await SaveCustomAttributesAsync(eventMessage.Entity); + await SaveProductCustomAttributesAsync(eventMessage.Entity); var isMeasurableProduct = await fruitBankAttributeService.IsMeasurableEntityAsync(product.Id); @@ -51,61 +51,42 @@ public class FruitBankEventConsumer(IHttpContextAccessor httpContextAccessor, Fr await base.HandleEventAsync(eventMessage); } - public async Task HandleEventAsync(EntityInsertedEvent eventMessage) + public override async Task HandleEventAsync(EntityInsertedEvent eventMessage) { - await SaveCustomAttributesAsync(eventMessage.Entity); + await SaveProductCustomAttributesAsync(eventMessage.Entity); + await base.HandleEventAsync(eventMessage); } - private async Task SaveCustomAttributesAsync(Product product) + private async Task SaveProductCustomAttributesAsync(Product product) { - if (product == null) - return; + if (product == null) return; - var form = httpContextAccessor.HttpContext?.Request?.Form; - if (form == null || !form.Any()) - return; + var form = HttpContextAccessor.HttpContext?.Request?.Form; + if (form == null || form.Count == 0) return; - var store = await storeContext.GetCurrentStoreAsync(); - - // Save IsMeasurable - if (form.ContainsKey(nameof(IMeasuringAttributeValues.IsMeasurable))) + if (form.ContainsKey(nameof(IMeasurable.IsMeasurable)) && form.ContainsKey(nameof(IMeasuringNetWeight.NetWeight))) { - var isMeasurable = form[nameof(IMeasuringAttributeValues.IsMeasurable)].ToString().Contains("true"); - await genericAttributeService.SaveAttributeAsync(product, nameof(IMeasuringAttributeValues.IsMeasurable), isMeasurable, store.Id); - //Akkor ez kell? - Á. - //await fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync(product.Id, 0, 0, isMeasurable, false); - } + var isMeasurable = form[nameof(IMeasurable.IsMeasurable)].ToString().Contains("true"); + //var isMeasurable = CommonHelper.To(form[nameof(IMeasurable.IsMeasurable)].ToString()); + var netWeight = CommonHelper.To(form[nameof(IMeasuringNetWeight.NetWeight)].ToString()); - // Save NetWeight - if (form.ContainsKey(nameof(IMeasuringAttributeValues.NetWeight))) - { - var netWeightStr = form[nameof(IMeasuringAttributeValues.NetWeight)].ToString(); - if (!string.IsNullOrWhiteSpace(netWeightStr) && double.TryParse(netWeightStr, NumberStyles.Float, CultureInfo.InvariantCulture, out var netWeight)) - { - await genericAttributeService.SaveAttributeAsync(product, nameof(IMeasuringAttributeValues.NetWeight), netWeight, store.Id); - //Akkor ez kell? - Á. - //await fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync(product.Id, 0, 0, , false); - } - } - - // Save IncomingQuantity - if (form.ContainsKey("IncomingQuantity")) - { - var incomingQtyStr = form["IncomingQuantity"].ToString(); - if (!string.IsNullOrWhiteSpace(incomingQtyStr) && int.TryParse(incomingQtyStr, out var incomingQuantity)) - { - await genericAttributeService.SaveAttributeAsync(product, "IncomingQuantity", incomingQuantity, store.Id); - } + await fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync(product.Id, netWeight, 0, isMeasurable, false); } if (form.ContainsKey(nameof(ITare.Tare))) { - var tareStr = form[nameof(ITare.Tare)].ToString(); - if (!string.IsNullOrWhiteSpace(tareStr) && - double.TryParse(tareStr, NumberStyles.Float, CultureInfo.InvariantCulture, out var tare)) - { - await genericAttributeService.SaveAttributeAsync(product, nameof(ITare.Tare), tare, store.Id); - } + var tare = CommonHelper.To(form[nameof(ITare.Tare)].ToString()); + if (tare < 0) throw new Exception($"FruitBankEventConsumer->SaveProductCustomAttributesAsync(); (tare < 0); productId: {product.Id}; tare: {tare}"); + + await fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync(product.Id, nameof(ITare.Tare), tare); + } + + if (form.ContainsKey("IncomingQuantity")) + { + var incomingQuantity = CommonHelper.To(form["IncomingQuantity"].ToString()); + if (incomingQuantity < 0) throw new Exception($"FruitBankEventConsumer->SaveProductCustomAttributesAsync(); (incomingQuantity < 0); productId: {product.Id}; incomingQuantity: {incomingQuantity}"); + + await fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync(product.Id, "IncomingQuantity", incomingQuantity); } } diff --git a/Nop.Plugin.Misc.AIPlugin/Factories/CustomOrderModelFactory.cs b/Nop.Plugin.Misc.AIPlugin/Factories/CustomOrderModelFactory.cs index 2631089..8a11a28 100644 --- a/Nop.Plugin.Misc.AIPlugin/Factories/CustomOrderModelFactory.cs +++ b/Nop.Plugin.Misc.AIPlugin/Factories/CustomOrderModelFactory.cs @@ -176,7 +176,8 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Factories var orderListModelExtended = new OrderListModelExtended(); var extendedRows = new List(orderListModel.RecordsFiltered); - PropertyHelper.CopyPublicProperties(orderListModel, orderListModelExtended); + //TODO: Megnézni miért száll el az IEnumerable!!! - J. + //PropertyHelper.CopyPublicProperties(orderListModel, orderListModelExtended); foreach (var orderModel in orderListModel.Data) { diff --git a/Nop.Plugin.Misc.AIPlugin/Services/FruitBankAttributeService.cs b/Nop.Plugin.Misc.AIPlugin/Services/FruitBankAttributeService.cs index aed08e1..ea6f623 100644 --- a/Nop.Plugin.Misc.AIPlugin/Services/FruitBankAttributeService.cs +++ b/Nop.Plugin.Misc.AIPlugin/Services/FruitBankAttributeService.cs @@ -19,6 +19,15 @@ public class FruitBankAttributeService(IGenericAttributeService genericAttribute return (await genericAttributeService.GetAttributesForEntityAsync(entityId, typeof(TEntity).Name)).SingleOrDefault(ga => ga.StoreId == storeId && ga.Key == key); } + public Task GetGenericAttributeValueAsync(int entityId, string key) + => GetGenericAttributeValueAsync(entityId, key, storeContext.GetCurrentStore().Id); + + public async Task GetGenericAttributeValueAsync(int entityId, string key, int storeId) + { + var ga = await GetGenericAttributeAsync(entityId, key, storeId); + return ga == null ? default : CommonHelper.To(ga.Value); + } + public async Task?> GetMeasuringAttributesAsync(int entityId, int storeId) { var measuringAttributes = (await genericAttributeService.GetAttributesForEntityAsync(entityId, typeof(TEntity).Name))