From f8bc35cf054d9348aad805e505e960bc6d97f7f0 Mon Sep 17 00:00:00 2001 From: Loretta Date: Tue, 26 Nov 2024 09:10:58 +0100 Subject: [PATCH] imrpovements, fixes --- .../Content/Js/LiveAnnouncement.js | 3 +- .../Domains/DataLayer/AuctionDbContext.cs | 14 +++++- .../Hubs/SignalRMessageHandler.cs | 24 +++++----- .../Services/AuctionService.cs | 39 +-------------- .../Views/PublicProductBidBox.cshtml | 47 +++++++++++++++++-- 5 files changed, 73 insertions(+), 54 deletions(-) diff --git a/Nop.Plugin.Misc.AuctionPlugin/Content/Js/LiveAnnouncement.js b/Nop.Plugin.Misc.AuctionPlugin/Content/Js/LiveAnnouncement.js index 05f42a8..83b3d15 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Content/Js/LiveAnnouncement.js +++ b/Nop.Plugin.Misc.AuctionPlugin/Content/Js/LiveAnnouncement.js @@ -9,7 +9,8 @@ SecondWarning: 4, Pause: 8, Sold: 16, - NotSold: 32 + NotSold: 32, + TEST: 64 }); // HUF Formatter diff --git a/Nop.Plugin.Misc.AuctionPlugin/Domains/DataLayer/AuctionDbContext.cs b/Nop.Plugin.Misc.AuctionPlugin/Domains/DataLayer/AuctionDbContext.cs index f70e988..540e1e3 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Domains/DataLayer/AuctionDbContext.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Domains/DataLayer/AuctionDbContext.cs @@ -6,6 +6,7 @@ using Nop.Plugin.Misc.AuctionPlugin.Domains.DataLayer.Interfaces; using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities; using Nop.Services.Catalog; using System; +using Nop.Plugin.Misc.AuctionPlugin.Services; using Nop.Services.Logging; namespace Nop.Plugin.Misc.AuctionPlugin.Domains.DataLayer; @@ -13,6 +14,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Domains.DataLayer; public class AuctionDbContext : MgDbContextBase, IAuctionDbSet, IProductToAuctionDbSet, IAuctionBidDbSet { private readonly IProductService _productService; + private readonly IStaticCacheManager _staticCacheManager; public AuctionDbTable Auctions { get; set; } public ProductToAuctionDbTable ProductToAuctions { get; set; } @@ -23,9 +25,10 @@ public class AuctionDbContext : MgDbContextBase, IAuctionDbSet, //public IRepository AuctionBids2 { get; set; } - public AuctionDbContext(INopDataProvider dataProvider, AuctionDbTable auctionDbTable, ProductToAuctionDbTable productToAuctionDbTable, AuctionBidDbTable auctionBidDbTable, IProductService productService, ILogger logger) : base(dataProvider, logger) + public AuctionDbContext(INopDataProvider dataProvider, AuctionDbTable auctionDbTable, ProductToAuctionDbTable productToAuctionDbTable, AuctionBidDbTable auctionBidDbTable, IProductService productService, IStaticCacheManager staticCacheManager, ILogger logger) : base(dataProvider, logger) { _productService = productService; + _staticCacheManager = staticCacheManager; Auctions = auctionDbTable; ProductToAuctions = productToAuctionDbTable; @@ -84,9 +87,16 @@ public class AuctionDbContext : MgDbContextBase, IAuctionDbSet, } if (basePrice <= 0) basePrice = product.Price; - else product.Price = basePrice; + else + { + product.Price = basePrice; + await _productService.UpdateProductAsync(product); + } await ProductToAuctions.ResetAsync(productToAuction, basePrice); await AuctionBids.DeleteAllByProductToAuctionIdAsync(productToAuction.Id); + + await _staticCacheManager.ClearAsync(); + //await _staticCacheManager.RemoveByPrefixAsync(AUCTION_PATTERN_KEY); } } \ No newline at end of file diff --git a/Nop.Plugin.Misc.AuctionPlugin/Hubs/SignalRMessageHandler.cs b/Nop.Plugin.Misc.AuctionPlugin/Hubs/SignalRMessageHandler.cs index 9ea2ee9..52ffa61 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Hubs/SignalRMessageHandler.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Hubs/SignalRMessageHandler.cs @@ -13,6 +13,8 @@ using Nop.Core.Domain.Customers; using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities; using Nop.Services.Customers; using DocumentFormat.OpenXml.Wordprocessing; +using Mango.Nop.Core.Repositories; +using Nop.Core.Caching; namespace Nop.Plugin.Misc.AuctionPlugin.Hubs { @@ -91,7 +93,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs { if (revertAuctionBidRequest == null) { - logger.Error($"SignalRMessageHandler.HanldeRevertAuctionBidRequest(); auctionProductStatusRequest == null", null, customer); + logger.Error($"SignalRMessageHandler.HandleRevertAuctionBidRequest(); auctionProductStatusRequest == null", null, customer); return; } @@ -100,7 +102,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs var productToAuction = (await auctionService.GetProductToAuctionMappingByIdAsync(revertAuctionBidRequest.ProductToAuctionId)); if (productToAuction is not { AuctionStatus: AuctionStatus.Pause }) { - logger.Warning($"SignalRMessageHandler.HandleBidRequest(); (productToAuction is not {{ AuctionStatus: AuctionStatus.Pause }}); AuctionStatus: {productToAuction?.AuctionStatus}", null, customer); + logger.Warning($"SignalRMessageHandler.HandleRevertAuctionBidRequest(); (productToAuction is not {{ AuctionStatus: AuctionStatus.Pause }}); AuctionStatus: {productToAuction?.AuctionStatus}", null, customer); return; //TODO: - J. } @@ -111,6 +113,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs if (revertLastBid == null) { //TODO: NA ILYENKOR VAN A CUMI... - J. + logger.Warning($"SignalRMessageHandler.HandleRevertAuctionBidRequest(); revertLastBid == null", null, customer); return; } @@ -119,7 +122,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs } catch (Exception ex) { - logger.Error($"SignalRMessageHandler.HanldeRevertAuctionBidRequest(); Exception: {ex.Message}", ex, customer); + logger.Error($"SignalRMessageHandler.HandleRevertAuctionBidRequest(); Exception: {ex.Message}", ex, customer); } } @@ -234,9 +237,9 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs } var activeProductAuction = (await auctionService.GetProductToAuctionByAuctionIdAndProductIdAsync(bidRequestMessage.AuctionId, bidRequestMessage.ProductId, true)).FirstOrDefault(); - if (activeProductAuction is not { IsActiveItem: true } || activeProductAuction.WinnerCustomerId == customer.Id) + if (activeProductAuction is not { IsActiveItem: true } || (activeProductAuction.WinnerCustomerId == customer.Id && !await customerService.IsAdminAsync(customer))) { - logger.Warning($"SignalRMessageHandler.HandleBidRequest(); (activeProductAuction is not {{ IsActiveItem: true }}); AuctionStatus: {activeProductAuction?.AuctionStatus};", null, 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. } @@ -246,14 +249,13 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs var auctionBid = bidRequestMessage.CreateMainEntity(); auctionBid.ProductAuctionMappingId = activeProductAuction.Id; - //TODO: validate the bidprice amount + //TODO: Validálni, h a bejövő BidPrice valóban a következő licit lépcső! - J. if (product.Price >= auctionBid.BidPrice || activeProductAuction.CurrentPrice >= auctionBid.BidPrice) { logger.Warning($"SignalRMessageHandler.HandleBidRequest(); (product.Price >= bidRequestMessage.BidPrice || activeProductAuction.CurrentPrice >= auctionBid.BidPrice); productPrice: {product.Price}; bidRequestPrice: {auctionBid.BidPrice}; activeProductAuctionPrice: {activeProductAuction.CurrentPrice}", null, customer); return; } - //save bid await auctionService.InsertBidAsync(auctionBid); activeProductAuction.AuctionStatus = AuctionStatus.Active; @@ -316,13 +318,13 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs switch (oldStatus) { case AuctionStatus.None: - return newStatus == AuctionStatus.Active; + return newStatus is AuctionStatus.Active or AuctionStatus.Pause; case AuctionStatus.Active: - return newStatus is AuctionStatus.None or AuctionStatus.FirstWarning or AuctionStatus.Pause; + return newStatus is AuctionStatus.FirstWarning or AuctionStatus.Pause; case AuctionStatus.FirstWarning: - return newStatus is AuctionStatus.None or AuctionStatus.SecondWarning or AuctionStatus.Pause; + return newStatus is AuctionStatus.SecondWarning or AuctionStatus.Pause; case AuctionStatus.SecondWarning: - return newStatus is AuctionStatus.None or AuctionStatus.Sold or AuctionStatus.Pause; + return newStatus is AuctionStatus.Sold or AuctionStatus.Pause; case AuctionStatus.Pause: return newStatus is AuctionStatus.None or AuctionStatus.Active; case AuctionStatus.Sold: diff --git a/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs b/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs index d100a63..c1322fd 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs @@ -21,25 +21,10 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Services; /// public class AuctionService : IAuctionService { - #region Constants - - /// - /// Cache key for pickup points - /// - /// - /// {0} : current store ID - /// - private readonly CacheKey _auctionAllKey = new("Nop.auction.all-{0}", AUCTION_PATTERN_KEY); - private const string AUCTION_PATTERN_KEY = "Nop.auction."; - - #endregion - #region Fields private readonly AuctionDbContext _ctx; private readonly IProductService _productService; - private readonly IShortTermCacheManager _shortTermCacheManager; - private readonly IStaticCacheManager _staticCacheManager; private readonly IWorkContext _workContext; private readonly IOrderProcessingService _orderProcessingService; private readonly ILogger _logger; @@ -53,27 +38,13 @@ public class AuctionService : IAuctionService /// /// Short term cache manager /// Cache manager + /// /// /// - public AuctionService( - AuctionDbContext ctx, - IProductService productService, - - //IRepository auctionRepository, - //IRepository productToAuctionRepository, - IShortTermCacheManager shortTermCacheManager, - IStaticCacheManager staticCacheManager, - IWorkContext workContext, - IOrderProcessingService orderProcessingService, - ILogger logger) + public AuctionService(AuctionDbContext ctx, IProductService productService, IWorkContext workContext, IOrderProcessingService orderProcessingService, ILogger logger) { _ctx = ctx; _productService = productService; - //_customerBidRepository = customerBidRepository; - //_auctionRepository = auctionRepository; - //_productToAuctionRepository = productToAuctionRepository; - _shortTermCacheManager = shortTermCacheManager; - _staticCacheManager = staticCacheManager; _workContext = workContext; _orderProcessingService = orderProcessingService; _logger = logger; @@ -195,7 +166,6 @@ public class AuctionService : IAuctionService if (await ValidateAuctionBid(auctionBid)) { await _ctx.AuctionBids.InsertAsync(auctionBid, false); - await _staticCacheManager.RemoveByPrefixAsync(AUCTION_PATTERN_KEY); } } @@ -204,7 +174,6 @@ public class AuctionService : IAuctionService if (await ValidateAuctionBid(auctionBid)) { await _ctx.AuctionBids.UpdateAsync(auctionBid, false); - await _staticCacheManager.RemoveByPrefixAsync(AUCTION_PATTERN_KEY); } } @@ -216,7 +185,6 @@ public class AuctionService : IAuctionService public async Task DeleteBidAsync(AuctionBid pickupPoint) { await _ctx.AuctionBids.DeleteAsync(pickupPoint, false); - await _staticCacheManager.RemoveByPrefixAsync(AUCTION_PATTERN_KEY); } public async Task CreateOrderForWinner(ProductToAuctionMapping auctionItem) @@ -230,13 +198,11 @@ public class AuctionService : IAuctionService public async Task InsertAuctionAsync(Auction auction) { await _ctx.Auctions.InsertAsync(auction, false); - await _staticCacheManager.RemoveByPrefixAsync(AUCTION_PATTERN_KEY); } public async Task UpdateAuctionAsync(Auction auction) { await _ctx.Auctions.UpdateAsync(auction); - await _staticCacheManager.RemoveByPrefixAsync(AUCTION_PATTERN_KEY); } public async Task> GetAllAuctionsAsync() @@ -368,7 +334,6 @@ public class AuctionService : IAuctionService public async Task UpdateProductToAuctionMappingAsync(ProductToAuctionMapping productToAuctionMapping) { await _ctx.ProductToAuctions.UpdateAsync(productToAuctionMapping); - await _staticCacheManager.RemoveByPrefixAsync(AUCTION_PATTERN_KEY); } #endregion Dtos diff --git a/Nop.Plugin.Misc.AuctionPlugin/Views/PublicProductBidBox.cshtml b/Nop.Plugin.Misc.AuctionPlugin/Views/PublicProductBidBox.cshtml index 3ef195b..349f3dc 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Views/PublicProductBidBox.cshtml +++ b/Nop.Plugin.Misc.AuctionPlugin/Views/PublicProductBidBox.cshtml @@ -118,10 +118,12 @@ document.getElementById("signalRBidButton").disabled = true; event.preventDefault(); + if (!bidBoxPageViewModel) { console.log("we need viewmodel data"); bidBoxPageViewModel = @Html.Raw(Json.Serialize(Model)); } + var bidMessage = { ProductAuctionMappingId: bidBoxPageViewModel.ProductToAuctionId, AuctionId: bidBoxPageViewModel.AuctionId, @@ -129,6 +131,7 @@ ProductId: bidBoxPageViewModel.ProductId, CustomerId: bidBoxPageViewModel.CustomerId }; + var content = JSON.stringify(bidMessage); console.log("WTF " + content); sendMessageToServer("BidRequestMessage", bidBoxPageViewModel.CustomerId, content); @@ -150,7 +153,7 @@ document.getElementById("signalRCloseItemButton").disabled = true; event.preventDefault(); - sendAuctionStatusChange(AuctionStatus.Sold); //Itt SoldOut volt, átírtam Sold-ra! - J. + sendAuctionStatusChange(AuctionStatus.Sold); return false; }); @@ -186,11 +189,30 @@ document.getElementById("signalRRevertBidButton").disabled = true; event.preventDefault(); - sendAuctionStatusChange(AuctionStatus.Revert); + if (!bidBoxPageViewModel) { + console.log("we need viewmodel data"); + bidBoxPageViewModel = @Html.Raw(Json.Serialize(Model)); + } + + var revertMessage = { + ProductToAuctionId: bidBoxPageViewModel.ProductToAuctionId, + }; + + var content = JSON.stringify(revertMessage); + console.log("WTF " + content); + sendMessageToServer("RevertAuctionBidRequest", bidBoxPageViewModel.CustomerId, content); return false; }); + $("#signalRResetItemButton").on("click", function () { + document.getElementById("signalRResetItemButton").disabled = true; + event.preventDefault(); + + sendAuctionStatusChange(AuctionStatus.None); + + return false; + }); }); function sendAuctionStatusChange(auctionStatus) { @@ -231,7 +253,11 @@ console.log(data); var productAuctionMappingId = data.auctionDto.productToAuctionDtos[0].id; console.log(productAuctionMappingId); + + //TODO: TESZT STATUS!!! - JTEST. var status = data.auctionDto.productToAuctionDtos[0].auctionStatus; + //var status = AuctionStatus.TEST; + //if (status == AuctionStatus.FirstWarning) { setButtons(status); //} @@ -274,9 +300,13 @@ function handleAuctionUpdate(data) { var widgetPriceElement = document.getElementById("price-value-" + bidBoxPageViewModel.ProductId); var productAuctionMappingId = data.auctionDto.productToAuctionDtos[0].id; + + //TODO: TESZT STATUS!!! - JTEST. var itemStatus = data.auctionDto.productToAuctionDtos[0].auctionStatus; + //var itemStatus = AuctionStatus.TEST; + console.log("handle auction update called" + productAuctionMappingId); - console.log("auction status:" + itemStatus) + console.log("auction status:" + itemStatus); if (widgetPriceElement) { if (productAuctionMappingId == bidBoxPageViewModel.ProductToAuctionId) { @@ -376,6 +406,17 @@ signalRRevertBidButton: false, signalRResetItemButton: false, }, + [AuctionStatus.TEST]: { + signalRBidButton: false, + signalRFirstWarningButton: false, + signalRSecondWarningButton: false, + signalROpenItemButton: false, + signalRCloseItemButton: false, + signalRPauseItemButton: false, + signalRRevertBidButton: false, + signalRResetItemButton: false, + }, + }; // Get the states for the given auctionStatus