signalR client improvements, fixes

This commit is contained in:
Loretta 2024-11-19 19:29:09 +01:00
parent ae63aa8edc
commit 59ae22f5cd
4 changed files with 42 additions and 14 deletions

View File

@ -40,8 +40,9 @@
}); });
$('.toast-success').css("background-color", "#4caf50"); $('.toast-success').css("background-color", "#4caf50");
const publicProductBidBox = document.getElementById("publicProductBidBox"); var publicProductBidBox = document.getElementById("publicProductBidBox");
if (publicProductBidBox) { if (publicProductBidBox)
{
refreshPublicBidBox(data); refreshPublicBidBox(data);
} }

View File

@ -10,6 +10,7 @@ using Nop.Services.Logging;
using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities; using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities;
using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Serialization;
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using Nop.Core;
namespace Nop.Plugin.Misc.AuctionPlugin.Hubs namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
{ {
@ -19,13 +20,15 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
protected readonly IProductService _productService; protected readonly IProductService _productService;
protected readonly AuctionService _auctionService; protected readonly AuctionService _auctionService;
private IHubContext<AuctionHub> _hubContext; private IHubContext<AuctionHub> _hubContext;
private readonly IWorkContext _workContext;
public SignalRMessageHandler(ILogger logger, IProductService productService, AuctionService auctionService, IHubContext<AuctionHub> hubContext) public SignalRMessageHandler(ILogger logger, IProductService productService, AuctionService auctionService, IHubContext<AuctionHub> hubContext, IWorkContext workContext)
{ {
_logger = logger; _logger = logger;
_productService = productService; _productService = productService;
_auctionService = auctionService; _auctionService = auctionService;
_hubContext = hubContext; _hubContext = hubContext;
_workContext = workContext;
} }
public async Task HandleMessage(MessageWrapper message) public async Task HandleMessage(MessageWrapper message)
@ -36,9 +39,15 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
return; 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) switch (message.MessageType)
{ {
//nameof(IAuctionHubClient.SendPrice)
case "BidRequestMessage": case "BidRequestMessage":
await HandleBidRequest(message.Data.ToString()?.JsonTo<AuctionBidRequest>()); await HandleBidRequest(message.Data.ToString()?.JsonTo<AuctionBidRequest>());
break; 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}"); 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); var product = await _productService.GetProductByIdAsync(bidRequestMessage.ProductId);
if (product == null) if (product == null)
{ {
@ -80,9 +95,9 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
auctionBid.ProductAuctionMappingId = mapping.FirstOrDefault()!.Id; auctionBid.ProductAuctionMappingId = mapping.FirstOrDefault()!.Id;
//TODO: validate the bidprice amount //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; return;
} }
@ -99,8 +114,8 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
MessageType = "bidNotification", MessageType = "bidNotification",
Data = new BidNotificationMessage Data = new BidNotificationMessage
{ {
ProductName = bidRequestMessage.ProductId.ToString(), ProductName = auctionBid.ProductId.ToString(),
BidPrice = bidRequestMessage.BidPrice.ToString(CultureInfo.InvariantCulture), BidPrice = auctionBid.BidPrice.ToString(CultureInfo.InvariantCulture),
NextStepAmount = "50000" NextStepAmount = "50000"
} }
}; };

View File

@ -74,7 +74,7 @@ public class AuctionService : IAuctionService
private async Task<bool> ValidateAuctionBid(AuctionBid auctionBid) private async Task<bool> 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... //etc...
return true; return true;

View File

@ -21,7 +21,7 @@
<span class="value">@String.Format("{0:c}", Model.LicitStep)</span> <span class="value">@String.Format("{0:c}", Model.LicitStep)</span>
</div> </div>
<div> <div>
<button id="signalRBidButton" class="btn btn-success"> <button id="signalRBidButton" class="btn btn-success" type="button">
Bid @String.Format("{0:c}", Model.BidPrice) Bid @String.Format("{0:c}", Model.BidPrice)
</button> </button>
@* <button id="bidButton" class="btn btn-success"> @* <button id="bidButton" class="btn btn-success">
@ -81,6 +81,10 @@
// }); // });
$("#signalRBidButton").on("click", function () { $("#signalRBidButton").on("click", function () {
document.getElementById("signalRBidButton").disabled = true;
event.preventDefault();
var bidMessage = { var bidMessage = {
AuctionId: pageViewModel.AuctionId.toFixed(), AuctionId: pageViewModel.AuctionId.toFixed(),
BidPrice: pageViewModel.BidPrice.toFixed(2), BidPrice: pageViewModel.BidPrice.toFixed(2),
@ -90,7 +94,7 @@
sendMessageToServer("BidRequestMessage", bidMessage); sendMessageToServer("BidRequestMessage", bidMessage);
return false;
}); });
@ -114,13 +118,21 @@
// } // }
// }); // });
const widgetPriceElement = document.getElementById("price-value-"+pageViewModel.ProductId); var widgetPriceElement = document.getElementById("price-value-"+pageViewModel.ProductId);
const budButtonelement = document.getElementById("signalRBidButton"); var budButtonElement = document.getElementById("signalRBidButton");
if (widgetPriceElement) { if (widgetPriceElement) {
widgetPriceElement.textContent = data.bidPrice; // Update the price widgetPriceElement.textContent = data.bidPrice; // Update the price
budButtonElement.textContent = data.bidPrice + data.nextStep;
pageViewModel.BidPrice = Number(data.bidPrice) + Number(data.nextStepAmount);
budButtonElement.textContent = pageViewModel.BidPrice.toFixed(2);
// if (pageViewModel.CustomerId == data.CustomerId) {
// }
console.log(`WidgetPrice updated to: ${data.bidPrice}`); console.log(`WidgetPrice updated to: ${data.bidPrice}`);
budButtonElement.disabled = false;
} else { } else {
console.warn("Element with ID 'WidgetPrice' not found in the DOM."); console.warn("Element with ID 'WidgetPrice' not found in the DOM.");
} }