OrderId, OrderGuid add to ProductToAuctionMappibg; improvements, fixes, etc..

This commit is contained in:
Loretta 2024-11-26 18:58:23 +01:00
parent 12d207e208
commit 85c857de99
9 changed files with 63 additions and 56 deletions

View File

@ -76,8 +76,7 @@ public class AuctionController : BasePluginController
public async Task<IActionResult> LiveScreen(int auctionId)
{
var auctionDto = await _auctionService.GetAuctionDtoWithAuctionBids(auctionId, true);
var auctionDto = await _auctionService.GetAuctionDtoWithAuctionBids(auctionId, true, 5);
var activeMapping = auctionDto?.ProductToAuctionDtos.MinBy(x => x.SortIndex);
var isAnyItemLive = activeMapping != null;
if (auctionDto == null)

View File

@ -60,6 +60,8 @@ public class ProductToAuctionDbTable : MgDbTableBase<ProductToAuctionMapping>
productToAuction.StartingPrice = basePrice;
productToAuction.CurrentPrice = basePrice;
productToAuction.WinnerCustomerId = 0;
productToAuction.OrderGuid = null;
productToAuction.OrderId = 0;
productToAuction.AuctionStatus = AuctionStatus.None;
await UpdateAsync(productToAuction);

View File

@ -5,4 +5,6 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Domains.Entities.Interfaces;
public interface IProductToAuctionMapping : IProductToAuctionDtoBase, ITimeStampInfo //, ISoftRemoveEntityInt
{
public int OrderId { get; set; }
public Guid? OrderGuid { get; set; }
}

View File

@ -13,6 +13,9 @@ public partial class ProductToAuctionMapping : MgEntityBase, IProductToAuctionMa
{
public int ProductId { get; set; }
public int AuctionId { get; set; }
public int OrderId { get; set; }
public Guid? OrderGuid { get; set; }
public int WinnerCustomerId { get; set; }
[NotMapped]

View File

@ -15,7 +15,7 @@ public class AuctionEventConsumer(IHttpContextAccessor httpContextAccessor, ILog
{
await Logger.InformationAsync($"AuctionEventConsumer.HandleEventAsync<Product>();");
var productToAuctions = await ctx.ProductToAuctions.GetByProductId(eventMessage.Entity.Id).Where(x => x.AuctionStatus == AuctionStatus.None).ToListAsync();
var productToAuctions = await ctx.ProductToAuctions.GetByProductId(eventMessage.Entity.Id).Where(x => x.AuctionStatus == AuctionStatus.None && x.OrderGuid == null).ToListAsync();
foreach (var productToAuction in productToAuctions)
{

View File

@ -45,7 +45,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
return;
}
await logger.InformationAsync($"SignalRMessageHandler.HandleMessage(); jsonData: {message.Data}", null, customer);
await logger.InformationAsync($"SignalRMessageHandler.HandleMessage(); MessageType: {message.MessageType}; jsonData: {message.Data}", null, customer);
if (customer == null || message.SenderId <= 0 || message.SenderId != customer.Id || await customerService.IsGuestAsync(customer))
{
@ -104,9 +104,11 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
if (productToAuction is not { AuctionStatus: AuctionStatus.Pause })
{
logger.Warning($"SignalRMessageHandler.HandleRevertAuctionBidRequest(); (productToAuction is not {{ AuctionStatus: AuctionStatus.Pause }}); AuctionStatus: {productToAuction?.AuctionStatus}", null, customer);
return; //TODO: - J.
return;
}
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;
@ -176,12 +178,9 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
productToAuction.CurrentPrice = lastAuctionBid.BidPrice;
productToAuction.WinnerCustomerId = lastAuctionBid.CustomerId;
//productToAuction.OrderGuid = Guid.NewGuid(); //TODO: - J.
var placeOrderResult = await auctionService.CreateOrderForWinnerAsync(productToAuction);
if (placeOrderResult is not { Success: true }) return;
//productToAuction.OrderId = placedOrder.PlacedOrder.Id; //TODO: - J.
break;
case AuctionStatus.NotSold:
@ -215,12 +214,6 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
}
}
private Task ResetProductToAuction(ProductToAuctionMapping productToAuction)
{
productToAuction.AuctionStatus = AuctionStatus.None;
return auctionService.ResetProductToAuctionAsync(productToAuction, productToAuction.StartingPrice);
}
private async Task HandleBidRequest(Customer customer, AuctionBidRequest bidRequestMessage)
{
if (bidRequestMessage == null)
@ -244,7 +237,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
if (activeProductAuction is not { IsActiveItem: true } || (activeProductAuction.WinnerCustomerId == customer.Id && !await customerService.IsAdminAsync(customer)))
{
logger.Warning($"SignalRMessageHandler.HandleBidRequest(); (activeProductAuction is not {{ IsActiveItem: true }} || activeProductAuction.WinnerCustomerId == customer.Id && !await customerService.IsAdminAsync(customer)); AuctionStatus: {activeProductAuction?.AuctionStatus}; WinnerCustomerId: {activeProductAuction?.WinnerCustomerId}", null, customer);
return; //TODO: - J.
return;
}
var product = await auctionService.GetProductById(bidRequestMessage.ProductId);
@ -268,15 +261,26 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
}
}
private bool IsValidBidPrice(decimal productPrice, decimal productToAuctionCurrentPrice, decimal bidRequestPrice, Customer customer = null)
private async Task<bool> SetAuctionBidPrice(decimal bidPrice, ProductToAuctionMapping productToAuction, Product product, int lastBidCustomerId)
{
if (productPrice >= bidRequestPrice || productToAuctionCurrentPrice >= bidRequestPrice || bidRequestPrice != GetNextBidPrice(productToAuctionCurrentPrice))
{
logger.Warning($"SignalRMessageHandler.IsValidBidPrice(); (productPrice >= bidRequestPrice || productToAuctionCurrentPrice >= bidRequestPrice || bidRequestPrice != GetNextBidPrice(productToAuctionCurrentPrice)); productPrice: {productPrice}; productToAuctionCurrentPrice: {productToAuctionCurrentPrice}; bidRequestPrice: {bidRequestPrice}", null, customer);
return true;
}
try
{
product.Price = bidPrice;
await productService.UpdateProductAsync(product);
return false;
//activeProductAuction.StartingPrice = product.OldPrice; //TODO: ez biztosan kezelve van mikor összerendeljük? - J.
productToAuction.CurrentPrice = product.Price;
productToAuction.WinnerCustomerId = lastBidCustomerId;
await auctionService.UpdateProductToAuctionMappingAsync(productToAuction);
return true;
}
catch (Exception ex)
{
logger.Error($"SignalRMessageHandler.HandleBidRequest(); SetAuctionBidPrice() == false", ex);
}
return false;
}
private async Task SendAuctionBidMessageAsync(ProductToAuctionMapping productToAuction, Product product, int customerId)
@ -289,7 +293,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
{
MessageType = "bidNotification",
SenderId = customerId,
Data = new BidNotificationMessage(await auctionService.GetAuctionDtoByProductToAuctionIdAsync(productToAuction.Id, true), bidsCount, "EMPTY") //TODO: NE KÉRJÜK LE DB-BŐL!!! - j.
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,
@ -301,26 +305,21 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
await hubContext.Clients.All.SendAsync("send", bidMessage.ToJson());
}
private async Task<bool> SetAuctionBidPrice(decimal bidPrice, ProductToAuctionMapping productToAuction, Product product, int lastBidCustomerId)
private Task ResetProductToAuction(ProductToAuctionMapping productToAuction)
{
try
{
product.Price = bidPrice;
await productService.UpdateProductAsync(product);
productToAuction.AuctionStatus = AuctionStatus.None;
return auctionService.ResetProductToAuctionAsync(productToAuction, productToAuction.StartingPrice);
}
//activeProductAuction.StartingPrice = product.OldPrice; //TODO: ez biztosan kezelve van mikor összerendeljük? - J.
productToAuction.CurrentPrice = product.Price;
productToAuction.WinnerCustomerId = lastBidCustomerId;
await auctionService.UpdateProductToAuctionMappingAsync(productToAuction);
private bool IsValidBidPrice(decimal productPrice, decimal productToAuctionCurrentPrice, decimal bidRequestPrice, Customer customer = null)
{
if (productPrice >= bidRequestPrice || productToAuctionCurrentPrice >= bidRequestPrice || bidRequestPrice != GetNextBidPrice(productToAuctionCurrentPrice))
{
logger.Warning($"SignalRMessageHandler.IsValidBidPrice(); (productPrice >= bidRequestPrice || productToAuctionCurrentPrice >= bidRequestPrice || bidRequestPrice != GetNextBidPrice(productToAuctionCurrentPrice)); productPrice: {productPrice}; productToAuctionCurrentPrice: {productToAuctionCurrentPrice}; bidRequestPrice: {bidRequestPrice}", null, customer);
return true;
}
return true;
}
catch (Exception ex)
{
logger.Error($"SignalRMessageHandler.HandleBidRequest(); SetAuctionBidPrice() == false", ex);
}
return false;
return false;
}
private static decimal GetStepAmount(decimal currentBidPrice) => AuctionService.GetStepAmount(currentBidPrice);

View File

@ -25,7 +25,7 @@ public class AuctionBuilder : NopEntityBuilder<Auction>
//.WithColumn(nameof(Auction.AuctionId)).AsInt32().ForeignKey<Auction>(onDelete: Rule.Cascade) //Rule.Cascade??
//.WithColumn(nameof(Auction.ProductId)).AsInt32().ForeignKey<Product>(onDelete: Rule.Cascade) //Rule.Cascade??
.WithColumn(nameof(Auction.AuctionType)).AsByte().NotNullable() //enum??? - J.
.WithColumn(nameof(Auction.AuctionType)).AsByte().NotNullable()
//.WithColumn(nameof(Auction.StartingPrice)).AsInt32().NotNullable()
//.WithColumn(nameof(Auction.BidPrice)).AsInt32().NotNullable()

View File

@ -94,14 +94,6 @@ public class AuctionService : IAuctionService
//200 000 000 Ft fölött 20 000 000 Ft-tal
}
private async Task<bool> ValidateAuctionBid(AuctionBid auctionBid)
{
//auctionBid.CustomerId = (await _workContext.GetCurrentCustomerAsync()).Id; //elvileg megnézi cache-ben, csak utána DB-zik! - J.
//etc...
return true;
}
public async Task ResetProductToAuctionByProductId(int productId)
=> await ResetProductToAuctionAsync(await _ctx.ProductToAuctions.GetByProductId(productId).FirstOrDefaultAsync());
@ -164,7 +156,7 @@ public class AuctionService : IAuctionService
public async Task InsertBidAsync(AuctionBid auctionBid)
{
if (await ValidateAuctionBid(auctionBid))
//if (await ValidateAuctionBid(auctionBid))
{
await _ctx.AuctionBids.InsertAsync(auctionBid, false);
}
@ -172,7 +164,7 @@ public class AuctionService : IAuctionService
public async Task UpdateBidAsync(AuctionBid auctionBid)
{
if (await ValidateAuctionBid(auctionBid))
//if (await ValidateAuctionBid(auctionBid))
{
await _ctx.AuctionBids.UpdateAsync(auctionBid, false);
}
@ -207,9 +199,19 @@ public class AuctionService : IAuctionService
processPaymentRequest.OrderGuid = Guid.NewGuid();
var placeOrderResult = await _orderProcessingService.PlaceOrderAsync(processPaymentRequest);
//placeOrderResult.PlacedOrder //TODO:... - J.
if (placeOrderResult.Success)
{
//placeOrderResult.PlacedOrder //TODO:... - J.
auctionItem.OrderId = placeOrderResult.PlacedOrder.Id;
auctionItem.OrderGuid = placeOrderResult.PlacedOrder.OrderGuid;
return placeOrderResult;
}
return placeOrderResult.Success ? placeOrderResult : null;
return null;
}
catch (Exception ex)
{
@ -244,14 +246,14 @@ public class AuctionService : IAuctionService
#region Dtos
public async Task<AuctionDto> GetAuctionDtoWithAuctionBids(int auctionId, bool activeProductOnly)
public async Task<AuctionDto> GetAuctionDtoWithAuctionBids(int auctionId, bool activeProductOnly, int maxBidsCount = int.MaxValue)
{
var auctionDto = await GetAuctionDtoByIdAsync(auctionId, true, activeProductOnly);
if (auctionDto == null) return null;
foreach (var auctionDtoProductToAuctionDto in auctionDto.ProductToAuctionDtos)
{
auctionDtoProductToAuctionDto.AuctionBidDtos.AddRange(await _ctx.AuctionBids.GetByProductToAuctionId(auctionDtoProductToAuctionDto.Id).OrderByDescending(x => x.Id).Select(x => new AuctionBidDto(x)).ToListAsync());
auctionDtoProductToAuctionDto.AuctionBidDtos.AddRange(await _ctx.AuctionBids.GetByProductToAuctionId(auctionDtoProductToAuctionDto.Id).OrderByDescending(x => x.Id).Take(maxBidsCount).Select(x => new AuctionBidDto(x)).ToListAsync());
}
return auctionDto;

View File

@ -41,7 +41,7 @@ public interface IAuctionService
Task<IList<Auction>> GetAllAuctionsAsync();
Task<AuctionDto> GetAuctionDtoWithAuctionBids(int auctionId, bool activeProductOnly);
Task<AuctionDto> GetAuctionDtoWithAuctionBids(int auctionId, bool activeProductOnly, int maxBidsCount);
Task<List<ProductToAuctionMapping>> GetProductToAuctionsByAuctionIdAsync(int auctionId, bool onlyActiveItems);
Task<AuctionDto> GetAuctionDtoByIdAsync(int auctionId, bool widthProducts, bool activeProductOnly);