Compare commits

..

No commits in common. "63abc01006143abd471ca9f05465485ec9791adb" and "865c13d62a2e330dbc331792c4dd24fdcaf53baf" have entirely different histories.

3 changed files with 22 additions and 53 deletions

View File

@ -1,6 +1,5 @@
using AyCode.Core.Loggers; using AyCode.Core.Loggers;
using AyCode.Services.SignalRs; using AyCode.Services.SignalRs;
using FruitBank.Common.Dtos;
using FruitBank.Common.Entities; using FruitBank.Common.Entities;
using FruitBank.Common.Interfaces; using FruitBank.Common.Interfaces;
using FruitBank.Common.Loggers; using FruitBank.Common.Loggers;
@ -199,6 +198,7 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Controllers
public async Task<List<CustomerRole>> GetCustomerRolesByCustomerId(int customerId) public async Task<List<CustomerRole>> GetCustomerRolesByCustomerId(int customerId)
{ {
_logger.Detail($"GetCustomerRolesByCustomerId invoked; customerId: {customerId}"); _logger.Detail($"GetCustomerRolesByCustomerId invoked; customerId: {customerId}");
return await ctx.GetCustomerRolesByCustomerId(customerId).ToListAsync(); return await ctx.GetCustomerRolesByCustomerId(customerId).ToListAsync();
} }
@ -206,21 +206,8 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Controllers
public async Task<List<ProductDto>> GetProductDtos() public async Task<List<ProductDto>> GetProductDtos()
{ {
_logger.Detail($"GetProductDtos invoked"); _logger.Detail($"GetProductDtos invoked");
return await ctx.GetAllProductDtos(false).ToListAsync();
}
[SignalR(SignalRTags.GetAllMeasuringProductDtos)] return await ctx.GetAllProducts().Select(c => new ProductDto(c)).ToListAsync();
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)] [SignalR(SignalRTags.AuthenticateUser)]

View File

@ -20,8 +20,6 @@ using Nop.Plugin.Misc.FruitBankPlugin.Services;
using Nop.Services.Catalog; using Nop.Services.Catalog;
using Nop.Services.Common; using Nop.Services.Common;
using System.Transactions; using System.Transactions;
using FruitBank.Common.Dtos;
using Mango.Nop.Core.Dtos;
namespace Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer; namespace Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer;
@ -104,20 +102,8 @@ public class FruitBankDbContext : MgDbContextBase, IPartnerDbSet<PartnerDbTable>
return query.Distinct(); return query.Distinct();
} }
public IQueryable<Product> GetAllProducts(bool includeDeleted) public IQueryable<Product> GetAllProducts()
=> Products.Table.Where(p => includeDeleted || !p.Deleted).OrderBy(o => o.Name); => Products.Table.Where(p => !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) public Task<bool> UpdateMeasuredShippingItemAsync(ShippingItem shippingItem)
{ {
@ -192,13 +178,12 @@ public class FruitBankDbContext : MgDbContextBase, IPartnerDbSet<PartnerDbTable>
{ {
var measuringValues = new MeasuringAttributeValues var measuringValues = new MeasuringAttributeValues
{ {
Id = product.Id,
NetWeight = productIdUnchanged ? shippingItem.MeasuredNetWeight - dbShippingItem.MeasuredNetWeight.GetValueOrDefault(0) : shippingItem.MeasuredNetWeight, NetWeight = productIdUnchanged ? shippingItem.MeasuredNetWeight - dbShippingItem.MeasuredNetWeight.GetValueOrDefault(0) : shippingItem.MeasuredNetWeight,
GrossWeight = productIdUnchanged ? shippingItem.MeasuredGrossWeight - dbShippingItem.MeasuredGrossWeight.GetValueOrDefault(0) : shippingItem.MeasuredGrossWeight, GrossWeight = productIdUnchanged ? shippingItem.MeasuredGrossWeight - dbShippingItem.MeasuredGrossWeight.GetValueOrDefault(0) : shippingItem.MeasuredGrossWeight,
IsMeasurable = shippingItem.IsMeasurable IsMeasurable = shippingItem.IsMeasurable
}; };
await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync<Product>(measuringValues, true); await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync<Product>(product.Id, measuringValues, true);
} }
} }
@ -216,8 +201,8 @@ public class FruitBankDbContext : MgDbContextBase, IPartnerDbSet<PartnerDbTable>
if (!productIsMeasurable) return true; if (!productIsMeasurable) return true;
var measuringValues = new MeasuringAttributeValues(product.Id, -dbShippingItem.MeasuredNetWeight.GetValueOrDefault(0), -dbShippingItem.MeasuredGrossWeight.GetValueOrDefault(0), dbShippingItem.IsMeasurable); var measuringValues = new MeasuringAttributeValues(-dbShippingItem.MeasuredNetWeight.GetValueOrDefault(0), -dbShippingItem.MeasuredGrossWeight.GetValueOrDefault(0), dbShippingItem.IsMeasurable);
await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync<Product>(measuringValues, true); await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync<Product>(product.Id, measuringValues, true);
} }
else Logger.Warning($"product == null; dbShippingItem.ProductId: {dbShippingItem.ProductId}"); else Logger.Warning($"product == null; dbShippingItem.ProductId: {dbShippingItem.ProductId}");

