diff --git a/Nop.Plugin.Misc.AuctionPlugin/Domains/Entities/Auction.cs b/Nop.Plugin.Misc.AuctionPlugin/Domains/Entities/Auction.cs index bc83520..a94e8b7 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Domains/Entities/Auction.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Domains/Entities/Auction.cs @@ -20,6 +20,7 @@ public partial class Auction: MgEntityBase, IAuction public DateTime? EndDateUtc { get; set; } public bool Closed { get; set; } + public int? CategoryId { get; set; } [SkipValuesOnUpdate] public DateTime Created { get; set; } diff --git a/Nop.Plugin.Misc.AuctionPlugin/Domains/Entities/Interfaces/IAuction.cs b/Nop.Plugin.Misc.AuctionPlugin/Domains/Entities/Interfaces/IAuction.cs index 449f98b..905dd6c 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Domains/Entities/Interfaces/IAuction.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Domains/Entities/Interfaces/IAuction.cs @@ -6,5 +6,5 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Domains.Entities.Interfaces; public interface IAuction : IAuctionDtoBase, ITimeStampInfo //, ISoftRemoveEntityInt { - + public int? CategoryId { get; set; } } \ No newline at end of file diff --git a/Nop.Plugin.Misc.AuctionPlugin/Hubs/SignalRMessageHandler.cs b/Nop.Plugin.Misc.AuctionPlugin/Hubs/SignalRMessageHandler.cs index ea9066a..332ede6 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Hubs/SignalRMessageHandler.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Hubs/SignalRMessageHandler.cs @@ -26,7 +26,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs //- ha bid-elt 1x is, kerüljön a watch-ba //- DbTransaction-t vhogy megcsinánli! - public class SignalRMessageHandler(ILogger logger, IProductService productService, AuctionService auctionService, IHubContext hubContext, IWorkContext workContext, ICustomerService customerService) + public class SignalRMessageHandler(ILogger logger, IProductService productService, AuctionService auctionService, IHubContext hubContext, IWorkContext workContext, ICustomerService customerService, ICategoryService categoryService) { private readonly Mutex _handleMessageMutex = new(); @@ -132,13 +132,6 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs { await logger.InformationAsync($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); ProductToAuctionMappingId: {auctionProductStatusRequest.ProductToAuctionId}; Status: {auctionProductStatusRequest.AuctionStatus}({(int)auctionProductStatusRequest.AuctionStatus})", null, customer); - var auction = await auctionService.GetAuctionDtoByProductToAuctionIdAsync(auctionProductStatusRequest.ProductToAuctionId, false); - if (auction == null || auction.Closed) - { - logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); (auction == null || auction.Closed); Closed: {auction?.Closed}", null, customer); - return; - } - var productToAuction = await auctionService.GetProductToAuctionMappingByIdAsync(auctionProductStatusRequest.ProductToAuctionId); if (productToAuction == null) { @@ -146,13 +139,20 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs return; } + var auction = await auctionService.GetAuctionByIdAsync(productToAuction.AuctionId); + if (auction == null || auction.Closed) + { + logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); (auction == null || auction.Closed); Closed: {auction?.Closed}", null, customer); + return; + } + if (!IsValidRequestAuctionStatus(auctionProductStatusRequest.AuctionStatus, productToAuction.AuctionStatus)) { logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); RequestAuctionStatusIsValid() == false; newStatus: {auctionProductStatusRequest.AuctionStatus}; oldStatus: {productToAuction.AuctionStatus}", null, customer); return; } - if (auctionProductStatusRequest.AuctionStatus == AuctionStatus.None) await ResetProductToAuction(productToAuction); + if (auctionProductStatusRequest.AuctionStatus == AuctionStatus.None) await ResetProductToAuction(productToAuction, auction.CategoryId); else { switch (auctionProductStatusRequest.AuctionStatus) @@ -207,8 +207,11 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs SenderId = customer.Id, Data = new ProductToAuctionStatusNotification(await auctionService.GetAuctionDtoByProductToAuctionIdAsync(productToAuction.Id, true), bidsCount, "EMPTY").ToJson() }; - + await hubContext.Clients.All.SendAsync("send", productToauctionChangedNotification.ToJson()); + + var isFeaturedProduct = productToAuction.IsActiveItem || productToAuction.AuctionStatus == AuctionStatus.Pause; + await UpdateProductCategoryIsFeaturedAsync(isFeaturedProduct, auction.CategoryId, productToAuction.ProductId); } catch (Exception ex) { @@ -245,7 +248,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs var product = await auctionService.GetProductById(bidRequestMessage.ProductId); if (product == null) return; - if (IsValidBidPrice(product.Price, activeProductAuction.CurrentPrice, bidRequestMessage.BidPrice, customer)) return; + if (!IsValidBidPrice(product.Price, activeProductAuction.CurrentPrice, bidRequestMessage.BidPrice, customer)) return; var auctionBid = bidRequestMessage.CreateMainEntity(); auctionBid.ProductAuctionMappingId = activeProductAuction.Id; @@ -307,10 +310,34 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs await hubContext.Clients.All.SendAsync("send", bidMessage.ToJson()); } - private Task ResetProductToAuction(ProductToAuctionMapping productToAuction) + private async Task ResetProductToAuction(ProductToAuctionMapping productToAuction, int? categoryId = null) { productToAuction.AuctionStatus = AuctionStatus.None; - return auctionService.ResetProductToAuctionAsync(productToAuction, productToAuction.StartingPrice); + await auctionService.ResetProductToAuctionAsync(productToAuction, productToAuction.StartingPrice); + + categoryId ??= (await auctionService.GetAuctionByIdAsync(productToAuction.AuctionId))?.CategoryId; + await UpdateProductCategoryIsFeaturedAsync(false, categoryId, productToAuction.ProductId); + } + + private async Task UpdateProductCategoryIsFeaturedAsync(bool isFeatured, int? categoryId, int productId) + { + //Leszarjuk ha elszáll, az aukció menjen tovább... - J. + try + { + if (categoryId.GetValueOrDefault(0) == 0) return; + + var productCategory = (await categoryService.GetProductCategoriesByProductIdAsync(productId)).FirstOrDefault(x => x.CategoryId == categoryId); + if (productCategory == null) return; + + if (productCategory.IsFeaturedProduct == isFeatured) return; + + productCategory.IsFeaturedProduct = isFeatured; + await categoryService.UpdateProductCategoryAsync(productCategory); + } + catch (Exception ex) + { + logger.Error($"SignalRMessageHandler.UpdateProductCategoryIsFeaturedAsync(); categoryId: {categoryId}; productId: {productId}; isFeatured: {isFeatured}", ex); + } } private bool IsValidBidPrice(decimal productPrice, decimal productToAuctionCurrentPrice, decimal bidRequestPrice, Customer customer = null) @@ -318,10 +345,10 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs if (productPrice >= bidRequestPrice || productToAuctionCurrentPrice >= bidRequestPrice || bidRequestPrice != GetNextBidPrice(productToAuctionCurrentPrice)) { logger.Warning($"SignalRMessageHandler.IsValidBidPrice(); (productPrice >= bidRequestPrice || productToAuctionCurrentPrice >= bidRequestPrice || bidRequestPrice != GetNextBidPrice(productToAuctionCurrentPrice)); productPrice: {productPrice}; productToAuctionCurrentPrice: {productToAuctionCurrentPrice}; bidRequestPrice: {bidRequestPrice}", null, customer); - return true; + return false; } - return false; + return true; } private static decimal GetStepAmount(decimal currentBidPrice) => AuctionService.GetStepAmount(currentBidPrice); diff --git a/Nop.Plugin.Misc.AuctionPlugin/Nop.Plugin.Misc.AuctionPlugin.csproj b/Nop.Plugin.Misc.AuctionPlugin/Nop.Plugin.Misc.AuctionPlugin.csproj index 9df370f..384c49a 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Nop.Plugin.Misc.AuctionPlugin.csproj +++ b/Nop.Plugin.Misc.AuctionPlugin/Nop.Plugin.Misc.AuctionPlugin.csproj @@ -182,12 +182,6 @@ - - - Always - - - diff --git a/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs b/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs index aa87929..972b593 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs @@ -417,9 +417,11 @@ public class AuctionService( return auctionDto; } + public async Task GetAuctionByIdAsync(int auctionId) => await ctx.Auctions.GetByIdAsync(auctionId); + public async Task GetAuctionDtoByIdAsync(int auctionId, bool widthProducts, bool activeProductOnly) { - var auction = await ctx.Auctions.GetByIdAsync(auctionId); + var auction = await GetAuctionByIdAsync(auctionId); if (auction == null) return null; var auctionDto = new AuctionDto(auction); diff --git a/Nop.Plugin.Misc.AuctionPlugin/Services/IAuctionService.cs b/Nop.Plugin.Misc.AuctionPlugin/Services/IAuctionService.cs index 9c2b8a0..13167e0 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Services/IAuctionService.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Services/IAuctionService.cs @@ -45,6 +45,7 @@ public interface IAuctionService Task GetAuctionDtoWithAuctionBids(int auctionId, bool activeProductOnly, int maxBidsCount); Task> GetProductToAuctionsByAuctionIdAsync(int auctionId, bool onlyActiveItems); + Task GetAuctionByIdAsync(int auctionId); Task GetAuctionDtoByIdAsync(int auctionId, bool widthProducts, bool activeProductOnly); Task> GetProductToAuctionDtosByAuctionId(int auctionId, bool activeProductOnly);