Mango.Nop.Plugins/Nop.Plugin.Misc.AuctionPlugin/Hubs/SignalRMessageHandler.cs

134 lines
5.1 KiB
C#

using Nop.Plugin.Misc.AuctionPlugin.Hubs.Messages;
using Newtonsoft.Json.Linq;
using Nop.Plugin.Misc.AuctionPlugin.Models;
using Nop.Plugin.Misc.AuctionPlugin.Services;
using Nop.Services.Catalog;
using Newtonsoft.Json;
using Nop.Services.Logging;
using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities;
using Newtonsoft.Json.Serialization;
using Microsoft.AspNetCore.SignalR;
namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
{
public class SignalRMessageHandler
{
protected readonly ILogger _logger;
protected readonly IProductService _productService;
protected readonly AuctionService _auctionService;
private IHubContext<AuctionHub> _hubContext;
public SignalRMessageHandler(ILogger logger, IProductService productService, AuctionService auctionService, IHubContext<AuctionHub> hubContext)
{
_logger = logger;
_productService = productService;
_auctionService = auctionService;
_hubContext = hubContext;
}
public async Task HandleMessage(MessageWrapper message)
{
switch (message.MessageType)
{
//nameof(IAuctionHubClient.SendPrice)
case "BidRequestMessage":
await HandleBidRequest(message.Data);
break;
// Add other message types here
default:
await _logger.ErrorAsync("Unknown message type");
break;
}
}
private async Task HandleBidRequest(object data)
{
AuctionBidRequest bidRequestMessage = new AuctionBidRequest();
try
{
var a = JsonConvert.DeserializeObject<AuctionBidRequest>(data.ToString());
//var b = (message.Data as JObject)?.ToObject<AuctionBidRequest>();
if (a == null)
{
await _logger.InformationAsync("Deserialization returned null. Message data might not match the expected structure.");
}
bidRequestMessage = a;
}
catch (Exception ex)
{
await _logger.ErrorAsync("Error during deserialization of AuctionBidRequest", ex);
}
//AuctionBidRequest bidRequestMessage = new AuctionBidRequest();
try
{
if (bidRequestMessage != null)
{
await _logger.InformationAsync($"Bid received: - Auction:{bidRequestMessage.AuctionId} Product: {bidRequestMessage.ProductId} - Bid: {bidRequestMessage.BidPrice} - Customer: {bidRequestMessage.CustomerId}");
//get product
var product = await _productService.GetProductByIdAsync(Convert.ToInt32(bidRequestMessage.ProductId));
if (product != null)
{
//validate the bidprice amount
//set new price
product.Price = Convert.ToDecimal(bidRequestMessage.BidPrice);
}
List<ProductToAuctionMapping> mapping = await _auctionService.GetProductToAuctionByAuctionIdAndProductIdAsync(Convert.ToInt32(bidRequestMessage.AuctionId), Convert.ToInt32(bidRequestMessage.ProductId));
AuctionBid auctionBid = new AuctionBid();
auctionBid.ProductId = Convert.ToInt32(bidRequestMessage.ProductId);
auctionBid.CustomerId = Convert.ToInt32(bidRequestMessage.CustomerId);
auctionBid.BidPrice = Convert.ToDecimal(bidRequestMessage.BidPrice);
auctionBid.ProductAuctionMappingId = mapping.FirstOrDefault().Id;
//save bid
try
{
await _auctionService.InsertBidAsync(auctionBid);
}
catch (Exception e)
{
_logger.Error($"MessageHandling InsertBidAsync error: {e.ToString()}");
}
//update product
await _productService.UpdateProductAsync(product);
// Optionally broadcast to all clients
var bid = new MessageWrapper
{
MessageType = "bidNotification",
Data = new BidNotificationMessage
{
ProductName = bidRequestMessage.ProductId,
BidPrice = bidRequestMessage.BidPrice,
NextStepAmount = "50000"
}
};
var jsonMessage = JsonConvert.SerializeObject(bid, Formatting.Indented,
new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }
);
await _hubContext.Clients.All.SendAsync("send", jsonMessage);
}
}
catch (Exception ex)
{
_logger.Error($"MessageHandling error: {ex.ToString()}");
}
}
}
}