View File

@ -1,6 +1,5 @@
#nullable enable #nullable enable
using FruitBank.Common.Interfaces; using FruitBank.Common.Interfaces;
using FruitBank.Common.Models;
using Nop.Core; using Nop.Core;
using Nop.Core.Domain.Catalog; using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Common; using Nop.Core.Domain.Common;
@ -19,7 +18,7 @@ public class FruitBankAttributeService(IGenericAttributeService genericAttribute
{ {
var measuringAttributes = (await genericAttributeService.GetAttributesForEntityAsync(entityId, typeof(TEntity).Name)) var measuringAttributes = (await genericAttributeService.GetAttributesForEntityAsync(entityId, typeof(TEntity).Name))
.Where(ga => ga.StoreId == storeId && .Where(ga => ga.StoreId == storeId &&
(ga.Key is nameof(IMeasuringAttributeValues.NetWeight) or nameof(IMeasuringAttributeValues.GrossWeight) or nameof(IMeasuringAttributeValues.IsMeasurable))) (ga.Key is nameof(MeasuringAttributeValues.NetWeight) or nameof(MeasuringAttributeValues.GrossWeight) or nameof(MeasuringAttributeValues.IsMeasurable)))
.ToList(); .ToList();
return measuringAttributes; return measuringAttributes;
@ -35,10 +34,9 @@ public class FruitBankAttributeService(IGenericAttributeService genericAttribute
if (measuringAttributes.Count == 0) return null; if (measuringAttributes.Count == 0) return null;
var measuringAttributeValues = new MeasuringAttributeValues( var measuringAttributeValues = new MeasuringAttributeValues(
entityId, CommonHelper.To<double?>(measuringAttributes.FirstOrDefault(ga => ga.Key == nameof(MeasuringAttributeValues.NetWeight))),
CommonHelper.To<double?>(measuringAttributes.FirstOrDefault(ga => ga.Key == nameof(IMeasuringAttributeValues.NetWeight))?.Value), CommonHelper.To<double?>(measuringAttributes.FirstOrDefault(ga => ga.Key == nameof(MeasuringAttributeValues.GrossWeight))),
CommonHelper.To<double?>(measuringAttributes.FirstOrDefault(ga => ga.Key == nameof(IMeasuringAttributeValues.GrossWeight))?.Value), CommonHelper.To<bool?>(measuringAttributes.FirstOrDefault(ga => ga.Key == nameof(MeasuringAttributeValues.IsMeasurable))));
CommonHelper.To<bool?>(measuringAttributes.FirstOrDefault(ga => ga.Key == nameof(IMeasuringAttributeValues.IsMeasurable))?.Value));
return measuringAttributeValues; return measuringAttributeValues;
} }
@ -46,31 +44,30 @@ public class FruitBankAttributeService(IGenericAttributeService genericAttribute
public Task<bool> IsMeasurableEntityAsync<TEntity>(int entityId) => IsMeasurableEntityAsync<TEntity>(entityId, storeContext.GetCurrentStore().Id); public Task<bool> IsMeasurableEntityAsync<TEntity>(int entityId) => IsMeasurableEntityAsync<TEntity>(entityId, storeContext.GetCurrentStore().Id);
public async Task<bool> IsMeasurableEntityAsync<TEntity>(int entityId, int storeId) public async Task<bool> IsMeasurableEntityAsync<TEntity>(int entityId, int storeId)
{ {
var measurableAttribute = await GetGenericAttributeAsync<TEntity>(entityId, nameof(IMeasuringAttributeValues.IsMeasurable), storeId); var measurableAttribute = await GetGenericAttributeAsync<TEntity>(entityId, nameof(MeasuringAttributeValues.IsMeasurable), storeId);
return measurableAttribute != null && CommonHelper.To<bool>(measurableAttribute.Value); return measurableAttribute != null && CommonHelper.To<bool>(measurableAttribute.Value);
} }
public Task InsertOrUpdateMeasuringAttributeValuesAsync<TEntity>(IMeasuringAttributeValues measuringAttributeValues, bool cumulativeWeightUpdate) public Task InsertOrUpdateMeasuringAttributeValuesAsync<TEntity>(int entityId, MeasuringAttributeValues measuringAttributeValues, bool cumulativeWeightUpdate)
=> InsertOrUpdateMeasuringAttributeValuesAsync<TEntity>(measuringAttributeValues, cumulativeWeightUpdate, storeContext.GetCurrentStore().Id); => InsertOrUpdateMeasuringAttributeValuesAsync<TEntity>(entityId, storeContext.GetCurrentStore().Id, measuringAttributeValues, cumulativeWeightUpdate);
public async Task InsertOrUpdateMeasuringAttributeValuesAsync<TEntity>(IMeasuringAttributeValues measuringAttributeValues, bool cumulativeWeightUpdate, int storeId) public async Task InsertOrUpdateMeasuringAttributeValuesAsync<TEntity>(int entityId, int storeId, MeasuringAttributeValues measuringAttributeValues, bool cumulativeWeightUpdate)
{ {
if (!measuringAttributeValues.HasValues()) throw new Exception($"FruitBankAttributeService->InsertOrUpdateMeasuringAttributeValuesAsync; measuringAttributeValues.HasValues() == false; keyGroup: {typeof(TEntity).Name} values: {measuringAttributeValues}"); if (!measuringAttributeValues.HasValues()) throw new Exception($"FruitBankAttributeService->InsertOrUpdateMeasuringAttributeValuesAsync; measuringAttributeValues.HasValues() == false; entityId: {entityId}; values: {measuringAttributeValues}");
var entityId = measuringAttributeValues.Id;
var measuringAttributes = await GetMeasuringAttributesAsync<TEntity>(entityId, storeId); var measuringAttributes = await GetMeasuringAttributesAsync<TEntity>(entityId, storeId);
var netWeightAttribute = measuringAttributes.FirstOrDefault(ma => ma.Key == nameof(IMeasuringAttributeValues.NetWeight)); var netWeightAttribute = measuringAttributes.FirstOrDefault(ma => ma.Key == nameof(MeasuringAttributeValues.NetWeight));
if (netWeightAttribute != null) await UpdateMeasuringWeightAttributeValueAsync(netWeightAttribute, measuringAttributeValues.NetWeight!.Value, cumulativeWeightUpdate); if (netWeightAttribute != null) await UpdateMeasuringWeightAttributeValueAsync(netWeightAttribute, measuringAttributeValues.NetWeight!.Value, cumulativeWeightUpdate);
else await InsertGenericAttributeAsync<TEntity, double>(entityId, nameof(IMeasuringAttributeValues.NetWeight), measuringAttributeValues.NetWeight!.Value, storeId); else await InsertGenericAttributeAsync<TEntity, double>(entityId, nameof(MeasuringAttributeValues.NetWeight), measuringAttributeValues.NetWeight!.Value, storeId);
var grossWeightAttribute = measuringAttributes.FirstOrDefault(ma => ma.Key == nameof(IMeasuringAttributeValues.GrossWeight)); var grossWeightAttribute = measuringAttributes.FirstOrDefault(ma => ma.Key == nameof(MeasuringAttributeValues.GrossWeight));
if (grossWeightAttribute != null) await UpdateMeasuringWeightAttributeValueAsync(grossWeightAttribute, measuringAttributeValues.GrossWeight!.Value, cumulativeWeightUpdate); if (grossWeightAttribute != null) await UpdateMeasuringWeightAttributeValueAsync(grossWeightAttribute, measuringAttributeValues.GrossWeight!.Value, cumulativeWeightUpdate);
else await InsertGenericAttributeAsync<TEntity, double>(entityId, nameof(IMeasuringAttributeValues.GrossWeight), measuringAttributeValues.GrossWeight!.Value, storeId); else await InsertGenericAttributeAsync<TEntity, double>(entityId, nameof(MeasuringAttributeValues.GrossWeight), measuringAttributeValues.GrossWeight!.Value, storeId);
var isMeasurableAttribute = measuringAttributes.FirstOrDefault(ma => ma.Key == nameof(IMeasuringAttributeValues.IsMeasurable)); var isMeasurableAttribute = measuringAttributes.FirstOrDefault(ma => ma.Key == nameof(MeasuringAttributeValues.IsMeasurable));
if (isMeasurableAttribute != null) await UpdateGenericAttributeAsync(isMeasurableAttribute, measuringAttributeValues.IsMeasurable!.Value); if (isMeasurableAttribute != null) await UpdateGenericAttributeAsync(isMeasurableAttribute, measuringAttributeValues.IsMeasurable!.Value);
else await InsertGenericAttributeAsync<TEntity, bool>(entityId, nameof(IMeasuringAttributeValues.IsMeasurable), measuringAttributeValues.IsMeasurable!.Value, storeId); else await InsertGenericAttributeAsync<TEntity, bool>(entityId, nameof(MeasuringAttributeValues.IsMeasurable), measuringAttributeValues.IsMeasurable!.Value, storeId);
} }
private async Task UpdateMeasuringWeightAttributeValueAsync(GenericAttribute genericAttribute, double newWeightValue, bool cumulativeWeightUpdate) private async Task UpdateMeasuringWeightAttributeValueAsync(GenericAttribute genericAttribute, double newWeightValue, bool cumulativeWeightUpdate)