improvements, fixes, etc...

This commit is contained in:
Loretta 2025-09-11 12:51:58 +02:00
parent 3abfc03d1e
commit 568deb0131
7 changed files with 118 additions and 110 deletions

View File

@ -1,21 +1,32 @@
using AyCode.Core.Loggers;
using AyCode.Services.SignalRs;
using DocumentFormat.OpenXml.Office2010.Excel;
using FruitBank.Common.Entities;
using FruitBank.Common.Interfaces;
using FruitBank.Common.Loggers;
using FruitBank.Common.Models;
using FruitBank.Common.Server;
using FruitBank.Common.SignalRs;
using Microsoft.AspNetCore.Mvc;
using Mango.Nop.Core.Dtos;
using Mango.Nop.Core.Models;
using Nop.Core;
using Nop.Core.Domain.Customers;
using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer;
using Nop.Services.Customers;
using Nop.Services.Localization;
using Nop.Web.Framework.Controllers;
using NUglify.Helpers;
namespace Nop.Plugin.Misc.FruitBankPlugin.Controllers
{
//https://linq2db.github.io/articles/sql/Join-Operators.html
public class FruitBankDataController(FruitBankDbContext ctx, IWorkContext workContext, ICustomerService customerService, IEnumerable<IAcLogWriterBase> logWriters): BasePluginController, IFruitBankDataControllerServer
public class FruitBankDataController(
FruitBankDbContext ctx,
IWorkContext workContext,
ICustomerService customerService,
ICustomerRegistrationService customerRegistrationService,
ILocalizationService localizationService,
IEnumerable<IAcLogWriterBase> logWriters)
: BasePluginController, IFruitBankDataControllerServer
{
private readonly ILogger _logger = new Logger<FruitBankDataController>(logWriters.ToArray());
@ -45,9 +56,9 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Controllers
{
_logger.Detail($"GetPartnerById invoked; id: {id}");
var customers = await ctx.GetCustormersBySystemRoleName("Measuring").ToListAsync();
//var customers = await ctx.GetCustormersBySystemRoleName("Measuring").ToListAsync();
//_logger.Error($"COUNT: {customers.Count}");
_logger.Error($"COUNT: {customers.Count}");
return await ctx.Partners.GetByIdAsync(id);
}
@ -110,5 +121,87 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Controllers
return await ctx.ShippingDocuments.GetByIdAsync(id);
}
[SignalR(SignalRTags.GetMeasuringUsers)]
public async Task<List<CustomerDto>> GetMeasuringUsers()
{
_logger.Detail($"GetMeasuringUsers invoked");
var customers = await ctx.GetCustormersBySystemRoleName(FruitBankConst.MeasuringRoleSystemName).OrderBy(o => o.Username).Select(c => new CustomerDto(c)).ToListAsync();
return customers; //.ToModelDto<CustomerDto>();
}
[SignalR(SignalRTags.GetProductDtos)]
public async Task<List<ProductDto>> GetProductDtos()
{
_logger.Detail($"GetProductDtos invoked");
return await ctx.GetProducts().OrderBy(o => o.Name).Select(c => new ProductDto(c)).ToListAsync();
}
[SignalR(SignalRTags.AuthenticateUser)]
public async Task<MgLoginModelResponse> LoginMeasuringUser(MgLoginModelRequest loginModelRequest)
{
var customerEmail = loginModelRequest?.Email;
var customerPassword = loginModelRequest?.Password;
_logger.Detail($"LoginMeasuringUser invoked; customerEmail; {customerEmail}");
var resultLoginModel = new MgLoginModelResponse();
if (!customerEmail.IsNullOrWhiteSpace() && !customerPassword.IsNullOrWhiteSpace())
{
var loginResult = await customerRegistrationService.ValidateCustomerAsync(customerEmail, customerPassword);
switch (loginResult)
{
case CustomerLoginResults.Successful:
{
var customer = await customerService.GetCustomerByEmailAsync(customerEmail);
var isInMeasuringRole = await customerService.IsInCustomerRoleAsync(customer, FruitBankConst.MeasuringRoleSystemName);
if (!isInMeasuringRole)
{
resultLoginModel.ErrorMessage = "Is not in MeauringRole!";
break;
}
//var actionResult = await customerRegistrationService.SignInCustomerAsync(customer, returnUrl, loginModel.RememberMe);
//await _workContext.SetCurrentCustomerAsync(customer);
//await _authenticationService.SignInAsync(customer, isPersist);
////raise event
//await _eventPublisher.PublishAsync(new CustomerLoggedinEvent(customer));
resultLoginModel.CustomerDto = new CustomerDto(customer); //customer.ToModel<CustomerDto>();
break;
}
case CustomerLoginResults.CustomerNotExist:
resultLoginModel.ErrorMessage = await localizationService.GetResourceAsync("Account.Login.WrongCredentials.CustomerNotExist");
break;
case CustomerLoginResults.Deleted:
resultLoginModel.ErrorMessage = await localizationService.GetResourceAsync("Account.Login.WrongCredentials.Deleted");
break;
case CustomerLoginResults.NotActive:
resultLoginModel.ErrorMessage = await localizationService.GetResourceAsync("Account.Login.WrongCredentials.NotActive");
break;
case CustomerLoginResults.NotRegistered:
resultLoginModel.ErrorMessage = await localizationService.GetResourceAsync("Account.Login.WrongCredentials.NotRegistered");
break;
case CustomerLoginResults.LockedOut:
resultLoginModel.ErrorMessage = await localizationService.GetResourceAsync("Account.Login.WrongCredentials.LockedOut");
break;
case CustomerLoginResults.WrongPassword:
default:
resultLoginModel.ErrorMessage = await localizationService.GetResourceAsync("Account.Login.WrongCredentials");
break;
}
}
else resultLoginModel.ErrorMessage = await localizationService.GetResourceAsync("Account.Login.WrongCredentials");
if (!resultLoginModel.ErrorMessage.IsNullOrWhiteSpace()) _logger.Error($"{resultLoginModel.ErrorMessage}; email: {customerEmail}");
return resultLoginModel;
}
}
}

