product event fixex; etc...

This commit is contained in:
Loretta 2025-10-11 17:51:30 +02:00
parent c9fa03a69c
commit ca1f59a5e1
4 changed files with 53 additions and 87 deletions

View File

@ -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<IViewComponentResult> 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<Product>(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<bool>(dbProduct, IS_MEASURABLE_KEY, storeScope.Id);
//var dbNetWeight = await _genericAttributeService.GetAttributeAsync<decimal?>(dbProduct, NET_WEIGHT_KEY, storeScope.Id);
//var dbIncomingQuantity = await _genericAttributeService.GetAttributeAsync<decimal?>(dbProduct, "IncomingQuantity", storeScope.Id);
model.IsMeasurable = await _genericAttributeService
.GetAttributeAsync<bool>(dbProduct, IS_MEASURABLE_KEY, storeScope.Id);
model.NetWeight = await _genericAttributeService
.GetAttributeAsync<double>(dbProduct, NET_WEIGHT_KEY, storeScope.Id);
model.IncomingQuantity = await _genericAttributeService
.GetAttributeAsync<int?>(dbProduct, "IncomingQuantity", storeScope.Id);
model.Tare = await _genericAttributeService
.GetAttributeAsync<double>(dbProduct, TARE_KEY, storeScope.Id);
model.Tare = await _fruitBankAttributeService.GetGenericAttributeValueAsync<Product, double>(model.ProductId, nameof(ITare.Tare));
model.IncomingQuantity = await _fruitBankAttributeService.GetGenericAttributeValueAsync<Product, int>(model.ProductId, "IncomingQuantity");
}
return View("~/Plugins/Misc.FruitBankPlugin/Views/ProductAttributes.cshtml", model);

View File

@ -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<IAcLogWriterBase> logWriters, IGenericAttributeService genericAttributeService) :
MgEventConsumer(httpContextAccessor, logWriters),
public class FruitBankEventConsumer(IHttpContextAccessor httpContextAcc, FruitBankDbContext ctx, FruitBankAttributeService fruitBankAttributeService, IEnumerable<IAcLogWriterBase> logWriters) :
MgEventConsumer(httpContextAcc, logWriters),
IConsumer<EntityDeletedEvent<Shipping>>,
IConsumer<EntityInsertedEvent<ShippingItem>>,
IConsumer<EntityUpdatedEvent<ShippingItem>>,
@ -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>(product.Id);
@ -51,61 +51,42 @@ public class FruitBankEventConsumer(IHttpContextAccessor httpContextAccessor, Fr
await base.HandleEventAsync(eventMessage);
}
public async Task HandleEventAsync(EntityInsertedEvent<Product> eventMessage)
public override async Task HandleEventAsync(EntityInsertedEvent<Product> 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>(product.Id, 0, 0, isMeasurable, false);
}
var isMeasurable = form[nameof(IMeasurable.IsMeasurable)].ToString().Contains("true");
//var isMeasurable = CommonHelper.To<bool>(form[nameof(IMeasurable.IsMeasurable)].ToString());
var netWeight = CommonHelper.To<double>(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>(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>(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<double>(form[nameof(ITare.Tare)].ToString());
if (tare < 0) throw new Exception($"FruitBankEventConsumer->SaveProductCustomAttributesAsync(); (tare < 0); productId: {product.Id}; tare: {tare}");
await fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync<Product, double>(product.Id, nameof(ITare.Tare), tare);
}
if (form.ContainsKey("IncomingQuantity"))
{
var incomingQuantity = CommonHelper.To<int>(form["IncomingQuantity"].ToString());
if (incomingQuantity < 0) throw new Exception($"FruitBankEventConsumer->SaveProductCustomAttributesAsync(); (incomingQuantity < 0); productId: {product.Id}; incomingQuantity: {incomingQuantity}");
await fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync<Product, int>(product.Id, "IncomingQuantity", incomingQuantity);
}
}

View File

@ -176,7 +176,8 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Factories
var orderListModelExtended = new OrderListModelExtended();
var extendedRows = new List<OrderModelExtended>(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)
{

View File

@ -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<TPropType?> GetGenericAttributeValueAsync<TEntity, TPropType>(int entityId, string key)
=> GetGenericAttributeValueAsync<TEntity, TPropType>(entityId, key, storeContext.GetCurrentStore().Id);
public async Task<TPropType?> GetGenericAttributeValueAsync<TEntity, TPropType>(int entityId, string key, int storeId)
{
var ga = await GetGenericAttributeAsync<TEntity>(entityId, key, storeId);
return ga == null ? default : CommonHelper.To<TPropType>(ga.Value);
}
public async Task<List<GenericAttribute>?> GetMeasuringAttributesAsync<TEntity>(int entityId, int storeId)
{
var measuringAttributes = (await genericAttributeService.GetAttributesForEntityAsync(entityId, typeof(TEntity).Name))