This commit is contained in:
Loretta 2024-11-19 11:30:30 +01:00
parent d7dffd1485
commit 992e4a6c40
4 changed files with 68 additions and 57 deletions

View File

@ -50,14 +50,16 @@
};
addAntiForgeryToken(postData);
console.log("postData: " + JSON.stringify(postData));
var jsonData = JSON.stringify(postData);
console.log("postData: " + jsonData);
debugger;
// Perform AJAX call
$.ajax({
url: "/Admin/AuctionPluginAdmin/AssignProductToAuction", // Update to match your endpoint
type: "POST",
contentType: "application/json",
data: JSON.stringify(postData),
data: jsonData,
traditional: true,
success: function (response) {
alert("Product successfully assigned to the auction!");

View File

@ -30,6 +30,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
{
switch (message.MessageType)
{
//nameof(IAuctionHubClient.SendPrice)
case "BidRequestMessage":
await HandleBidRequest(message.Data);
break;
@ -52,6 +53,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
{
await _logger.InformationAsync("Deserialization returned null. Message data might not match the expected structure.");
}
bidRequestMessage = a;
}
catch (Exception ex)
@ -60,66 +62,72 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
}
//AuctionBidRequest bidRequestMessage = new AuctionBidRequest();
if (bidRequestMessage != null)
try
{
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)
if (bidRequestMessage != null)
{
//validate the bidprice amount
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));
//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
{
var result = _auctionService.InsertBidAsync(auctionBid);
}
catch(Exception ex)
{
_logger.Error($"MessageHandling error: {ex.ToString()}");
}
//update product
await _productService.UpdateProductAsync(product);
// Optionally broadcast to all clients
var bid = new MessageWrapper
{
MessageType = "bidNotification",
Data = new BidNotificationMessage
if (product != null)
{
ProductName = bidRequestMessage.ProductId,
BidPrice = bidRequestMessage.BidPrice,
NextStepAmount = "50000"
}
};
var jsonMessage = JsonConvert.SerializeObject(bid, Formatting.Indented,
new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }
);
//validate the bidprice amount
await _hubContext.Clients.All.SendAsync("send", jsonMessage);
//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()}");
}
}
}
}

View File

@ -211,12 +211,15 @@ public class AuctionService : IAuctionService
return new List<ProductToAuctionMapping>(await _ctx.ProductToAuctions.GetByAuctionAndProductId(auctionId, productId).ToListAsync());
}
public async Task<bool> AssignProductToAuctionAsync(int productId, decimal startingPrice, decimal bidPrice, int auctionId)
public async Task<ProductToAuctionMapping> AssignProductToAuctionAsync(int productId, decimal startingPrice, decimal bidPrice, int auctionId)
{
var auction = await GetAuctionDtoByIdAsync(auctionId);
if (auction == null)
return null;
var existedProductToAuction = (await GetProductToAuctionByAuctionIdAndProductIdAsync(auctionId, productId)).FirstOrDefault();
if (existedProductToAuction != null) return existedProductToAuction;
var mapping = new ProductToAuctionMapping
{
ProductId = productId,

View File

@ -50,6 +50,4 @@ public interface IAuctionService
Task<List<ProductToAuctionMapping>> GetProductToAuctionsByProductIdAsync(int productId);
Task<List<ProductToAuctionMapping>> GetProductToAuctionByAuctionIdAndProductIdAsync(int auctionId, int productId);
Task<bool> AssignProductToAuctionAsync(int productId, decimal startingPrice, decimal bidPrice, int auctionId);
}