View File

@ -4,6 +4,7 @@ using LinqToDB;
using Mango.Nop.Core.Repositories;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Nop.Core.Caching;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Customers;
using Nop.Data;
using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer.Interfaces;
@ -18,20 +19,20 @@ public class FruitBankDbContext : MgDbContextBase, IPartnerDbSet<PartnerDbTable>
private readonly IProductService _productService;
private readonly IStaticCacheManager _staticCacheManager;
protected readonly IRepository<Customer> _customerRepository;
protected readonly IRepository<CustomerCustomerRoleMapping> _customerCustomerRoleMappingRepository;
protected readonly IRepository<CustomerRole> _customerRoleRepository;
public PartnerDbTable Partners { get; set; }
public ShippingDbTable Shippings { get; set; }
public ShippingItemDbTable ShippingItems { get; set; }
public ShippingDocumentDbTable ShippingDocuments { get; set; }
//public EntityRepository<Auction> Auctions2 { get; set; }
//public IRepository<AuctionBid> AuctionBids2 { get; set; }
public IRepository<Product> Products { get; set; }
public IRepository<Customer> Customers { get; set; }
public IRepository<CustomerRole> CustomerRoles { get; set; }
public IRepository<CustomerCustomerRoleMapping> CustomerRoleMappings { get; set; }
public FruitBankDbContext(INopDataProvider dataProvider, PartnerDbTable partnerDbTable, ShippingDbTable shippingDbTable, ShippingItemDbTable shippingItemDbTable,
ShippingDocumentDbTable shippingDocumentDbTable, IProductService productService, IStaticCacheManager staticCacheManager,
IRepository<Product> productRepository,
IRepository<Customer> customerRepository,
IRepository<CustomerCustomerRoleMapping> customerCustomerRoleMappingRepository,
IRepository<CustomerRole> customerRoleRepository,
@ -45,13 +46,10 @@ public class FruitBankDbContext : MgDbContextBase, IPartnerDbSet<PartnerDbTable>
ShippingItems = shippingItemDbTable;
ShippingDocuments = shippingDocumentDbTable;
_customerRepository = customerRepository;
_customerCustomerRoleMappingRepository = customerCustomerRoleMappingRepository;
_customerRoleRepository = customerRoleRepository;
//Auctions.Table
//var auctions = DataProvider.GetTable<Auction>().Where(x => x.Closed);
Products = productRepository;
Customers = customerRepository;
CustomerRoles = customerRoleRepository;
CustomerRoleMappings = customerCustomerRoleMappingRepository;
}
public IQueryable<MeasuringModel> GetMeasuringModelByShippingId(int shippingId)
@ -70,92 +68,15 @@ public class FruitBankDbContext : MgDbContextBase, IPartnerDbSet<PartnerDbTable>
if (systemRoleName.IsNullOrWhiteSpace()) throw new ArgumentException("systemRoleName.IsNullOrWhiteSpace()", nameof(systemRoleName));
var query =
from c in _customerRepository.Table
join crm in _customerCustomerRoleMappingRepository.Table on c.Id equals crm.CustomerId
join cr in _customerRoleRepository.Table on crm.CustomerRoleId equals cr.Id
from c in Customers.Table
join crm in CustomerRoleMappings.Table on c.Id equals crm.CustomerId
join cr in CustomerRoles.Table on crm.CustomerRoleId equals cr.Id
where c.Active && !c.Deleted && cr.SystemName == systemRoleName
select c;
return query.Distinct();
// query = query.Join(_customerCustomerRoleMappingRepository.Table, x => x.Id, y => y.CustomerId,
// (x, y) => new { Customer = x, Mapping = y })
// .Where(z => z.Mapping.CustomerRoleId == customerRoleId)
// .Select(z => z.Customer)
// .Distinct();
//});
//return customers;
}
////public AuctionDbContext(IRepository<Auction> _auctionRepository, IRepository<AuctionBid> _auctionBidRepository)
////{
//// Auctions2 = _auctionRepository as EntityRepository<Auction>;
//// AuctionBids2 = _auctionBidRepository;
////}
//public Task<List<AuctionBid>> GetAllLastBidByAuctionIdAsync(int auctionId) => AuctionBids.GetAllLastBidByAuctionId(auctionId).ToListAsync();
//public Task<Dictionary<int, AuctionBid>> GetAllLastBidDictionaryByAuctionIdAsync(int auctionId)
// => AuctionBids.GetAllLastBidByAuctionId(auctionId).ToDictionaryAsync(x => x.ProductAuctionMappingId);
//public async Task<List<ProductToAuctionMapping>> GetProductToAuctionsByProductIdAsync(int productId)
//{
// return [..await ProductToAuctions.GetByProductId(productId).ToListAsync()];
//}
//public async Task<List<ProductToAuctionMapping>> GetProductToAuctionByAuctionIdAndProductIdAsync(int auctionId, int productId, bool activeProductOnly)
//{
// return [..await ProductToAuctions.GetByAuctionAndProductId(auctionId, productId, activeProductOnly).ToListAsync()];
//}
//public Task<bool> HasBidByProductToAuctionIdAsync(int productToAuctionId)
// => AuctionBids.HasBidByProductToAuctionIdAsync(productToAuctionId);
//public Task<AuctionBid> GetLastAuctionBidByProductToAuctionId(int productToAuctionId)
// => AuctionBids.GetLastAuctionBidByProductToAuctionId(productToAuctionId).FirstOrDefaultAsync();
//public Task<int> GetBidsCountByProductToAuctionIdAsync(int productToAuctionId)
// => AuctionBids.GetBidsCountByProductToAuctionIdAsync(productToAuctionId);
//public Task<AuctionBid> RevertAuctionBidByProductToAuctionId(int productToAuctionId)
// => AuctionBids.RevertByProductToAuctionIdAsync(productToAuctionId);
////public async Task ResetProductToAuctionByProductId(int productId)
//// => await ResetProductToAuctionAsync(await ProductToAuctions.GetByProductId(productId).FirstOrDefaultAsync());
//public async Task ResetProductToAuctionByIdAsync(int productToAuctionId, decimal basePrice = 0)
// => await ResetProductToAuctionAsync(ProductToAuctions.GetById(productToAuctionId), basePrice);
//public async Task ResetProductToAuctionAsync(ProductToAuctionMapping productToAuction, decimal basePrice = 0)
//{
// if (productToAuction == null)
// {
// Logger.Error($"AuctionDbContext.ResetProductToAuctionAsync(); productToAuction == null");
// return;
// }
// await Logger.InformationAsync($"AuctionDbContext.ResetProductToAuctionAsync(); productToAuction.Id: {productToAuction.Id}; basePrice: {basePrice}");
// var product = await _productService.GetProductByIdAsync(productToAuction.ProductId);
// if (product == null)
// {
// Logger.Error($"AuctionDbContext.ResetProductToAuctionAsync(); product == null");
// return;
// }
// if (basePrice <= 0) basePrice = product.Price;
// else
// {
// product.Price = basePrice;
// await _productService.UpdateProductAsync(product);
// }
// await ProductToAuctions.ResetAsync(productToAuction, basePrice);
// await AuctionBids.DeleteAllByProductToAuctionIdAsync(productToAuction.Id);
// await _staticCacheManager.ClearAsync();
// //await _staticCacheManager.RemoveByPrefixAsync(AUCTION_PATTERN_KEY);
//}
public IQueryable<Product> GetProducts()
=> Products.Table.Where(p => !p.Deleted);
}

