From 59ae22f5cd51e63d2eca5d93022bd9fdeece7dab Mon Sep 17 00:00:00 2001 From: Loretta Date: Tue, 19 Nov 2024 19:29:09 +0100 Subject: [PATCH] signalR client improvements, fixes --- .../Content/Js/MgMessageHandler.js | 5 ++-- .../Hubs/SignalRMessageHandler.cs | 27 ++++++++++++++----- .../Services/AuctionService.cs | 2 +- .../Views/PublicProductBidBox.cshtml | 22 +++++++++++---- 4 files changed, 42 insertions(+), 14 deletions(-) diff --git a/Nop.Plugin.Misc.AuctionPlugin/Content/Js/MgMessageHandler.js b/Nop.Plugin.Misc.AuctionPlugin/Content/Js/MgMessageHandler.js index ceb6f54..bf6c458 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Content/Js/MgMessageHandler.js +++ b/Nop.Plugin.Misc.AuctionPlugin/Content/Js/MgMessageHandler.js @@ -40,8 +40,9 @@ }); $('.toast-success').css("background-color", "#4caf50"); - const publicProductBidBox = document.getElementById("publicProductBidBox"); - if (publicProductBidBox) { + var publicProductBidBox = document.getElementById("publicProductBidBox"); + if (publicProductBidBox) + { refreshPublicBidBox(data); } diff --git a/Nop.Plugin.Misc.AuctionPlugin/Hubs/SignalRMessageHandler.cs b/Nop.Plugin.Misc.AuctionPlugin/Hubs/SignalRMessageHandler.cs index f539544..3504dcf 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Hubs/SignalRMessageHandler.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Hubs/SignalRMessageHandler.cs @@ -10,6 +10,7 @@ using Nop.Services.Logging; using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities; using Newtonsoft.Json.Serialization; using Microsoft.AspNetCore.SignalR; +using Nop.Core; namespace Nop.Plugin.Misc.AuctionPlugin.Hubs { @@ -19,13 +20,15 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs protected readonly IProductService _productService; protected readonly AuctionService _auctionService; private IHubContext _hubContext; + private readonly IWorkContext _workContext; - public SignalRMessageHandler(ILogger logger, IProductService productService, AuctionService auctionService, IHubContext hubContext) + public SignalRMessageHandler(ILogger logger, IProductService productService, AuctionService auctionService, IHubContext hubContext, IWorkContext workContext) { _logger = logger; _productService = productService; _auctionService = auctionService; _hubContext = hubContext; + _workContext = workContext; } public async Task HandleMessage(MessageWrapper message) @@ -36,9 +39,15 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs return; } + //TODO: A MessageWrapper-ben kéne küldözgetni a UserId (CustomerId-t) - J. + //if (message.UserId != (await _workContext.GetCurrentCustomerAsync()).Id) + //{ + // _logger.Error($"SignalRMessageHandler.HandleMessage(); message.UserId != (await _workContext.GetCurrentCustomerAsync()).Id"); + // return; + //} + switch (message.MessageType) { - //nameof(IAuctionHubClient.SendPrice) case "BidRequestMessage": await HandleBidRequest(message.Data.ToString()?.JsonTo()); break; @@ -62,6 +71,12 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs { await _logger.InformationAsync($"SignalRMessageHandler.HandleBidRequest(); Bid received: - Auction: {bidRequestMessage.AuctionId} Product: {bidRequestMessage.ProductId} - Bid: {bidRequestMessage.BidPrice} - Customer: {bidRequestMessage.CustomerId}"); + if (bidRequestMessage.CustomerId != (await _workContext.GetCurrentCustomerAsync()).Id) + { + _logger.Error($"SignalRMessageHandler.HandleBidRequest(); bidRequestMessage.CustomerId != (await _workContext.GetCurrentCustomerAsync()).Id"); + return; + } + var product = await _productService.GetProductByIdAsync(bidRequestMessage.ProductId); if (product == null) { @@ -80,9 +95,9 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs auctionBid.ProductAuctionMappingId = mapping.FirstOrDefault()!.Id; //TODO: validate the bidprice amount - if (product.Price >= bidRequestMessage.BidPrice) + if (product.Price >= auctionBid.BidPrice) { - _logger.Warning($"SignalRMessageHandler.HandleBidRequest(); product.Price >= bidRequestMessage.BidPrice; productPrice: {product.Price}; bidRequestPrice: {bidRequestMessage.BidPrice}"); + _logger.Warning($"SignalRMessageHandler.HandleBidRequest(); product.Price >= bidRequestMessage.BidPrice; productPrice: {product.Price}; bidRequestPrice: {auctionBid.BidPrice}"); return; } @@ -99,8 +114,8 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs MessageType = "bidNotification", Data = new BidNotificationMessage { - ProductName = bidRequestMessage.ProductId.ToString(), - BidPrice = bidRequestMessage.BidPrice.ToString(CultureInfo.InvariantCulture), + ProductName = auctionBid.ProductId.ToString(), + BidPrice = auctionBid.BidPrice.ToString(CultureInfo.InvariantCulture), NextStepAmount = "50000" } }; diff --git a/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs b/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs index 33f7792..c8b4d23 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs @@ -74,7 +74,7 @@ public class AuctionService : IAuctionService private async Task ValidateAuctionBid(AuctionBid auctionBid) { - auctionBid.CustomerId = (await _workContext.GetCurrentCustomerAsync()).Id; //elvileg megnézi cache-ben, csak utána DB-zik! - J. + //auctionBid.CustomerId = (await _workContext.GetCurrentCustomerAsync()).Id; //elvileg megnézi cache-ben, csak utána DB-zik! - J. //etc... return true; diff --git a/Nop.Plugin.Misc.AuctionPlugin/Views/PublicProductBidBox.cshtml b/Nop.Plugin.Misc.AuctionPlugin/Views/PublicProductBidBox.cshtml index da4ca46..d674954 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Views/PublicProductBidBox.cshtml +++ b/Nop.Plugin.Misc.AuctionPlugin/Views/PublicProductBidBox.cshtml @@ -21,7 +21,7 @@ @String.Format("{0:c}", Model.LicitStep)
- @*