diff --git a/Nop.Plugin.Misc.AuctionPlugin/Controllers/AuctionController.cs b/Nop.Plugin.Misc.AuctionPlugin/Controllers/AuctionController.cs index bfc1739..3898509 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Controllers/AuctionController.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Controllers/AuctionController.cs @@ -76,8 +76,7 @@ public class AuctionController : BasePluginController public async Task 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) diff --git a/Nop.Plugin.Misc.AuctionPlugin/Domains/DataLayer/ProductToAuctionDbTable.cs b/Nop.Plugin.Misc.AuctionPlugin/Domains/DataLayer/ProductToAuctionDbTable.cs index c652770..a35987b 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Domains/DataLayer/ProductToAuctionDbTable.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Domains/DataLayer/ProductToAuctionDbTable.cs @@ -60,6 +60,8 @@ public class ProductToAuctionDbTable : MgDbTableBase productToAuction.StartingPrice = basePrice; productToAuction.CurrentPrice = basePrice; productToAuction.WinnerCustomerId = 0; + productToAuction.OrderGuid = null; + productToAuction.OrderId = 0; productToAuction.AuctionStatus = AuctionStatus.None; await UpdateAsync(productToAuction); diff --git a/Nop.Plugin.Misc.AuctionPlugin/Domains/Entities/Interfaces/IProductToAuctionMapping.cs b/Nop.Plugin.Misc.AuctionPlugin/Domains/Entities/Interfaces/IProductToAuctionMapping.cs index 0d85a94..5999838 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Domains/Entities/Interfaces/IProductToAuctionMapping.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Domains/Entities/Interfaces/IProductToAuctionMapping.cs @@ -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; } } \ No newline at end of file diff --git a/Nop.Plugin.Misc.AuctionPlugin/Domains/Entities/ProductToAuctionMapping.cs b/Nop.Plugin.Misc.AuctionPlugin/Domains/Entities/ProductToAuctionMapping.cs index e7f9b30..b08fa12 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Domains/Entities/ProductToAuctionMapping.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Domains/Entities/ProductToAuctionMapping.cs @@ -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] diff --git a/Nop.Plugin.Misc.AuctionPlugin/Domains/EventConsumers/AuctionEventConsumer.cs b/Nop.Plugin.Misc.AuctionPlugin/Domains/EventConsumers/AuctionEventConsumer.cs index 90083a6..2c857c2 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Domains/EventConsumers/AuctionEventConsumer.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Domains/EventConsumers/AuctionEventConsumer.cs @@ -15,7 +15,7 @@ public class AuctionEventConsumer(IHttpContextAccessor httpContextAccessor, ILog { await Logger.InformationAsync($"AuctionEventConsumer.HandleEventAsync();"); - 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) { diff --git a/Nop.Plugin.Misc.AuctionPlugin/Hubs/SignalRMessageHandler.cs b/Nop.Plugin.Misc.AuctionPlugin/Hubs/SignalRMessageHandler.cs index 1835714..9144612 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Hubs/SignalRMessageHandler.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Hubs/SignalRMessageHandler.cs @@ -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 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 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); diff --git a/Nop.Plugin.Misc.AuctionPlugin/Mapping/Builders/AuctionBuilder.cs b/Nop.Plugin.Misc.AuctionPlugin/Mapping/Builders/AuctionBuilder.cs index 4d18e62..8a34bf8 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Mapping/Builders/AuctionBuilder.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Mapping/Builders/AuctionBuilder.cs @@ -25,7 +25,7 @@ public class AuctionBuilder : NopEntityBuilder //.WithColumn(nameof(Auction.AuctionId)).AsInt32().ForeignKey(onDelete: Rule.Cascade) //Rule.Cascade?? //.WithColumn(nameof(Auction.ProductId)).AsInt32().ForeignKey(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() diff --git a/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs b/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs index 17cdcc5..05cff68 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs @@ -94,14 +94,6 @@ public class AuctionService : IAuctionService //200 000 000 Ft fölött 20 000 000 Ft-tal } - private async Task 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 GetAuctionDtoWithAuctionBids(int auctionId, bool activeProductOnly) + public async Task 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; diff --git a/Nop.Plugin.Misc.AuctionPlugin/Services/IAuctionService.cs b/Nop.Plugin.Misc.AuctionPlugin/Services/IAuctionService.cs index 7667034..01cd477 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Services/IAuctionService.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Services/IAuctionService.cs @@ -41,7 +41,7 @@ public interface IAuctionService Task> GetAllAuctionsAsync(); - Task GetAuctionDtoWithAuctionBids(int auctionId, bool activeProductOnly); + Task GetAuctionDtoWithAuctionBids(int auctionId, bool activeProductOnly, int maxBidsCount); Task> GetProductToAuctionsByAuctionIdAsync(int auctionId, bool onlyActiveItems); Task GetAuctionDtoByIdAsync(int auctionId, bool widthProducts, bool activeProductOnly);