View File

@ -15,7 +15,6 @@ public class PartnerDbTable : MgDbTableBase<Partner>
{
}
public IQueryable<Partner> GetAll() => Table;
//public IOrderedQueryable<AuctionBid> GetAllLastBidByAuctionId(int auctionId)
//{

View File

@ -14,6 +14,4 @@ public class ShippingDbTable : MgDbTableBase<Shipping>
: base(eventPublisher, dataProvider, shortTermCacheManager, staticCacheManager, appSettings, logger)
{
}
public IQueryable<Shipping> GetAll() => Table;
}

View File

@ -14,6 +14,4 @@ public class ShippingDocumentDbTable : MgDbTableBase<ShippingDocument>
: base(eventPublisher, dataProvider, shortTermCacheManager, staticCacheManager, appSettings, logger)
{
}
public IQueryable<ShippingDocument> GetAll() => Table;
}

View File

@ -14,6 +14,4 @@ public class ShippingItemDbTable : MgDbTableBase<ShippingItem>
: base(eventPublisher, dataProvider, shortTermCacheManager, staticCacheManager, appSettings, logger)
{
}
public IQueryable<ShippingItem> GetAll() => Table;
}

View File

@ -1,4 +1,5 @@
using FruitBank.Common.Server;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Routing;
@ -50,7 +51,7 @@ namespace Nop.Plugin.Misc.FruitBankPlugin
// --- INSTALL ---
public override async Task InstallAsync()
{
//TODO: Add "Measuring" role
//TODO: Add "Measuring" role - FruitBankConst.MeasuringRoleSystemName
//TODO: Add "NeedsToBeMeasured" product attribute if not exists
// Default settings