using Microsoft.AspNetCore.Mvc; using Nop.Core; using Nop.Plugin.Misc.AuctionPlugin.Domains.Dtos; using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities; using Nop.Plugin.Misc.AuctionPlugin.Domains.Enums; using Nop.Plugin.Misc.AuctionPlugin.Models; using Nop.Plugin.Misc.AuctionPlugin.Services; using Nop.Services.Cms; using Nop.Services.Common; using Nop.Services.Customers; using Nop.Services.Logging; using Nop.Web.Framework.Components; using Nop.Web.Framework.Infrastructure; using Nop.Web.Models.Catalog; namespace Nop.Plugin.Misc.AuctionPlugin.Components; public class AuctionPublicViewComponent : NopViewComponent { #region Fields protected readonly IAddressService _addressService; protected readonly IGenericAttributeService _genericAttributeService; protected readonly IWidgetPluginManager _widgetPluginManager; protected readonly IWorkContext _workContext; protected readonly AuctionService _auctionService; protected readonly AuctionSettings _auctionSettings; protected readonly ICustomerService _customerService; protected readonly ILogger _logger; #endregion #region Ctor public AuctionPublicViewComponent(IAddressService addressService, IGenericAttributeService genericAttributeService, IWidgetPluginManager widgetPluginManager, IWorkContext workContext, AuctionService auctionService, AuctionSettings auctionSettings, ICustomerService customerService, ILogger logger) { _addressService = addressService; _genericAttributeService = genericAttributeService; _widgetPluginManager = widgetPluginManager; _workContext = workContext; _auctionService = auctionService; _auctionSettings = auctionSettings; _customerService = customerService; _logger = logger; } #endregion #region Methods /// /// Invoke the widget view component /// /// Widget zone /// Additional parameters /// /// A task that represents the asynchronous operation /// The task result contains the view component result /// public async Task InvokeAsync(string widgetZone, object additionalData) { ProductBidBoxViewModel productBidBoxViewModel = new ProductBidBoxViewModel(); await _logger.InformationAsync("WidgetViewComponent called"); //ensure that widget is active and enabled var customer = await _workContext.GetCurrentCustomerAsync(); await _logger.InformationAsync($"WidgetViewComponent customer: {customer.Email}"); if (!await _widgetPluginManager.IsPluginActiveAsync(AuctionDefaults.SystemName, customer)) return Content(string.Empty); await _logger.InformationAsync("WidgetViewComponent widget active"); //if (!_auctionSettings.Enabled) // return Content(string.Empty); var productDetailsModel = additionalData as ProductDetailsModel; await _logger.InformationAsync($"WidgetViewComponent product: {productDetailsModel.Name}"); //if (productDetailsModel is null) //{ // await _logger.InformationAsync("WidgetViewComponent productdetailsmodel is null"); // return Content(string.Empty); //} if (!widgetZone.Equals(PublicWidgetZones.ProductDetailsOverviewTop)) { await _logger.InformationAsync($"WidgetViewComponent is NOT in ProductDetailsTop now {widgetZone}"); return Content(string.Empty); } await _logger.InformationAsync("WidgetViewComponent called II"); //is it under Auction? List auctionIds = await _auctionService.GetProductToAuctionsByProductIdAsync(productDetailsModel.Id); int auctionId = 0; AuctionDto auction; if(auctionIds == null || auctionIds.Count == 0) { return Content(string.Empty); } if (auctionIds.Count > 1) { List openAuctionList = []; //we have more auctions so we return the closest open foreach (var auctionMapping in auctionIds) { var auctionItem = await _auctionService.GetAuctionDtoByIdAsync(auctionMapping.AuctionId); if (!auctionItem.Closed) { openAuctionList.Add(auctionItem); } } //first auction that started or strats not earlier than yesterday auction = openAuctionList.Where(x => x.StartDateUtc > DateTime.UtcNow.AddDays(-1)).OrderBy(x => x.StartDateUtc).FirstOrDefault(); auctionId = auction.Id; } else { auction = await _auctionService.GetAuctionDtoByIdAsync(auctionIds.FirstOrDefault().AuctionId); auctionId = auction.Id; } var productToAuctionId = await _auctionService.GetProductToAuctionByAuctionIdAndProductIdAsync(auctionId, productDetailsModel.Id); productBidBoxViewModel.IsAdmin = await _customerService.IsAdminAsync(customer); productBidBoxViewModel.IsGuest = await _customerService.IsGuestAsync(customer); productBidBoxViewModel.AuctionClosed = auction.Closed; productBidBoxViewModel.WidgetZone = widgetZone; productBidBoxViewModel.BasePrice = productDetailsModel.ProductPrice.OldPriceValue; productBidBoxViewModel.CurrentPrice = productDetailsModel.ProductPrice.PriceValue; productBidBoxViewModel.ProductToAuctionsId = productToAuctionId.FirstOrDefault().Id; productBidBoxViewModel.AuctionId = auctionId; productBidBoxViewModel.CustomerId = customer.Id; productBidBoxViewModel.ProductId = productDetailsModel.Id; productBidBoxViewModel.LicitStep = 50000; //add calculation productBidBoxViewModel.BidPrice = productDetailsModel.ProductPrice.PriceValue + productBidBoxViewModel.LicitStep; return View("~/Plugins/Misc.AuctionPlugin/Views/PublicProductBidBox.cshtml", productBidBoxViewModel); } #endregion }