improvements, fixes, etc...

This commit is contained in:
Loretta 2025-09-30 18:18:22 +02:00
parent 53a336cb3e
commit 5a0e0e9b2f
3 changed files with 53 additions and 22 deletions

View File

@ -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<List<CustomerRole>> 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<List<ProductDto>> 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<List<MeasuringProductDto>> GetAllMeasuringProductDtos()
{
_logger.Detail($"GetAllMeasuringProductDtos invoked");
return await ctx.GetAllMeasuringProductDtos(false).ToListAsync();
}
[SignalR(SignalRTags.GetMeasuringProductDtoById)]
public async Task<MeasuringProductDto?> GetMeasuringProductDtoById(int productId)
{
_logger.Detail($"GetMeasuringProductDtoById invoked; productId: {productId}");
return await ctx.GetMeasuringProductDtoByIdAsync(productId);
}
[SignalR(SignalRTags.AuthenticateUser)]

View File

@ -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<PartnerDbTable>
return query.Distinct();
}
public IQueryable<Product> GetAllProducts()
=> Products.Table.Where(p => !p.Deleted).OrderBy(o => o.Name);
public IQueryable<Product> GetAllProducts(bool includeDeleted)
=> Products.Table.Where(p => includeDeleted || !p.Deleted).OrderBy(o => o.Name);
public IQueryable<ProductDto> GetAllProductDtos(bool includeDeleted)
=> GetAllProducts(includeDeleted).Select(product => new ProductDto(product));
public IAsyncEnumerable<MeasuringProductDto> GetAllMeasuringProductDtos(bool includeDeleted)
=> GetAllProducts(includeDeleted).SelectAwait(async product => new MeasuringProductDto(product, await GetMeasuringAttributeValuesByProductIdAsync(product.Id)));
public async Task<MeasuringProductDto?> 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<MeasuringAttributeValues?> GetMeasuringAttributeValuesByProductIdAsync(int productId)
=> await _fruitBankAttributeService.GetMeasuringAttributeValuesAsync<Product>(productId);
public Task<bool> UpdateMeasuredShippingItemAsync(ShippingItem shippingItem)
{
@ -178,12 +192,13 @@ public class FruitBankDbContext : MgDbContextBase, IPartnerDbSet<PartnerDbTable>
{
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>(product.Id, measuringValues, true);
await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync<Product>(measuringValues, true);
}
}
@ -201,8 +216,8 @@ public class FruitBankDbContext : MgDbContextBase, IPartnerDbSet<PartnerDbTable>
if (!productIsMeasurable) return true;
var measuringValues = new MeasuringAttributeValues(-dbShippingItem.MeasuredNetWeight.GetValueOrDefault(0), -dbShippingItem.MeasuredGrossWeight.GetValueOrDefault(0), dbShippingItem.IsMeasurable);
await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync<Product>(product.Id, measuringValues, true);
var measuringValues = new MeasuringAttributeValues(product.Id, -dbShippingItem.MeasuredNetWeight.GetValueOrDefault(0), -dbShippingItem.MeasuredGrossWeight.GetValueOrDefault(0), dbShippingItem.IsMeasurable);
await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync<Product>(measuringValues, true);
}
else Logger.Warning($"product == null; dbShippingItem.ProductId: {dbShippingItem.ProductId}");

View File

@ -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<double?>(measuringAttributes.FirstOrDefault(ga => ga.Key == nameof(MeasuringAttributeValues.NetWeight))),
CommonHelper.To<double?>(measuringAttributes.FirstOrDefault(ga => ga.Key == nameof(MeasuringAttributeValues.GrossWeight))),
CommonHelper.To<bool?>(measuringAttributes.FirstOrDefault(ga => ga.Key == nameof(MeasuringAttributeValues.IsMeasurable))));
entityId,
CommonHelper.To<double?>(measuringAttributes.FirstOrDefault(ga => ga.Key == nameof(IMeasuringAttributeValues.NetWeight))?.Value),
CommonHelper.To<double?>(measuringAttributes.FirstOrDefault(ga => ga.Key == nameof(IMeasuringAttributeValues.GrossWeight))?.Value),
CommonHelper.To<bool?>(measuringAttributes.FirstOrDefault(ga => ga.Key == nameof(IMeasuringAttributeValues.IsMeasurable))?.Value));
return measuringAttributeValues;
}
@ -44,30 +46,31 @@ public class FruitBankAttributeService(IGenericAttributeService genericAttribute
public Task<bool> IsMeasurableEntityAsync<TEntity>(int entityId) => IsMeasurableEntityAsync<TEntity>(entityId, storeContext.GetCurrentStore().Id);
public async Task<bool> IsMeasurableEntityAsync<TEntity>(int entityId, int storeId)
{
var measurableAttribute = await GetGenericAttributeAsync<TEntity>(entityId, nameof(MeasuringAttributeValues.IsMeasurable), storeId);
var measurableAttribute = await GetGenericAttributeAsync<TEntity>(entityId, nameof(IMeasuringAttributeValues.IsMeasurable), storeId);
return measurableAttribute != null && CommonHelper.To<bool>(measurableAttribute.Value);
}
public Task InsertOrUpdateMeasuringAttributeValuesAsync<TEntity>(int entityId, MeasuringAttributeValues measuringAttributeValues, bool cumulativeWeightUpdate)
=> InsertOrUpdateMeasuringAttributeValuesAsync<TEntity>(entityId, storeContext.GetCurrentStore().Id, measuringAttributeValues, cumulativeWeightUpdate);
public Task InsertOrUpdateMeasuringAttributeValuesAsync<TEntity>(IMeasuringAttributeValues measuringAttributeValues, bool cumulativeWeightUpdate)
=> InsertOrUpdateMeasuringAttributeValuesAsync<TEntity>(measuringAttributeValues, cumulativeWeightUpdate, storeContext.GetCurrentStore().Id);
public async Task InsertOrUpdateMeasuringAttributeValuesAsync<TEntity>(int entityId, int storeId, MeasuringAttributeValues measuringAttributeValues, bool cumulativeWeightUpdate)
public async Task InsertOrUpdateMeasuringAttributeValuesAsync<TEntity>(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<TEntity>(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<TEntity, double>(entityId, nameof(MeasuringAttributeValues.NetWeight), measuringAttributeValues.NetWeight!.Value, storeId);
else await InsertGenericAttributeAsync<TEntity, double>(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<TEntity, double>(entityId, nameof(MeasuringAttributeValues.GrossWeight), measuringAttributeValues.GrossWeight!.Value, storeId);
else await InsertGenericAttributeAsync<TEntity, double>(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<TEntity, bool>(entityId, nameof(MeasuringAttributeValues.IsMeasurable), measuringAttributeValues.IsMeasurable!.Value, storeId);
else await InsertGenericAttributeAsync<TEntity, bool>(entityId, nameof(IMeasuringAttributeValues.IsMeasurable), measuringAttributeValues.IsMeasurable!.Value, storeId);
}
private async Task UpdateMeasuringWeightAttributeValueAsync(GenericAttribute genericAttribute, double newWeightValue, bool cumulativeWeightUpdate)