improvements, fixes, etc...
This commit is contained in:
parent
59ae22f5cd
commit
cb83fbe077
|
|
@ -118,6 +118,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Areas.Admin.Controllers
|
||||||
MessageType = "bidNotification",
|
MessageType = "bidNotification",
|
||||||
Data = new BidNotificationMessage
|
Data = new BidNotificationMessage
|
||||||
{
|
{
|
||||||
|
//AuctionDto = TODO: ??? - J.
|
||||||
ProductName = viewModel.ProductName,
|
ProductName = viewModel.ProductName,
|
||||||
BidPrice = viewModel.BidPrice.ToString(),
|
BidPrice = viewModel.BidPrice.ToString(),
|
||||||
NextStepAmount = viewModel.NextStepAmount.ToString()
|
NextStepAmount = viewModel.NextStepAmount.ToString()
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
using Nop.Plugin.Misc.AuctionPlugin.Domains.Dtos;
|
||||||
|
|
||||||
|
namespace Nop.Plugin.Misc.AuctionPlugin.Hubs.Messages;
|
||||||
|
|
||||||
|
public abstract class AuctionNotificationBase
|
||||||
|
{
|
||||||
|
public AuctionDto AuctionDto { get; set; }
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
namespace Nop.Plugin.Misc.AuctionPlugin.Hubs.Messages;
|
||||||
|
|
||||||
|
public class AuctionProductStatusNotification : AuctionNotificationBase
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
namespace Nop.Plugin.Misc.AuctionPlugin.Hubs.Messages;
|
||||||
|
|
||||||
|
public class AuctionStatusNotification : AuctionNotificationBase
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Nop.Plugin.Misc.AuctionPlugin.Domains.Dtos;
|
||||||
|
|
||||||
namespace Nop.Plugin.Misc.AuctionPlugin.Hubs.Messages
|
namespace Nop.Plugin.Misc.AuctionPlugin.Hubs.Messages
|
||||||
{
|
{
|
||||||
|
|
@ -11,10 +12,18 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs.Messages
|
||||||
/// this message sends update to the clients. so it sends current (the already winner) price,
|
/// this message sends update to the clients. so it sends current (the already winner) price,
|
||||||
/// sends the ACTUAL bidstep, so the new price, and next bid can be calculated on the client side
|
/// sends the ACTUAL bidstep, so the new price, and next bid can be calculated on the client side
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class BidNotificationMessage
|
public class BidNotificationMessage : AuctionNotificationBase
|
||||||
{
|
{
|
||||||
public string ProductName { get; set; }
|
public string ProductName { get; set; }
|
||||||
public string BidPrice { get; set; }
|
public string BidPrice { get; set; }
|
||||||
public string NextStepAmount { get; set; }
|
public string NextStepAmount { get; set; }
|
||||||
|
|
||||||
|
public BidNotificationMessage() { }
|
||||||
|
|
||||||
|
public BidNotificationMessage(AuctionDto auctionDto)
|
||||||
|
{
|
||||||
|
AuctionDto = auctionDto;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,8 @@ 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;
|
using Nop.Core;
|
||||||
|
using Nop.Plugin.Misc.AuctionPlugin.Domains.Enums;
|
||||||
|
using Nop.Services.Customers;
|
||||||
|
|
||||||
namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
|
namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
|
||||||
{
|
{
|
||||||
|
|
@ -46,6 +48,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
|
||||||
// return;
|
// return;
|
||||||
//}
|
//}
|
||||||
|
|
||||||
|
//TODO: lock-olni! - J.
|
||||||
switch (message.MessageType)
|
switch (message.MessageType)
|
||||||
{
|
{
|
||||||
case "BidRequestMessage":
|
case "BidRequestMessage":
|
||||||
|
|
@ -69,11 +72,20 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
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)
|
//CustomerService a = new CustomerService()a.IsGuestAsync()
|
||||||
|
var customer = await _workContext.GetCurrentCustomerAsync();
|
||||||
|
if (customer == null || bidRequestMessage.CustomerId != customer.Id) //|| !customer.Active) //TODO: ??? - J.
|
||||||
{
|
{
|
||||||
_logger.Error($"SignalRMessageHandler.HandleBidRequest(); bidRequestMessage.CustomerId != (await _workContext.GetCurrentCustomerAsync()).Id");
|
_logger.Error($"SignalRMessageHandler.HandleBidRequest(); customer == null || bidRequestMessage.CustomerId != customer.Id");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var auction = await _auctionService.GetAuctionDtoByIdAsync(bidRequestMessage.AuctionId);
|
||||||
|
if (auction.Closed)
|
||||||
|
{
|
||||||
|
_logger.Warning($"SignalRMessageHandler.HandleBidRequest(); auction.Closed");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -84,15 +96,15 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
|
||||||
return; //ha nincs product vagy exception van, akkor ne broadcast-eljük ki az invalid Bid-et! - J.
|
return; //ha nincs product vagy exception van, akkor ne broadcast-eljük ki az invalid Bid-et! - J.
|
||||||
}
|
}
|
||||||
|
|
||||||
var mapping = await _auctionService.GetProductToAuctionByAuctionIdAndProductIdAsync(bidRequestMessage.AuctionId, bidRequestMessage.ProductId);
|
var activeProductAuction = (await _auctionService.GetProductToAuctionByAuctionIdAndProductIdAsync(bidRequestMessage.AuctionId, bidRequestMessage.ProductId, true)).FirstOrDefault();
|
||||||
if (mapping == null || mapping.Count == 0)
|
if (activeProductAuction == null) //|| productAuction.WinnerCustomerId == customer.Id)
|
||||||
{
|
{
|
||||||
_logger.Error($"SignalRMessageHandler.HandleBidRequest(); mapping == null || mapping.Count == 0");
|
_logger.Warning($"SignalRMessageHandler.HandleBidRequest(); activeProductAuction == null");
|
||||||
return; //ha nincs ProductToAuction, akkor ne broadcast-eljük ki az invalid Bid-et! - J.
|
return; //TODO: - J.
|
||||||
}
|
}
|
||||||
|
|
||||||
var auctionBid = bidRequestMessage.CreateMainEntity();
|
var auctionBid = bidRequestMessage.CreateMainEntity();
|
||||||
auctionBid.ProductAuctionMappingId = mapping.FirstOrDefault()!.Id;
|
auctionBid.ProductAuctionMappingId = activeProductAuction.Id;
|
||||||
|
|
||||||
//TODO: validate the bidprice amount
|
//TODO: validate the bidprice amount
|
||||||
if (product.Price >= auctionBid.BidPrice)
|
if (product.Price >= auctionBid.BidPrice)
|
||||||
|
|
@ -108,11 +120,15 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
|
||||||
product.Price = bidRequestMessage.BidPrice;
|
product.Price = bidRequestMessage.BidPrice;
|
||||||
await _productService.UpdateProductAsync(product);
|
await _productService.UpdateProductAsync(product);
|
||||||
|
|
||||||
|
activeProductAuction.StartingPrice = product.OldPrice;
|
||||||
|
activeProductAuction.BidPrice = product.Price;
|
||||||
|
await _auctionService.UpdateProductToAuctionMappingAsync(activeProductAuction);
|
||||||
|
|
||||||
// Optionally broadcast to all clients
|
// Optionally broadcast to all clients
|
||||||
var bid = new MessageWrapper
|
var bid = new MessageWrapper
|
||||||
{
|
{
|
||||||
MessageType = "bidNotification",
|
MessageType = "bidNotification",
|
||||||
Data = new BidNotificationMessage
|
Data = new BidNotificationMessage(await _auctionService.GetAuctionDtoWithProductByIdAsync(auction.Id, activeProductAuction.ProductId, true))
|
||||||
{
|
{
|
||||||
ProductName = auctionBid.ProductId.ToString(),
|
ProductName = auctionBid.ProductId.ToString(),
|
||||||
BidPrice = auctionBid.BidPrice.ToString(CultureInfo.InvariantCulture),
|
BidPrice = auctionBid.BidPrice.ToString(CultureInfo.InvariantCulture),
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ using Nop.Plugin.Misc.AuctionPlugin.Domains;
|
||||||
using Nop.Plugin.Misc.AuctionPlugin.Domains.DataLayer;
|
using Nop.Plugin.Misc.AuctionPlugin.Domains.DataLayer;
|
||||||
using Nop.Plugin.Misc.AuctionPlugin.Domains.Dtos;
|
using Nop.Plugin.Misc.AuctionPlugin.Domains.Dtos;
|
||||||
using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities;
|
using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities;
|
||||||
|
using Nop.Plugin.Misc.AuctionPlugin.Domains.Enums;
|
||||||
using Nop.Services.Logging;
|
using Nop.Services.Logging;
|
||||||
|
|
||||||
namespace Nop.Plugin.Misc.AuctionPlugin.Services;
|
namespace Nop.Plugin.Misc.AuctionPlugin.Services;
|
||||||
|
|
@ -146,6 +147,12 @@ public class AuctionService : IAuctionService
|
||||||
await _staticCacheManager.RemoveByPrefixAsync(AUCTION_PATTERN_KEY);
|
await _staticCacheManager.RemoveByPrefixAsync(AUCTION_PATTERN_KEY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public virtual async Task UpdateAuctionAsync(Auction auction)
|
||||||
|
{
|
||||||
|
await _ctx.Auctions.UpdateAsync(auction);
|
||||||
|
await _staticCacheManager.RemoveByPrefixAsync(AUCTION_PATTERN_KEY);
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<IList<Auction>> GetAllAuctionsAsync()
|
public async Task<IList<Auction>> GetAllAuctionsAsync()
|
||||||
{
|
{
|
||||||
return await _ctx.Auctions.GetAllAuctionsAsync();
|
return await _ctx.Auctions.GetAllAuctionsAsync();
|
||||||
|
|
@ -166,13 +173,13 @@ public class AuctionService : IAuctionService
|
||||||
return auction == null ? null : new AuctionDto(auction);
|
return auction == null ? null : new AuctionDto(auction);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<AuctionDto> GetAuctionDtoWithProductByIdAsync(int auctionId, int productId)
|
public async Task<AuctionDto> GetAuctionDtoWithProductByIdAsync(int auctionId, int productId, bool activeProductOnly = false)
|
||||||
{
|
{
|
||||||
var auction = await _ctx.Auctions.GetByIdAsync(auctionId);
|
var auction = await _ctx.Auctions.GetByIdAsync(auctionId);
|
||||||
if (auction == null) return null;
|
if (auction == null) return null;
|
||||||
|
|
||||||
var auctionDto = new AuctionDto(auction);
|
var auctionDto = new AuctionDto(auction);
|
||||||
auctionDto.ProductToAuctionDtos.AddRange(await _ctx.ProductToAuctions.GetByAuctionAndProductId(auctionId, productId).Select(x => new ProductToAuctionDto(x)).ToListAsync());
|
auctionDto.ProductToAuctionDtos.AddRange((await GetProductToAuctionByAuctionIdAndProductIdAsync(auctionId, productId, activeProductOnly)).Select(x => new ProductToAuctionDto(x)));
|
||||||
|
|
||||||
return auctionDto;
|
return auctionDto;
|
||||||
}
|
}
|
||||||
|
|
@ -206,9 +213,11 @@ public class AuctionService : IAuctionService
|
||||||
return new List<ProductToAuctionMapping>(await _ctx.ProductToAuctions.GetByProductId(productId).ToListAsync());
|
return new List<ProductToAuctionMapping>(await _ctx.ProductToAuctions.GetByProductId(productId).ToListAsync());
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<ProductToAuctionMapping>> GetProductToAuctionByAuctionIdAndProductIdAsync(int auctionId, int productId)
|
public async Task<List<ProductToAuctionMapping>> GetProductToAuctionByAuctionIdAndProductIdAsync(int auctionId, int productId, bool activeOnly = false)
|
||||||
{
|
{
|
||||||
return new List<ProductToAuctionMapping>(await _ctx.ProductToAuctions.GetByAuctionAndProductId(auctionId, productId).ToListAsync());
|
return new List<ProductToAuctionMapping>(await _ctx.ProductToAuctions.GetByAuctionAndProductId(auctionId, productId)
|
||||||
|
.Where(x => !activeOnly || (x.AuctionStatus == AuctionStatus.Active || x.AuctionStatus == AuctionStatus.FirstWarning || x.AuctionStatus == AuctionStatus.SecondWarning))
|
||||||
|
.ToListAsync());
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<ProductToAuctionMapping> AssignProductToAuctionAsync(int productId, decimal startingPrice, decimal bidPrice, int auctionId)
|
public async Task<ProductToAuctionMapping> AssignProductToAuctionAsync(int productId, decimal startingPrice, decimal bidPrice, int auctionId)
|
||||||
|
|
@ -242,5 +251,11 @@ public class AuctionService : IAuctionService
|
||||||
return mapping;
|
return mapping;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public virtual async Task UpdateProductToAuctionMappingAsync(ProductToAuctionMapping productToAuctionMapping)
|
||||||
|
{
|
||||||
|
await _ctx.ProductToAuctions.UpdateAsync(productToAuctionMapping);
|
||||||
|
await _staticCacheManager.RemoveByPrefixAsync(AUCTION_PATTERN_KEY);
|
||||||
|
}
|
||||||
|
|
||||||
#endregion Dtos
|
#endregion Dtos
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue