From 7353efadc43903ce6594df4950e478bcccb2a84d Mon Sep 17 00:00:00 2001 From: Loretta Date: Tue, 10 Dec 2024 13:45:29 +0100 Subject: [PATCH] improvements, fixes, etc... --- .../Components/AuctionAdminViewComponent.cs | 4 +- .../LiveAnnouncementViewComponent.cs | 76 ++++++++++++++----- .../Domains/DataLayer/AuctionDbTable.cs | 14 +++- .../Hubs/AuctionHub.cs | 11 ++- .../Services/AuctionService.cs | 11 ++- .../Services/IAuctionService.cs | 4 +- 6 files changed, 90 insertions(+), 30 deletions(-) diff --git a/Nop.Plugin.Misc.AuctionPlugin/Areas/Admin/Components/AuctionAdminViewComponent.cs b/Nop.Plugin.Misc.AuctionPlugin/Areas/Admin/Components/AuctionAdminViewComponent.cs index 31b5062..74e5029 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Areas/Admin/Components/AuctionAdminViewComponent.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Areas/Admin/Components/AuctionAdminViewComponent.cs @@ -98,11 +98,11 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Areas.Admin.Components productId = productModel.Id; - var auctions = await _auctionService.GetAllAuctionsAsync() ?? new List(); + var auctions = await _auctionService.GetAllAuctionsAsync() ?? []; // Assign the auctions to the ViewBag ViewBag.Auctions = auctions; - ProductAssignToAuctionViewModel viewModel = new ProductAssignToAuctionViewModel(); + var viewModel = new ProductAssignToAuctionViewModel(); viewModel.ProductId = productId; viewModel.StartingPrice = productModel.OldPrice; viewModel.BidPrice = productModel.OldPrice; diff --git a/Nop.Plugin.Misc.AuctionPlugin/Components/LiveAnnouncementViewComponent.cs b/Nop.Plugin.Misc.AuctionPlugin/Components/LiveAnnouncementViewComponent.cs index 4993a2e..c6fc986 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Components/LiveAnnouncementViewComponent.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Components/LiveAnnouncementViewComponent.cs @@ -1,5 +1,12 @@ -using Microsoft.AspNetCore.Mvc; +using AyCode.Core.Extensions; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.SignalR; using Nop.Core; +using Nop.Plugin.Misc.AuctionPlugin.Domains.Dtos; +using Nop.Plugin.Misc.AuctionPlugin.Domains.Enums; +using Nop.Plugin.Misc.AuctionPlugin.Hubs; +using Nop.Plugin.Misc.AuctionPlugin.Hubs.Messages; +using Nop.Plugin.Misc.AuctionPlugin.Services; using Nop.Services.Cms; using Nop.Services.Logging; using Nop.Web.Framework.Components; @@ -9,31 +16,64 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Components { [ViewComponent(Name = "LiveAnnouncement")] - public class LiveAnnouncementViewComponent : NopViewComponent + public class LiveAnnouncementViewComponent(ILogger logger, IWorkContext workContext, IWidgetPluginManager widgetPluginManager, AuctionService auctionService, IHubContext auctionHubContext) + : NopViewComponent { - protected readonly ILogger _logger; - protected readonly IWorkContext _workContext; - protected readonly IWidgetPluginManager _widgetPluginManager; - - public LiveAnnouncementViewComponent(ILogger logger, IWorkContext workContext, IWidgetPluginManager widgetPluginManager) - { - _logger = logger; - _workContext = workContext; - _widgetPluginManager = widgetPluginManager; - } - public async Task InvokeAsync(string widgetZone, object additionalData) { - await _logger.InformationAsync("SignalR Widget called"); + //await logger.InformationAsync("LiveAnnouncementViewComponent.InvokeAsync called"); + + //lock... - J. + var auctions = await auctionService.GetAllCurrentAutoOpenAndClosedAuctionsAsync(); + if (auctions.Count > 0) + { + await logger.InformationAsync($"LiveAnnouncementViewComponent.InvokeAsync auctions.Count > 0; count: {auctions.Count}; names: {string.Join("; ", auctions.Select(x => x.AuctionName))}"); + + var statusChangedMessageWrapper = new MessageWrapper + { + MessageType = nameof(ProductToAuctionStatusNotification), + SenderId = 0, + ResponseType = ResponseType.ToAllClients + }; + + foreach (var auction in auctions) + { + auction.Closed = false; + await auctionService.UpdateAuctionAsync(auction); + + var auctionDto = new AuctionDto(auction); + var productToAuctions = (await auctionService.GetProductToAuctionsByAuctionIdAsync(auction.Id, false)).Where(x => x.AuctionStatus == AuctionStatus.None).ToList(); + + foreach (var productToAuction in productToAuctions) + { + productToAuction.AuctionStatus = AuctionStatus.Active; + + await auctionService.UpdateProductToAuctionMappingAsync(productToAuction); + + auctionDto.ProductToAuctionDtos.Clear();//TEMPOPRARY - J. + auctionDto.ProductToAuctionDtos.Add(new ProductToAuctionDto(productToAuction)); + + ////TEMPOPRARY - J. + //statusChangedMessageWrapper.Data = new ProductToAuctionStatusNotification(auctionDto, 0, $"Az aukciót megnyitottuk: {auction.AuctionName}").ToJson(); + //await _auctionHubContext.Clients.All.SendAsync("send", statusChangedMessageWrapper.ToJson()); + ////TEMPOPRARY - J. + } + + await auctionService.UpdateProductToAuctionMappingAsync(productToAuctions); + + statusChangedMessageWrapper.Data = new ProductToAuctionStatusNotification(auctionDto, 0, $"Az aukciót megnyitottuk: {auction.AuctionName}").ToJson(); + await auctionHubContext.Clients.All.SendAsync("send", statusChangedMessageWrapper.ToJson()); + } + } //ensure that what3words widget is active and enabled - var customer = await _workContext.GetCurrentCustomerAsync(); - await _logger.InformationAsync($"SignalR Widget called customer: {customer.Email}"); + var customer = await workContext.GetCurrentCustomerAsync(); + await logger.InformationAsync($"SignalR Widget called customer: {customer.Email}"); - if (!await _widgetPluginManager.IsPluginActiveAsync(AuctionDefaults.SystemName, customer)) + if (!await widgetPluginManager.IsPluginActiveAsync(AuctionDefaults.SystemName, customer)) return Content(string.Empty); - await _logger.InformationAsync("SignalR Widget: widget active"); + await logger.InformationAsync("SignalR Widget: widget active"); if (!widgetZone.Equals(PublicWidgetZones.BodyStartHtmlTagAfter)) { diff --git a/Nop.Plugin.Misc.AuctionPlugin/Domains/DataLayer/AuctionDbTable.cs b/Nop.Plugin.Misc.AuctionPlugin/Domains/DataLayer/AuctionDbTable.cs index 26041c4..2810284 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Domains/DataLayer/AuctionDbTable.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Domains/DataLayer/AuctionDbTable.cs @@ -5,6 +5,7 @@ using Nop.Core.Configuration; using Nop.Core.Events; using Nop.Data; using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities; +using Nop.Plugin.Misc.AuctionPlugin.Domains.Enums; using Nop.Services.Logging; namespace Nop.Plugin.Misc.AuctionPlugin.Domains.DataLayer; @@ -16,9 +17,18 @@ public class AuctionDbTable: MgDbTableBase { } - public Task> GetAllAuctionsAsync() + public IQueryable GetAllAuctions() { - return GetAllAsync(auctions => auctions.OrderByDescending(x => x.StartDateUtc), _ => default); + return Table; //GetAllAsync(auctions => auctions.OrderByDescending(x => x.StartDateUtc), _ => default); } + /// + /// x.StartDateUtc <= utcNow && x.EndDateUtc >= utcNow + /// + /// + public IQueryable GetAllCurrentAutoOpenAndClosedAuctions() + { + var utcNow = DateTime.UtcNow; + return Table.Where(x => x.AuctionType == AuctionType.AutomaticAll && x.Closed && x.StartDateUtc <= utcNow && x.EndDateUtc >= utcNow).OrderByDescending(x=>x.StartDateUtc); + } } \ No newline at end of file diff --git a/Nop.Plugin.Misc.AuctionPlugin/Hubs/AuctionHub.cs b/Nop.Plugin.Misc.AuctionPlugin/Hubs/AuctionHub.cs index 06dab92..390d267 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Hubs/AuctionHub.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Hubs/AuctionHub.cs @@ -14,6 +14,7 @@ using Nop.Core.Domain.Catalog; using Nop.Core.Domain.Customers; using Nop.Services.Catalog; using Newtonsoft.Json.Linq; +using DocumentFormat.OpenXml.Spreadsheet; namespace Nop.Plugin.Misc.AuctionPlugin.Hubs { @@ -34,8 +35,10 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs if (httpContext == null) await logger.ErrorAsync($"AuctionHub.OnConnectedAsync(); (httpContext == null); connectionId: {connectionId}"); else { - if (sessionService.GetOrCreateSessionItem(httpContext.Session.Id) == null) - await logger.ErrorAsync($"AuctionHub.OnConnectedAsync(); (sessionItem == null); connectionId: {connectionId}; sessionId: {httpContext.Session.Id}"); + var sessionItem = sessionService.GetOrCreateSessionItem(httpContext.Session.Id); + + if (sessionItem == null) await logger.ErrorAsync($"AuctionHub.OnConnectedAsync(); (sessionItem == null); connectionId: {connectionId}; sessionId: {httpContext.Session.Id}"); + else sessionItem.SignaRConnectionId = connectionId; var userName = httpContext.Request.Query["ConnectionId"]; if (!string.IsNullOrEmpty(userName)) @@ -43,7 +46,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs await logger.InformationAsync($"AuctionHub.OnConnectedAsync(); Caller connected with name: {userName}; connectionId: {connectionId}"); } } - + await base.OnConnectedAsync(); } @@ -67,6 +70,8 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs var customer = await workContext.GetCurrentCustomerAsync(); //var connectionId = sessionItem?.SessionId; + //ArgumentNullException.ThrowIfNull(messageWrapper?.Data); + if (messageWrapper?.Data == null) { logger.Error($"AuctionHub.HandleMessageAsync(); message?.Data == null; connectionId: {connectionId}", null, customer); diff --git a/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs b/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs index 4655cd0..06f8c56 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs @@ -396,10 +396,8 @@ public class AuctionService( await ctx.Auctions.UpdateAsync(auction); } - public async Task> GetAllAuctionsAsync() - { - return await ctx.Auctions.GetAllAuctionsAsync(); - } + public Task> GetAllAuctionsAsync() =>ctx.Auctions.GetAllAuctions().ToListAsync(); + public Task> GetAllCurrentAutoOpenAndClosedAuctionsAsync() =>ctx.Auctions.GetAllCurrentAutoOpenAndClosedAuctions().ToListAsync(); public async Task> GetProductToAuctionsByAuctionIdAsync(int auctionId, bool onlyActiveItems) => await ctx.ProductToAuctions.GetProductToAuctionsByAuctionId(auctionId, onlyActiveItems).OrderBy(x => x.SortIndex).ToListAsync(); @@ -535,5 +533,10 @@ public class AuctionService( await ctx.ProductToAuctions.UpdateAsync(productToAuctionMapping); } + public async Task UpdateProductToAuctionMappingAsync(IList productToAuctionMappings) + { + await ctx.ProductToAuctions.UpdateAsync(productToAuctionMappings); + } + #endregion Dtos } \ No newline at end of file diff --git a/Nop.Plugin.Misc.AuctionPlugin/Services/IAuctionService.cs b/Nop.Plugin.Misc.AuctionPlugin/Services/IAuctionService.cs index adbf352..a65c1be 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Services/IAuctionService.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Services/IAuctionService.cs @@ -41,7 +41,8 @@ public interface IAuctionService Task InsertAuctionAsync(Auction auction); Task UpdateAuctionAsync(Auction auction); - Task> GetAllAuctionsAsync(); + Task> GetAllAuctionsAsync(); + Task> GetAllCurrentAutoOpenAndClosedAuctionsAsync(); Task GetAuctionDtoWithAuctionBids(int auctionId, bool activeProductOnly, int maxBidsCount); Task> GetProductToAuctionsByAuctionIdAsync(int auctionId, bool onlyActiveItems); @@ -65,6 +66,7 @@ public interface IAuctionService Task> GetProductToAuctionByAuctionIdAndProductIdAsync(int auctionId, int productId, bool activeProductOnly); Task GetProductToAuctionMappingByIdAsync(int productToAuctionMappingId); Task UpdateProductToAuctionMappingAsync(ProductToAuctionMapping productToAuctionMapping); + Task UpdateProductToAuctionMappingAsync(IList productToAuctionMappings); Task CreateOrderForWinnerAsync(ProductToAuctionMapping productToAuctionMapping); } \ No newline at end of file