AuctionHub improvements, fixes; Client Timeout set to 20p; etc...

This commit is contained in:
Loretta 2024-12-06 07:19:41 +01:00
parent aae7504589
commit fa07f23d70
6 changed files with 700 additions and 335 deletions

View File

@ -28,6 +28,8 @@
// SignalR connection setup
var connection = new signalR.HubConnectionBuilder()
.withUrl("/auctionhub")
.withServerTimeout(1200000)
.withKeepAliveInterval(600000)
.build();
connection.on("send", data => {

View File

@ -2,7 +2,8 @@
public enum ResponseType : byte
{
Error = 0,
None = 0,
ToCaller = 10,
ToAllClients = 20,
Error = 30,
}

View File

@ -10,15 +10,15 @@ using Nop.Plugin.Misc.AuctionPlugin.Domains.Enums;
using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Customers;
using Nop.Services.Catalog;
namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
{
public class AuctionHub(SessionService sessionService, ILogger logger, SignalRMessageHandler signalRMessageHandler, IWorkContext workContext, ICustomerService customerService)
public class AuctionHub(SessionService sessionService, ILogger logger, IProductService productService, AuctionService auctionService, IWorkContext workContext, ICustomerService customerService, ICategoryService categoryService)
: Hub<IAuctionHubClient>
{
private readonly SemaphoreSlim _handleMessageMutex = new SemaphoreSlim(1);
//HubCallerContext _hubCallerContext;
private readonly SemaphoreSlim _handleMessageMutex = new(1);
public override async Task OnConnectedAsync()
{
@ -52,19 +52,12 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
return base.OnDisconnectedAsync(exception);
}
public async Task ReceiveRegularMessageFromClient(string message)
{
IncrementRequestCount();
await Clients.All.SendAsync("Send", message);
}
public async Task ReceiveMessageFromClient(MessageWrapper messageWrapper)
{
var sessionItem = IncrementRequestCount();
await HandleMessageAsync(messageWrapper, sessionItem, Context.ConnectionId);
await SendMessageWrapperAsync(messageWrapper);
//await SendMessageWrapperAsync(messageWrapper);
}
public async Task<ResponseType> HandleMessageAsync(MessageWrapper messageWrapper, SessionItem sessionItem, string connectionId)
@ -96,7 +89,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
{
await logger.InformationAsync($"AuctionHub.HandleMessageAsync(); Enter lock; MessageType: {messageWrapper.MessageType}; connectionId: {connectionId}", null, customer);
if (messageWrapper.MessageType == "BidRequestMessage") return await signalRMessageHandler.HandleBidRequestAsync(customer, messageWrapper);
if (messageWrapper.MessageType == "BidRequestMessage") return await HandleBidRequestAsync(customer, messageWrapper);
else
{
if (!await customerService.IsAdminAsync(customer))
@ -108,19 +101,16 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
switch (messageWrapper.MessageType)
{
case nameof(AuctionProductStatusRequest):
return await signalRMessageHandler.HandleProductToAuctionStatusChangedRequest(customer, messageWrapper);
return await HandleProductToAuctionStatusChangedRequest(customer, messageWrapper);
case nameof(RevertAuctionBidRequest):
return await signalRMessageHandler.HandleRevertAuctionBidRequest(customer, messageWrapper);
return await HandleRevertAuctionBidRequest(customer, messageWrapper);
default:
// Add other message types here
await logger.ErrorAsync($"AuctionHub.HandleMessageAsync(); Unknown message type; MessageType: {messageWrapper.MessageType}; connectionId: {connectionId}", null, customer);
return ResponseType.Error;
}
}
//await logger.InformationAsync($"AuctionHub.HandleMessageAsync(); Exit lock; MessageType: {messageWrapper.MessageType}; connectionId: {connectionId}", null, customer);
}
catch (Exception ex)
{
@ -135,19 +125,364 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
return ResponseType.Error;
}
public async Task<ResponseType> HandleRevertAuctionBidRequest(Customer customer, MessageWrapper messageWrapper)
{
var revertAuctionBidRequest = messageWrapper.Data.JsonTo<RevertAuctionBidRequest>();
if (revertAuctionBidRequest == null)
{
logger.Error($"AuctionHub.HandleRevertAuctionBidRequest(); auctionProductStatusRequest == null", null, customer);
return ResponseType.Error;
}
try
{
var productToAuction = (await auctionService.GetProductToAuctionMappingByIdAsync(revertAuctionBidRequest.ProductToAuctionId));
if (productToAuction == null)
{
logger.Error($"AuctionHub.HandleRevertAuctionBidRequest(); (productToAuction == null);", null, customer);
return ResponseType.Error;
}
await logger.InformationAsync($"AuctionHub.HandleRevertAuctionBidRequest(); (productToAuction is not {{ AuctionStatus: AuctionStatus.Pause }}); AuctionStatus: {productToAuction.AuctionStatus}", null, customer);
var product = await auctionService.GetProductById(productToAuction.ProductId);
if (product == null)
{
logger.Error($"AuctionHub.HandleBidRequestAsync(); (product == null);", null, customer);
return ResponseType.Error;
}
if (productToAuction.AuctionStatus != AuctionStatus.Pause)
{
logger.Warning($"AuctionHub.HandleRevertAuctionBidRequest(); (productToAuction.AuctionStatus != AuctionStatus.Pause; AuctionStatus: {productToAuction.AuctionStatus}", null, customer);
await SendAuctionBidMessageAsync(messageWrapper, productToAuction, product, customer.Id, ResponseType.ToCaller);
return ResponseType.Error;
}
var revertLastBid = await auctionService.RevertAuctionBidByProductToAuctionIdAsync(productToAuction.Id);
if (revertLastBid == null) await ResetProductToAuction(productToAuction);
else
{
productToAuction.BidsCount = await auctionService.GetBidsCountByProductToAuctionIdAsync(productToAuction.Id);
await SetAuctionBidPrice(revertLastBid.BidPrice, productToAuction, product, revertLastBid.CustomerId);
}
await SendAuctionBidMessageAsync(messageWrapper, productToAuction, product, customer.Id, ResponseType.ToAllClients);
return ResponseType.ToAllClients;
}
catch (Exception ex)
{
logger.Error($"AuctionHub.HandleRevertAuctionBidRequest(); Exception: {ex.Message}", ex, customer);
}
return ResponseType.Error;
}
public async Task<ResponseType> HandleBidRequestAsync(Customer customer, MessageWrapper messageWrapper)
{
var bidRequestMessage = messageWrapper.Data.JsonTo<AuctionBidRequest>();
if (bidRequestMessage == null)
{
logger.Error($"AuctionHub.HandleBidRequestAsync(); (bidRequestMessage == null)", null, customer);
return ResponseType.Error;
}
try
{
await logger.InformationAsync($"AuctionHub.HandleBidRequestAsync(); Bid received; Auction: {bidRequestMessage.AuctionId}; ProductToAuction: {bidRequestMessage.ProductAuctionMappingId}; Product: {bidRequestMessage.ProductId}; Bid: {bidRequestMessage.BidPrice}; Customer: {bidRequestMessage.CustomerId}", null, customer);
var auction = await auctionService.GetAuctionDtoByIdAsync(bidRequestMessage.AuctionId, false, false);
if (auction == null || auction.Closed)
{
logger.Error($"AuctionHub.HandleBidRequestAsync(); (auction == null || auction.Closed); Closed: {auction?.Closed}", null, customer);
return ResponseType.Error;
}
var product = await auctionService.GetProductById(bidRequestMessage.ProductId);
if (product == null)
{
logger.Error($"AuctionHub.HandleBidRequestAsync(); (product == null);", null, customer);
return ResponseType.Error;
}
var activeProductAuction = (await auctionService.GetProductToAuctionByAuctionIdAndProductIdAsync(bidRequestMessage.AuctionId, bidRequestMessage.ProductId, true)).FirstOrDefault();
if (activeProductAuction is not { IsActiveItem: true } || (activeProductAuction.WinnerCustomerId == customer.Id && !await customerService.IsAdminAsync(customer)))
{
logger.Warning($"AuctionHub.HandleBidRequestAsync(); (activeProductAuction is not {{ IsActiveItem: true }} || activeProductAuction.WinnerCustomerId == customer.Id && !await customerService.IsAdminAsync(customer)); AuctionStatus: {activeProductAuction?.AuctionStatus}; WinnerCustomerId: {activeProductAuction?.WinnerCustomerId}", null, customer);
await SendAuctionBidMessageAsync(messageWrapper, activeProductAuction, product, customer.Id, ResponseType.ToCaller);
return ResponseType.Error;
}
if (!IsValidBidPrice(activeProductAuction.CurrentPrice, bidRequestMessage.BidPrice, activeProductAuction.BidsCount > 0, customer))
{
await SendAuctionBidMessageAsync(messageWrapper, activeProductAuction, product, customer.Id, ResponseType.ToCaller);
return ResponseType.Error;
}
var auctionBid = bidRequestMessage.CreateMainEntity();
auctionBid.ProductAuctionMappingId = activeProductAuction.Id;
await auctionService.InsertBidAsync(auctionBid);
activeProductAuction.BidsCount++;
activeProductAuction.AuctionStatus = AuctionStatus.Active;
if (!await SetAuctionBidPrice(auctionBid.BidPrice, activeProductAuction, product, customer.Id))
{
await SendAuctionBidMessageAsync(messageWrapper, activeProductAuction, product, customer.Id, ResponseType.ToCaller);
return ResponseType.Error;
}
await SendAuctionBidMessageAsync(messageWrapper, activeProductAuction, product, customer.Id, ResponseType.ToAllClients);
return ResponseType.ToAllClients;
}
catch (Exception ex)
{
logger.Error($"AuctionHub.HandleBidRequestAsync(); Exception: {ex.Message}", ex, customer);
}
return ResponseType.Error;
}
public async Task<ResponseType> HandleProductToAuctionStatusChangedRequest(Customer customer, MessageWrapper statusChangedMessageWrapper)
{
var auctionProductStatusRequest = statusChangedMessageWrapper.Data.JsonTo<AuctionProductStatusRequest>();
if (auctionProductStatusRequest == null)
{
logger.Error($"AuctionHub.HandleProductToAuctionStatusChangedRequest(); auctionProductStatusRequest == null", null, customer);
return ResponseType.Error;
}
try
{
//var responseType = ResponseType.ToAllClients;
await logger.InformationAsync($"AuctionHub.HandleProductToAuctionStatusChangedRequest(); ProductToAuctionMappingId: {auctionProductStatusRequest.ProductToAuctionId}; Status: {auctionProductStatusRequest.AuctionStatus}({(int)auctionProductStatusRequest.AuctionStatus})", null, customer);
var productToAuction = await auctionService.GetProductToAuctionMappingByIdAsync(auctionProductStatusRequest.ProductToAuctionId);
if (productToAuction == null)
{
logger.Error($"AuctionHub.HandleProductToAuctionStatusChangedRequest(); (productToAuction == null)", null, customer);
return ResponseType.Error;
}
var auction = await auctionService.GetAuctionByIdAsync(productToAuction.AuctionId);
if (auction == null || auction.Closed)
{
logger.Error($"AuctionHub.HandleProductToAuctionStatusChangedRequest(); (auction == null || auction.Closed); Closed: {auction?.Closed}", null, customer);
return ResponseType.Error;
}
if (!IsValidRequestAuctionStatus(auctionProductStatusRequest.AuctionStatus, productToAuction.AuctionStatus))
{
logger.Error($"AuctionHub.HandleProductToAuctionStatusChangedRequest(); RequestAuctionStatusIsValid() == false; newStatus: {auctionProductStatusRequest.AuctionStatus}; oldStatus: {productToAuction.AuctionStatus}", null, customer);
await SendStatusChangedNotificationAsync(customer, statusChangedMessageWrapper, productToAuction, ResponseType.ToCaller);
return ResponseType.Error;
}
else if (auctionProductStatusRequest.AuctionStatus == AuctionStatus.None) await ResetProductToAuction(productToAuction, auction.CategoryId);
else
{
switch (auctionProductStatusRequest.AuctionStatus)
{
case AuctionStatus.Active:
productToAuction.AuctionStatus = AuctionStatus.Active;
await UpdateProductCategoryIsFeaturedAsync(productToAuction.ProductId, auction.CategoryId, true);
break;
case AuctionStatus.FirstWarning:
case AuctionStatus.SecondWarning:
productToAuction.AuctionStatus = auctionProductStatusRequest.AuctionStatus;
break;
case AuctionStatus.Sold:
var lastAuctionBid = await auctionService.GetLastAuctionBidByProductToAuctionIdAsync(productToAuction.Id);
if (lastAuctionBid == null) productToAuction.AuctionStatus = AuctionStatus.NotSold;
else
{
productToAuction.AuctionStatus = AuctionStatus.Sold;
productToAuction.CurrentPrice = lastAuctionBid.BidPrice;
productToAuction.WinnerCustomerId = lastAuctionBid.CustomerId;
var placeOrderResult = await auctionService.CreateOrderForWinnerAsync(productToAuction);
if (placeOrderResult == null || placeOrderResult.Id == 0)
{
logger.Error($"AuctionHub.HandleProductToAuctionStatusChangedRequest(); (placeOrderResult == null || placeOrderResult.Id == 0)", null, customer);
//return; //TODO: EGYELŐRE HAGYJUK LEZÁRNI AKKOR IS, HA NEM SIKERÜLT AZ ORDERPLACE()! - J.
}
}
await UpdateProductCategoryIsFeaturedAsync(productToAuction.ProductId, auction.CategoryId, false);
break;
case AuctionStatus.Pause:
productToAuction.AuctionStatus = AuctionStatus.Pause;
break;
default:
logger.Error($"AuctionHub.HandleProductToAuctionStatusChangedRequest(); AuctionStatus not found; (auctionProductStatusRequest.AuctionStatus == {auctionProductStatusRequest.AuctionStatus})", null, customer);
await SendStatusChangedNotificationAsync(customer, statusChangedMessageWrapper, productToAuction, ResponseType.ToCaller);
return ResponseType.Error;
}
await auctionService.UpdateProductToAuctionMappingAsync(productToAuction);
}
await SendStatusChangedNotificationAsync(customer, statusChangedMessageWrapper, productToAuction, ResponseType.ToAllClients);
//TODO: gond van ha az Admin valamit módosít és újrazár egy régi ProductToAuction-t!!!! AuctionRequestMode.Normal...- J.
if (/*AuctionRequestMode.Normal &&*/ auction.AuctionType == AuctionType.AutomaticNext && productToAuction.AuctionStatus is AuctionStatus.Sold or AuctionStatus.NotSold)
await StepNextProductToAuctionAsync(statusChangedMessageWrapper, customer, productToAuction, auctionProductStatusRequest);
return ResponseType.ToAllClients;
}
catch (Exception ex)
{
logger.Error($"AuctionHub.HandleProductToAuctionStatusChangedRequest(); Exception: {ex.Message}", ex, customer);
}
return ResponseType.Error;
}
private async Task SendStatusChangedNotificationAsync(Customer customer, MessageWrapper statusChangedMessageWrapper, ProductToAuctionMapping productToAuction, ResponseType responseType)
{
await UpdateStatusChangedNotificationMessageWrapperAsync(statusChangedMessageWrapper, customer, productToAuction, responseType);
await SendMessageWrapperAsync(statusChangedMessageWrapper);
}
private async Task StepNextProductToAuctionAsync(MessageWrapper statusChangedMessageWrapper, Customer customer, ProductToAuctionMapping productToAuction, AuctionProductStatusRequest auctionProductStatusRequest)
{
var nextProductToAuction = await auctionService.GetNextProductToAuctionByAuctionIdAsync(productToAuction.AuctionId);
if (nextProductToAuction == null) return;
await logger.InformationAsync($"AuctionHub.StepNextProductToAuctionAsync(); NextProductToAuctionId: {nextProductToAuction.Id};", null, customer);
if (nextProductToAuction.AuctionStatus != AuctionStatus.None)
{
await logger.WarningAsync($"AuctionHub.StepNextProductToAuctionAsync(); (nextProductToAuction.AuctionStatus != AuctionStatus.None); NextProductToAuctionId: {nextProductToAuction.Id}; Status: {nextProductToAuction.AuctionStatus}({(int)nextProductToAuction.AuctionStatus})", null, customer);
return; //TODO: Ilyenkor mi legyen?! - J.
}
auctionProductStatusRequest.AuctionStatus = AuctionStatus.Active;
auctionProductStatusRequest.ProductToAuctionId = nextProductToAuction.Id;
statusChangedMessageWrapper.Data = auctionProductStatusRequest.ToJson();
await HandleProductToAuctionStatusChangedRequest(customer, statusChangedMessageWrapper);
}
private async Task<bool> SetAuctionBidPrice(decimal bidPrice, ProductToAuctionMapping productToAuction, Product product, int lastBidCustomerId)
{
try
{
product.Price = bidPrice;
await productService.UpdateProductAsync(product);
//activeProductAuction.StartingPrice = product.OldPrice; //TODO: ez biztosan kezelve van mikor összerendeljük? - J.
productToAuction.CurrentPrice = bidPrice;
productToAuction.WinnerCustomerId = lastBidCustomerId;
await auctionService.UpdateProductToAuctionMappingAsync(productToAuction);
return true;
}
catch (Exception ex)
{
logger.Error($"AuctionHub.HandleBidRequest(); SetAuctionBidPrice() == false", ex);
}
return false;
}
private bool IsValidBidPrice(decimal productToAuctionCurrentPrice, decimal bidRequestPrice, bool hasAuctionBidInDb, Customer customer = null)
{
var nextBidPrice = AuctionService.GetNextBidPrice(productToAuctionCurrentPrice, hasAuctionBidInDb);
if (bidRequestPrice != nextBidPrice)
{
logger.Warning($"AuctionHub.IsValidBidPrice(); (bidRequestPrice != nextBidPrice); productToAuctionCurrentPrice: {productToAuctionCurrentPrice}; bidRequestPrice: {bidRequestPrice}; hasAuctionBidInDb: {hasAuctionBidInDb}", null, customer);
return false;
}
return true;
}
private async Task UpdateStatusChangedNotificationMessageWrapperAsync(MessageWrapper statusChangedMessageWrapper, Customer customer, ProductToAuctionMapping productToAuction, ResponseType responseType)
{
var bidsCount = productToAuction.BidsCount;
statusChangedMessageWrapper.MessageType = nameof(ProductToAuctionStatusNotification);
statusChangedMessageWrapper.SenderId = customer.Id;
statusChangedMessageWrapper.ResponseType = responseType;
statusChangedMessageWrapper.Data = new ProductToAuctionStatusNotification(await auctionService.GetAuctionDtoByProductToAuctionIdAsync(productToAuction.Id, true), bidsCount, "EMPTY").ToJson();
}
public async Task UpdateBidNotificationMessageWrapperAsync(MessageWrapper messageWrapper, ProductToAuctionMapping productToAuction, Product product, int customerId, ResponseType responseType)
{
var bidsCount = productToAuction.BidsCount; //await auctionService.GetBidsCountByProductToAuctionIdAsync(productToAuction.Id);
var stepAmount = AuctionService.GetStepAmount(productToAuction.CurrentPrice);
var nextBidPrice = AuctionService.GetNextBidPrice(productToAuction.CurrentPrice, stepAmount, bidsCount > 0);
messageWrapper.SenderId = customerId;
messageWrapper.ResponseType = responseType;
messageWrapper.MessageType = "bidNotification";
messageWrapper.Data = new BidNotificationMessage(await auctionService.GetAuctionDtoByProductToAuctionIdAsync(productToAuction.Id, true), bidsCount, "EMPTY") //TODO: NE KÉRJÜK LE DB-BŐL!!! - J.
{
ProductName = product.Name,
CurrentPrice = productToAuction.CurrentPrice,
NextStepAmount = stepAmount,
NextBidPrice = nextBidPrice,
}.ToJson();
}
private async Task ResetProductToAuction(ProductToAuctionMapping productToAuction, int? categoryId = null)
{
productToAuction.AuctionStatus = AuctionStatus.None;
await auctionService.ResetProductToAuctionAsync(productToAuction, productToAuction.StartingPrice);
categoryId ??= (await auctionService.GetAuctionByIdAsync(productToAuction.AuctionId))?.CategoryId;
await UpdateProductCategoryIsFeaturedAsync(productToAuction.ProductId, categoryId, false);
}
private async Task UpdateProductCategoryIsFeaturedAsync(int productId, int? categoryId, bool isFeatured)
{
//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($"AuctionHub.UpdateProductCategoryIsFeaturedAsync(); categoryId: {categoryId}; productId: {productId}; isFeatured: {isFeatured}", ex);
}
}
public async Task SendAuctionBidMessageAsync(MessageWrapper messageWrapper, ProductToAuctionMapping productToAuction, Product product, int customerId, ResponseType responseType)
{
await UpdateBidNotificationMessageWrapperAsync(messageWrapper, productToAuction, product, customerId, responseType);
await SendMessageWrapperAsync(messageWrapper);
}
public async Task SendMessageWrapperAsync(MessageWrapper messageWrapper)
{
await logger.InformationAsync($"AuctionHub.SendMessageWrapperAsync(); MessageType: {messageWrapper.MessageType}; ResponseType: {messageWrapper.ResponseType}; jsonData: {messageWrapper.Data}");
switch (messageWrapper.ResponseType)
{
case ResponseType.ToCaller:
await Clients.Caller.send(messageWrapper.ToJson());
//await Clients.Caller.SendAsync("send", messageWrapper.ToJson());
break;
case ResponseType.ToAllClients:
await Clients.All.send(messageWrapper.ToJson());
//await Clients.All.SendAsync("send", messageWrapper.ToJson());
break;
case ResponseType.Error:
break;
@ -156,6 +491,33 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
}
}
//private async Task SendMessageToClientsAsync(MessageWrapper messageWrapper, bool sendToCaller = false)
//{
// if (sendToCaller) await Clients.Caller.send(messageWrapper.ToJson());
// else await Clients.All.send(messageWrapper.ToJson());
//}
private static bool IsValidRequestAuctionStatus(AuctionStatus newStatus, AuctionStatus oldStatus)
{
switch (oldStatus)
{
case AuctionStatus.None:
return newStatus is AuctionStatus.Active or AuctionStatus.Pause;
case AuctionStatus.Active:
return newStatus is AuctionStatus.FirstWarning or AuctionStatus.Pause;
case AuctionStatus.FirstWarning:
return newStatus is AuctionStatus.SecondWarning or AuctionStatus.Pause;
case AuctionStatus.SecondWarning:
return newStatus is AuctionStatus.Sold or AuctionStatus.Pause;
case AuctionStatus.Pause:
return newStatus is AuctionStatus.None or AuctionStatus.Active;
case AuctionStatus.Sold:
case AuctionStatus.NotSold:
default:
return false;
}
}
private SessionItem IncrementRequestCount()
{
SessionItem sessionItem = null;

View File

@ -8,7 +8,6 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
{
public interface IAuctionHubClient
{
Task SendAsync(string name, string message);
Task send(string message);
Task ReceiveMessageFromClient(string message);
Task SendPrice(string price);

View File

@ -95,353 +95,353 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
// }
//}
public async Task<ResponseType> HandleRevertAuctionBidRequest(Customer customer, MessageWrapper messageWrapper)
{
var revertAuctionBidRequest = messageWrapper.Data.JsonTo<RevertAuctionBidRequest>();
if (revertAuctionBidRequest == null)
{
logger.Error($"SignalRMessageHandler.HandleRevertAuctionBidRequest(); auctionProductStatusRequest == null", null, customer);
return ResponseType.Error;
}
//public async Task<ResponseType> HandleRevertAuctionBidRequest(Customer customer, MessageWrapper messageWrapper)
//{
// var revertAuctionBidRequest = messageWrapper.Data.JsonTo<RevertAuctionBidRequest>();
// if (revertAuctionBidRequest == null)
// {
// logger.Error($"SignalRMessageHandler.HandleRevertAuctionBidRequest(); auctionProductStatusRequest == null", null, customer);
// return ResponseType.Error;
// }
try
{
var productToAuction = (await auctionService.GetProductToAuctionMappingByIdAsync(revertAuctionBidRequest.ProductToAuctionId));
if (productToAuction is not { AuctionStatus: AuctionStatus.Pause })
{
logger.Warning($"SignalRMessageHandler.HandleRevertAuctionBidRequest(); (productToAuction is not {{ AuctionStatus: AuctionStatus.Pause }}); AuctionStatus: {productToAuction?.AuctionStatus}", null, customer);
return ResponseType.Error;
}
// try
// {
// var productToAuction = (await auctionService.GetProductToAuctionMappingByIdAsync(revertAuctionBidRequest.ProductToAuctionId));
// if (productToAuction is not { AuctionStatus: AuctionStatus.Pause })
// {
// logger.Warning($"SignalRMessageHandler.HandleRevertAuctionBidRequest(); (productToAuction is not {{ AuctionStatus: AuctionStatus.Pause }}); AuctionStatus: {productToAuction?.AuctionStatus}", null, customer);
// return ResponseType.Error;
// }
await logger.InformationAsync($"SignalRMessageHandler.HandleRevertAuctionBidRequest(); (productToAuction is not {{ AuctionStatus: AuctionStatus.Pause }}); AuctionStatus: {productToAuction.AuctionStatus}", null, customer);
// await logger.InformationAsync($"SignalRMessageHandler.HandleRevertAuctionBidRequest(); (productToAuction is not {{ AuctionStatus: AuctionStatus.Pause }}); AuctionStatus: {productToAuction.AuctionStatus}", null, customer);
var product = await auctionService.GetProductById(productToAuction.ProductId);
if (product == null) return ResponseType.Error;
// var product = await auctionService.GetProductById(productToAuction.ProductId);
// if (product == null) return ResponseType.Error;
var revertLastBid = await auctionService.RevertAuctionBidByProductToAuctionIdAsync(productToAuction.Id);
// var revertLastBid = await auctionService.RevertAuctionBidByProductToAuctionIdAsync(productToAuction.Id);
if (revertLastBid == null) await ResetProductToAuction(productToAuction);
else
{
productToAuction.BidsCount = await auctionService.GetBidsCountByProductToAuctionIdAsync(productToAuction.Id);
await SetAuctionBidPrice(revertLastBid.BidPrice, productToAuction, product, revertLastBid.CustomerId);
}
// if (revertLastBid == null) await ResetProductToAuction(productToAuction);
// else
// {
// productToAuction.BidsCount = await auctionService.GetBidsCountByProductToAuctionIdAsync(productToAuction.Id);
// await SetAuctionBidPrice(revertLastBid.BidPrice, productToAuction, product, revertLastBid.CustomerId);
// }
await SendAuctionBidMessageAsync(messageWrapper, productToAuction, product, customer.Id, ResponseType.ToAllClients);
return ResponseType.ToAllClients;
}
catch (Exception ex)
{
logger.Error($"SignalRMessageHandler.HandleRevertAuctionBidRequest(); Exception: {ex.Message}", ex, customer);
}
// await SendAuctionBidMessageAsync(messageWrapper, productToAuction, product, customer.Id, ResponseType.ToAllClients);
// return ResponseType.ToAllClients;
// }
// catch (Exception ex)
// {
// logger.Error($"SignalRMessageHandler.HandleRevertAuctionBidRequest(); Exception: {ex.Message}", ex, customer);
// }
return ResponseType.Error;
}
// return ResponseType.Error;
//}
public async Task<ResponseType> HandleProductToAuctionStatusChangedRequest(Customer customer, MessageWrapper messageWrapper)
{
var auctionProductStatusRequest = messageWrapper.Data.JsonTo<AuctionProductStatusRequest>();
if (auctionProductStatusRequest == null)
{
logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); auctionProductStatusRequest == null", null, customer);
return ResponseType.Error;
}
//public async Task<ResponseType> HandleProductToAuctionStatusChangedRequest(Customer customer, MessageWrapper messageWrapper)
//{
// var auctionProductStatusRequest = messageWrapper.Data.JsonTo<AuctionProductStatusRequest>();
// if (auctionProductStatusRequest == null)
// {
// logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); auctionProductStatusRequest == null", null, customer);
// return ResponseType.Error;
// }
try
{
var responseType = ResponseType.ToAllClients;
await logger.InformationAsync($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); ProductToAuctionMappingId: {auctionProductStatusRequest.ProductToAuctionId}; Status: {auctionProductStatusRequest.AuctionStatus}({(int)auctionProductStatusRequest.AuctionStatus})", null, customer);
// try
// {
// var responseType = ResponseType.ToAllClients;
// await logger.InformationAsync($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); ProductToAuctionMappingId: {auctionProductStatusRequest.ProductToAuctionId}; Status: {auctionProductStatusRequest.AuctionStatus}({(int)auctionProductStatusRequest.AuctionStatus})", null, customer);
var productToAuction = await auctionService.GetProductToAuctionMappingByIdAsync(auctionProductStatusRequest.ProductToAuctionId);
if (productToAuction == null)
{
logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); (productToAuction == null)", null, customer);
return ResponseType.Error;
}
// var productToAuction = await auctionService.GetProductToAuctionMappingByIdAsync(auctionProductStatusRequest.ProductToAuctionId);
// if (productToAuction == null)
// {
// logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); (productToAuction == null)", null, customer);
// return ResponseType.Error;
// }
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 ResponseType.Error;
}
// 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 ResponseType.Error;
// }
// if (!IsValidRequestAuctionStatus(auctionProductStatusRequest.AuctionStatus, productToAuction.AuctionStatus))
// {
// responseType = ResponseType.ToCaller;
// logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); RequestAuctionStatusIsValid() == false; newStatus: {auctionProductStatusRequest.AuctionStatus}; oldStatus: {productToAuction.AuctionStatus}", null, customer);
// //return ResponseType.Error;;
// }
// else if (auctionProductStatusRequest.AuctionStatus == AuctionStatus.None) await ResetProductToAuction(productToAuction, auction.CategoryId);
// else
// {
// switch (auctionProductStatusRequest.AuctionStatus)
// {
// case AuctionStatus.Active:
// productToAuction.AuctionStatus = AuctionStatus.Active;
// await UpdateProductCategoryIsFeaturedAsync(productToAuction.ProductId, auction.CategoryId, true);
// break;
// case AuctionStatus.FirstWarning:
// case AuctionStatus.SecondWarning:
// productToAuction.AuctionStatus = auctionProductStatusRequest.AuctionStatus;
// break;
// case AuctionStatus.Sold:
// var lastAuctionBid = await auctionService.GetLastAuctionBidByProductToAuctionIdAsync(productToAuction.Id);
// if (lastAuctionBid == null) productToAuction.AuctionStatus = AuctionStatus.NotSold;
// else
// {
// productToAuction.AuctionStatus = AuctionStatus.Sold;
// productToAuction.CurrentPrice = lastAuctionBid.BidPrice;
// productToAuction.WinnerCustomerId = lastAuctionBid.CustomerId;
// var placeOrderResult = await auctionService.CreateOrderForWinnerAsync(productToAuction);
// if (placeOrderResult == null || placeOrderResult.Id == 0)
// {
// logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); (placeOrderResult == null || placeOrderResult.Id == 0)", null, customer);
// //return; //TODO: EGYELŐRE HAGYJUK LEZÁRNI AKKOR IS, HA NEM SIKERÜLT AZ ORDERPLACE()! - J.
// }
// }
// await UpdateProductCategoryIsFeaturedAsync(productToAuction.ProductId, auction.CategoryId, false);
// break;
// case AuctionStatus.Pause:
// productToAuction.AuctionStatus = AuctionStatus.Pause;
// break;
// default:
// logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); AuctionStatus not found; (auctionProductStatusRequest.AuctionStatus == {auctionProductStatusRequest.AuctionStatus})", null, customer);
// return ResponseType.Error;
// }
// await auctionService.UpdateProductToAuctionMappingAsync(productToAuction);
// }
// var bidsCount = productToAuction.BidsCount;//await auctionService.GetBidsCountByProductToAuctionIdAsync(productToAuction.Id);
// var productToauctionChangedNotification = new MessageWrapper
// {
// MessageType = nameof(ProductToAuctionStatusNotification),
// SenderId = customer.Id,
// ResponseType = responseType,
// Data = new ProductToAuctionStatusNotification(await auctionService.GetAuctionDtoByProductToAuctionIdAsync(productToAuction.Id, true), bidsCount, "EMPTY").ToJson()
// };
// await SendMessageToClientsAsync(productToauctionChangedNotification, responseType == ResponseType.ToCaller);
// //TODO: gond van ha az Admin valamit módosít és újrazár egy régi ProductToAuction-t!!!! AuctionRequestMode.Normal...- J.
// if (responseType == ResponseType.ToAllClients && /*AuctionRequestMode.Normal &&*/ auction.AuctionType == AuctionType.AutomaticNext && productToAuction.AuctionStatus is AuctionStatus.Sold or AuctionStatus.NotSold)
// await StepNextProductToAuctionAsync(messageWrapper, customer, productToAuction, auctionProductStatusRequest);
// return ResponseType.ToAllClients;
// }
// catch (Exception ex)
// {
// logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); Exception: {ex.Message}", ex, customer);
// }
// return ResponseType.Error;
//}
//private async Task SendMessageToClientsAsync(MessageWrapper messageWrapper, bool sendToCaller = false)
//{
// //if (sendToCaller) await hubContext.Clients.Caller.SendAsync("send", bidMessage.ToJson());
// //else
// await hubContext.Clients.All.SendAsync("send", messageWrapper.ToJson());
//}
//public async Task<ResponseType> HandleBidRequestAsync(Customer customer, MessageWrapper messageWrapper)
//{
// var bidRequestMessage = messageWrapper.Data.JsonTo<AuctionBidRequest>();
// if (bidRequestMessage == null)
// {
// logger.Error($"SignalRMessageHandler.HandleBidRequestAsync(); (bidRequestMessage == null)", null, customer);
// return ResponseType.Error;
// }
// try
// {
// await logger.InformationAsync($"SignalRMessageHandler.HandleBidRequestAsync(); Bid received; Auction: {bidRequestMessage.AuctionId}; ProductToAuction: {bidRequestMessage.ProductAuctionMappingId}; Product: {bidRequestMessage.ProductId}; Bid: {bidRequestMessage.BidPrice}; Customer: {bidRequestMessage.CustomerId}", null, customer);
// var auction = await auctionService.GetAuctionDtoByIdAsync(bidRequestMessage.AuctionId, false, false);
// if (auction == null || auction.Closed)
// {
// logger.Warning($"SignalRMessageHandler.HandleBidRequestAsync(); (auction == null || auction.Closed); Closed: {auction?.Closed}", null, customer);
// return ResponseType.Error;
// }
// var product = await auctionService.GetProductById(bidRequestMessage.ProductId);
// if (product == null) return ResponseType.Error;
// var activeProductAuction = (await auctionService.GetProductToAuctionByAuctionIdAndProductIdAsync(bidRequestMessage.AuctionId, bidRequestMessage.ProductId, true)).FirstOrDefault();
// if (activeProductAuction is not { IsActiveItem: true } || (activeProductAuction.WinnerCustomerId == customer.Id && !await customerService.IsAdminAsync(customer)))
// {
// logger.Warning($"SignalRMessageHandler.HandleBidRequestAsync(); (activeProductAuction is not {{ IsActiveItem: true }} || activeProductAuction.WinnerCustomerId == customer.Id && !await customerService.IsAdminAsync(customer)); AuctionStatus: {activeProductAuction?.AuctionStatus}; WinnerCustomerId: {activeProductAuction?.WinnerCustomerId}", null, customer);
// await UpdateBidNotificationMessageWrapperAsync(messageWrapper, activeProductAuction, product, customer.Id, ResponseType.ToCaller);
// return ResponseType.ToCaller;
// }
// if (!IsValidBidPrice(activeProductAuction.CurrentPrice, bidRequestMessage.BidPrice, activeProductAuction.BidsCount > 0, customer))
// {
// await UpdateBidNotificationMessageWrapperAsync(messageWrapper, activeProductAuction, product, customer.Id, ResponseType.ToCaller);
// return ResponseType.ToCaller;
// }
// var auctionBid = bidRequestMessage.CreateMainEntity();
// auctionBid.ProductAuctionMappingId = activeProductAuction.Id;
if (!IsValidRequestAuctionStatus(auctionProductStatusRequest.AuctionStatus, productToAuction.AuctionStatus))
{
responseType = ResponseType.ToCaller;
logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); RequestAuctionStatusIsValid() == false; newStatus: {auctionProductStatusRequest.AuctionStatus}; oldStatus: {productToAuction.AuctionStatus}", null, customer);
//return ResponseType.Error;;
}
else if (auctionProductStatusRequest.AuctionStatus == AuctionStatus.None) await ResetProductToAuction(productToAuction, auction.CategoryId);
else
{
switch (auctionProductStatusRequest.AuctionStatus)
{
case AuctionStatus.Active:
productToAuction.AuctionStatus = AuctionStatus.Active;
await UpdateProductCategoryIsFeaturedAsync(productToAuction.ProductId, auction.CategoryId, true);
break;
// await auctionService.InsertBidAsync(auctionBid);
// activeProductAuction.BidsCount++;
case AuctionStatus.FirstWarning:
case AuctionStatus.SecondWarning:
productToAuction.AuctionStatus = auctionProductStatusRequest.AuctionStatus;
break;
case AuctionStatus.Sold:
var lastAuctionBid = await auctionService.GetLastAuctionBidByProductToAuctionIdAsync(productToAuction.Id);
if (lastAuctionBid == null) productToAuction.AuctionStatus = AuctionStatus.NotSold;
else
{
productToAuction.AuctionStatus = AuctionStatus.Sold;
productToAuction.CurrentPrice = lastAuctionBid.BidPrice;
productToAuction.WinnerCustomerId = lastAuctionBid.CustomerId;
var placeOrderResult = await auctionService.CreateOrderForWinnerAsync(productToAuction);
if (placeOrderResult == null || placeOrderResult.Id == 0)
{
logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); (placeOrderResult == null || placeOrderResult.Id == 0)", null, customer);
//return; //TODO: EGYELŐRE HAGYJUK LEZÁRNI AKKOR IS, HA NEM SIKERÜLT AZ ORDERPLACE()! - J.
}
}
await UpdateProductCategoryIsFeaturedAsync(productToAuction.ProductId, auction.CategoryId, false);
break;
case AuctionStatus.Pause:
productToAuction.AuctionStatus = AuctionStatus.Pause;
break;
default:
logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); AuctionStatus not found; (auctionProductStatusRequest.AuctionStatus == {auctionProductStatusRequest.AuctionStatus})", null, customer);
return ResponseType.Error;
}
await auctionService.UpdateProductToAuctionMappingAsync(productToAuction);
}
var bidsCount = productToAuction.BidsCount;//await auctionService.GetBidsCountByProductToAuctionIdAsync(productToAuction.Id);
var productToauctionChangedNotification = new MessageWrapper
{
MessageType = nameof(ProductToAuctionStatusNotification),
SenderId = customer.Id,
ResponseType = responseType,
Data = new ProductToAuctionStatusNotification(await auctionService.GetAuctionDtoByProductToAuctionIdAsync(productToAuction.Id, true), bidsCount, "EMPTY").ToJson()
};
await SendMessageToClientsAsync(productToauctionChangedNotification, responseType == ResponseType.ToCaller);
//TODO: gond van ha az Admin valamit módosít és újrazár egy régi ProductToAuction-t!!!! AuctionRequestMode.Normal...- J.
if (responseType== ResponseType.ToAllClients && /*AuctionRequestMode.Normal &&*/ auction.AuctionType == AuctionType.AutomaticNext && productToAuction.AuctionStatus is AuctionStatus.Sold or AuctionStatus.NotSold)
await StepNextProductToAuctionAsync(messageWrapper, customer, productToAuction, auctionProductStatusRequest);
return ResponseType.ToAllClients;
}
catch (Exception ex)
{
logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); Exception: {ex.Message}", ex, customer);
}
return ResponseType.Error;
}
private async Task SendMessageToClientsAsync(MessageWrapper messageWrapper, bool sendToCaller = false)
{
//if (sendToCaller) await hubContext.Clients.Caller.SendAsync("send", bidMessage.ToJson());
//else
await hubContext.Clients.All.SendAsync("send", messageWrapper.ToJson());
}
public async Task<ResponseType> HandleBidRequestAsync(Customer customer, MessageWrapper messageWrapper)
{
var bidRequestMessage = messageWrapper.Data.JsonTo<AuctionBidRequest>();
if (bidRequestMessage == null)
{
logger.Error($"SignalRMessageHandler.HandleBidRequestAsync(); (bidRequestMessage == null)", null, customer);
return ResponseType.Error;
}
try
{
await logger.InformationAsync($"SignalRMessageHandler.HandleBidRequestAsync(); Bid received; Auction: {bidRequestMessage.AuctionId}; ProductToAuction: {bidRequestMessage.ProductAuctionMappingId}; Product: {bidRequestMessage.ProductId}; Bid: {bidRequestMessage.BidPrice}; Customer: {bidRequestMessage.CustomerId}", null, customer);
var auction = await auctionService.GetAuctionDtoByIdAsync(bidRequestMessage.AuctionId, false, false);
if (auction == null || auction.Closed)
{
logger.Warning($"SignalRMessageHandler.HandleBidRequestAsync(); (auction == null || auction.Closed); Closed: {auction?.Closed}", null, customer);
return ResponseType.Error;
}
var product = await auctionService.GetProductById(bidRequestMessage.ProductId);
if (product == null) return ResponseType.Error;
var activeProductAuction = (await auctionService.GetProductToAuctionByAuctionIdAndProductIdAsync(bidRequestMessage.AuctionId, bidRequestMessage.ProductId, true)).FirstOrDefault();
if (activeProductAuction is not { IsActiveItem: true } || (activeProductAuction.WinnerCustomerId == customer.Id && !await customerService.IsAdminAsync(customer)))
{
logger.Warning($"SignalRMessageHandler.HandleBidRequestAsync(); (activeProductAuction is not {{ IsActiveItem: true }} || activeProductAuction.WinnerCustomerId == customer.Id && !await customerService.IsAdminAsync(customer)); AuctionStatus: {activeProductAuction?.AuctionStatus}; WinnerCustomerId: {activeProductAuction?.WinnerCustomerId}", null, customer);
await UpdateBidNotificationMessageWrapperAsync(messageWrapper, activeProductAuction, product, customer.Id, ResponseType.ToCaller);
return ResponseType.ToCaller;
}
if (!IsValidBidPrice(activeProductAuction.CurrentPrice, bidRequestMessage.BidPrice, activeProductAuction.BidsCount > 0, customer))
{
await UpdateBidNotificationMessageWrapperAsync(messageWrapper, activeProductAuction, product, customer.Id, ResponseType.ToCaller);
return ResponseType.ToCaller;
}
var auctionBid = bidRequestMessage.CreateMainEntity();
auctionBid.ProductAuctionMappingId = activeProductAuction.Id;
// activeProductAuction.AuctionStatus = AuctionStatus.Active;
// if (!await SetAuctionBidPrice(auctionBid.BidPrice, activeProductAuction, product, customer.Id)) return ResponseType.Error;
await auctionService.InsertBidAsync(auctionBid);
activeProductAuction.BidsCount++;
// await UpdateBidNotificationMessageWrapperAsync(messageWrapper, activeProductAuction, product, customer.Id, ResponseType.ToAllClients);
// return ResponseType.ToAllClients;
// }
// catch (Exception ex)
// {
// logger.Error($"SignalRMessageHandler.HandleBidRequestAsync(); Exception: {ex.Message}", ex, customer);
// }
activeProductAuction.AuctionStatus = AuctionStatus.Active;
if (!await SetAuctionBidPrice(auctionBid.BidPrice, activeProductAuction, product, customer.Id)) return ResponseType.Error;
await UpdateBidNotificationMessageWrapperAsync(messageWrapper, activeProductAuction, product, customer.Id, ResponseType.ToAllClients);
return ResponseType.ToAllClients;
}
catch (Exception ex)
{
logger.Error($"SignalRMessageHandler.HandleBidRequestAsync(); Exception: {ex.Message}", ex, customer);
}
// return ResponseType.Error;
//}
return ResponseType.Error;
}
//private async Task<bool> SetAuctionBidPrice(decimal bidPrice, ProductToAuctionMapping productToAuction, Product product, int lastBidCustomerId)
//{
// try
// {
// product.Price = bidPrice;
// await productService.UpdateProductAsync(product);
private async Task<bool> SetAuctionBidPrice(decimal bidPrice, ProductToAuctionMapping productToAuction, Product product, int lastBidCustomerId)
{
try
{
product.Price = bidPrice;
await productService.UpdateProductAsync(product);
// //activeProductAuction.StartingPrice = product.OldPrice; //TODO: ez biztosan kezelve van mikor összerendeljük? - J.
// productToAuction.CurrentPrice = bidPrice;
// productToAuction.WinnerCustomerId = lastBidCustomerId;
// await auctionService.UpdateProductToAuctionMappingAsync(productToAuction);
//activeProductAuction.StartingPrice = product.OldPrice; //TODO: ez biztosan kezelve van mikor összerendeljük? - J.
productToAuction.CurrentPrice = bidPrice;
productToAuction.WinnerCustomerId = lastBidCustomerId;
await auctionService.UpdateProductToAuctionMappingAsync(productToAuction);
// return true;
// }
// catch (Exception ex)
// {
// logger.Error($"SignalRMessageHandler.HandleBidRequest(); SetAuctionBidPrice() == false", ex);
// }
return true;
}
catch (Exception ex)
{
logger.Error($"SignalRMessageHandler.HandleBidRequest(); SetAuctionBidPrice() == false", ex);
}
// return false;
//}
return false;
}
//public async Task UpdateBidNotificationMessageWrapperAsync(MessageWrapper messageWrapper, ProductToAuctionMapping productToAuction, Product product, int customerId, ResponseType responseType)
//{
// var bidsCount = productToAuction.BidsCount; //await auctionService.GetBidsCountByProductToAuctionIdAsync(productToAuction.Id);
public async Task UpdateBidNotificationMessageWrapperAsync(MessageWrapper messageWrapper, ProductToAuctionMapping productToAuction, Product product, int customerId, ResponseType responseType)
{
var bidsCount = productToAuction.BidsCount; //await auctionService.GetBidsCountByProductToAuctionIdAsync(productToAuction.Id);
// var stepAmount = GetStepAmount(productToAuction.CurrentPrice);
// var nextBidPrice = GetNextBidPrice(productToAuction.CurrentPrice, stepAmount, bidsCount > 0);
var stepAmount = GetStepAmount(productToAuction.CurrentPrice);
var nextBidPrice = GetNextBidPrice(productToAuction.CurrentPrice, stepAmount, bidsCount > 0);
// messageWrapper.SenderId = customerId;
// messageWrapper.ResponseType = responseType;
// messageWrapper.MessageType = "bidNotification";
// messageWrapper.Data = new BidNotificationMessage(await auctionService.GetAuctionDtoByProductToAuctionIdAsync(productToAuction.Id, true), bidsCount, "EMPTY") //TODO: NE KÉRJÜK LE DB-BŐL!!! - J.
// {
// ProductName = product.Name,
// CurrentPrice = productToAuction.CurrentPrice,
// NextStepAmount = stepAmount,
// NextBidPrice = nextBidPrice,
// }.ToJson();
//}
messageWrapper.SenderId = customerId;
messageWrapper.ResponseType = responseType;
messageWrapper.MessageType = "bidNotification";
messageWrapper.Data = new BidNotificationMessage(await auctionService.GetAuctionDtoByProductToAuctionIdAsync(productToAuction.Id, true), bidsCount, "EMPTY") //TODO: NE KÉRJÜK LE DB-BŐL!!! - J.
{
ProductName = product.Name,
CurrentPrice = productToAuction.CurrentPrice,
NextStepAmount = stepAmount,
NextBidPrice = nextBidPrice,
}.ToJson();
}
//public async Task SendAuctionBidMessageAsync(MessageWrapper messageWrapper, ProductToAuctionMapping productToAuction, Product product, int customerId, ResponseType responseType)
//{
// await UpdateBidNotificationMessageWrapperAsync(messageWrapper, productToAuction, product, customerId, responseType);
// await SendMessageToClientsAsync(messageWrapper, responseType == ResponseType.ToCaller);
//}
public async Task SendAuctionBidMessageAsync(MessageWrapper messageWrapper, ProductToAuctionMapping productToAuction, Product product, int customerId, ResponseType responseType)
{
await UpdateBidNotificationMessageWrapperAsync(messageWrapper, productToAuction, product, customerId, responseType);
await SendMessageToClientsAsync(messageWrapper, responseType == ResponseType.ToCaller);
}
//private async Task StepNextProductToAuctionAsync(MessageWrapper messageWrapper, Customer customer, ProductToAuctionMapping productToAuction, AuctionProductStatusRequest auctionProductStatusRequest)
//{
// var nextProductToAuction = await auctionService.GetNextProductToAuctionByAuctionIdAsync(productToAuction.AuctionId);
// if (nextProductToAuction == null) return;
private async Task StepNextProductToAuctionAsync(MessageWrapper messageWrapper, Customer customer, ProductToAuctionMapping productToAuction, AuctionProductStatusRequest auctionProductStatusRequest)
{
var nextProductToAuction = await auctionService.GetNextProductToAuctionByAuctionIdAsync(productToAuction.AuctionId);
if (nextProductToAuction == null) return;
// await logger.InformationAsync($"SignalRMessageHandler.StepNextProductToAuctionAsync(); NextProductToAuctionId: {nextProductToAuction.Id};", null, customer);
await logger.InformationAsync($"SignalRMessageHandler.StepNextProductToAuctionAsync(); NextProductToAuctionId: {nextProductToAuction.Id};", null, customer);
// if (nextProductToAuction.AuctionStatus != AuctionStatus.None)
// {
// await logger.WarningAsync($"SignalRMessageHandler.StepNextProductToAuctionAsync(); (nextProductToAuction.AuctionStatus != AuctionStatus.None); NextProductToAuctionId: {nextProductToAuction.Id}; Status: {nextProductToAuction.AuctionStatus}({(int)nextProductToAuction.AuctionStatus})", null, customer);
// return; //TODO: Ilyenkor mi legyen?! - J.
// }
if (nextProductToAuction.AuctionStatus != AuctionStatus.None)
{
await logger.WarningAsync($"SignalRMessageHandler.StepNextProductToAuctionAsync(); (nextProductToAuction.AuctionStatus != AuctionStatus.None); NextProductToAuctionId: {nextProductToAuction.Id}; Status: {nextProductToAuction.AuctionStatus}({(int)nextProductToAuction.AuctionStatus})", null, customer);
return; //TODO: Ilyenkor mi legyen?! - J.
}
// auctionProductStatusRequest.AuctionStatus = AuctionStatus.Active;
// auctionProductStatusRequest.ProductToAuctionId = nextProductToAuction.Id;
auctionProductStatusRequest.AuctionStatus = AuctionStatus.Active;
auctionProductStatusRequest.ProductToAuctionId = nextProductToAuction.Id;
// await HandleProductToAuctionStatusChangedRequest(customer, messageWrapper);
//}
await HandleProductToAuctionStatusChangedRequest(customer, messageWrapper);
}
//private async Task ResetProductToAuction(ProductToAuctionMapping productToAuction, int? categoryId = null)
//{
// productToAuction.AuctionStatus = AuctionStatus.None;
// await auctionService.ResetProductToAuctionAsync(productToAuction, productToAuction.StartingPrice);
private async Task ResetProductToAuction(ProductToAuctionMapping productToAuction, int? categoryId = null)
{
productToAuction.AuctionStatus = AuctionStatus.None;
await auctionService.ResetProductToAuctionAsync(productToAuction, productToAuction.StartingPrice);
// categoryId ??= (await auctionService.GetAuctionByIdAsync(productToAuction.AuctionId))?.CategoryId;
// await UpdateProductCategoryIsFeaturedAsync(productToAuction.ProductId, categoryId, false);
//}
categoryId ??= (await auctionService.GetAuctionByIdAsync(productToAuction.AuctionId))?.CategoryId;
await UpdateProductCategoryIsFeaturedAsync(productToAuction.ProductId, categoryId, false);
}
//private async Task UpdateProductCategoryIsFeaturedAsync(int productId, int? categoryId, bool isFeatured)
//{
// //Leszarjuk ha elszáll, az aukció menjen tovább... - J.
// try
// {
// if (categoryId.GetValueOrDefault(0) == 0) return;
private async Task UpdateProductCategoryIsFeaturedAsync(int productId, int? categoryId, bool isFeatured)
{
//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;
var productCategory = (await categoryService.GetProductCategoriesByProductIdAsync(productId)).FirstOrDefault(x => x.CategoryId == categoryId);
if (productCategory == null) return;
// if (productCategory.IsFeaturedProduct == isFeatured) 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);
// }
//}
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 productToAuctionCurrentPrice, decimal bidRequestPrice, bool hasAuctionBidInDb, Customer customer = null)
//{
// var nextBidPrice = GetNextBidPrice(productToAuctionCurrentPrice, hasAuctionBidInDb);
// if (bidRequestPrice != nextBidPrice)
// {
// logger.Warning($"SignalRMessageHandler.IsValidBidPrice(); (bidRequestPrice != nextBidPrice); productToAuctionCurrentPrice: {productToAuctionCurrentPrice}; bidRequestPrice: {bidRequestPrice}; hasAuctionBidInDb: {hasAuctionBidInDb}", null, customer);
// return false;
// }
private bool IsValidBidPrice(decimal productToAuctionCurrentPrice, decimal bidRequestPrice, bool hasAuctionBidInDb, Customer customer = null)
{
var nextBidPrice = GetNextBidPrice(productToAuctionCurrentPrice, hasAuctionBidInDb);
if (bidRequestPrice != nextBidPrice)
{
logger.Warning($"SignalRMessageHandler.IsValidBidPrice(); (bidRequestPrice != nextBidPrice); productToAuctionCurrentPrice: {productToAuctionCurrentPrice}; bidRequestPrice: {bidRequestPrice}; hasAuctionBidInDb: {hasAuctionBidInDb}", null, customer);
return false;
}
// return true;
//}
return true;
}
//private static decimal GetStepAmount(decimal currentBidPrice) => AuctionService.GetStepAmount(currentBidPrice);
//private static decimal GetNextBidPrice(decimal currentBidPrice, bool hasAuctionBidInDb) => GetNextBidPrice(currentBidPrice, GetStepAmount(currentBidPrice), hasAuctionBidInDb);
//private static decimal GetNextBidPrice(decimal currentBidPrice, decimal stepAmount, bool hasAuctionBidInDb) => AuctionService.GetNextBidPrice(currentBidPrice, stepAmount, hasAuctionBidInDb);
private static decimal GetStepAmount(decimal currentBidPrice) => AuctionService.GetStepAmount(currentBidPrice);
private static decimal GetNextBidPrice(decimal currentBidPrice, bool hasAuctionBidInDb) => GetNextBidPrice(currentBidPrice, GetStepAmount(currentBidPrice), hasAuctionBidInDb);
private static decimal GetNextBidPrice(decimal currentBidPrice, decimal stepAmount, bool hasAuctionBidInDb) => AuctionService.GetNextBidPrice(currentBidPrice, stepAmount, hasAuctionBidInDb);
private static bool IsValidRequestAuctionStatus(AuctionStatus newStatus, AuctionStatus oldStatus)
{
switch (oldStatus)
{
case AuctionStatus.None:
return newStatus is AuctionStatus.Active or AuctionStatus.Pause;
case AuctionStatus.Active:
return newStatus is AuctionStatus.FirstWarning or AuctionStatus.Pause;
case AuctionStatus.FirstWarning:
return newStatus is AuctionStatus.SecondWarning or AuctionStatus.Pause;
case AuctionStatus.SecondWarning:
return newStatus is AuctionStatus.Sold or AuctionStatus.Pause;
case AuctionStatus.Pause:
return newStatus is AuctionStatus.None or AuctionStatus.Active;
case AuctionStatus.Sold:
case AuctionStatus.NotSold:
default:
return false;
}
}
//private static bool IsValidRequestAuctionStatus(AuctionStatus newStatus, AuctionStatus oldStatus)
//{
// switch (oldStatus)
// {
// case AuctionStatus.None:
// return newStatus is AuctionStatus.Active or AuctionStatus.Pause;
// case AuctionStatus.Active:
// return newStatus is AuctionStatus.FirstWarning or AuctionStatus.Pause;
// case AuctionStatus.FirstWarning:
// return newStatus is AuctionStatus.SecondWarning or AuctionStatus.Pause;
// case AuctionStatus.SecondWarning:
// return newStatus is AuctionStatus.Sold or AuctionStatus.Pause;
// case AuctionStatus.Pause:
// return newStatus is AuctionStatus.None or AuctionStatus.Active;
// case AuctionStatus.Sold:
// case AuctionStatus.NotSold:
// default:
// return false;
// }
//}
}
}

View File

@ -46,7 +46,8 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Infrastructure
services.AddSingleton<SessionService>();
services.AddSignalR(hubOptions =>
{
hubOptions.KeepAliveInterval = TimeSpan.FromMinutes(1);
hubOptions.ClientTimeoutInterval = TimeSpan.FromMinutes(20);
hubOptions.KeepAliveInterval = TimeSpan.FromMinutes(10);
});
//register services and interfaces
@ -70,7 +71,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Infrastructure
services.AddScoped<AuctionDbContext>();
services.AddScoped<MyProductModelFactory>();
services.AddScoped<SignalRMessageHandler>();
//services.AddScoped<SignalRMessageHandler>();
services.AddScoped<IOrderService, OrderService>();
services.AddScoped<IOrderProcessingService, OrderProcessingService>();
services.AddScoped<IShoppingCartService, ShoppingCartService>();