Mango.Nop.Plugins/Nop.Plugin.Misc.AuctionPlugin/Components/AuctionPublicViewComponent.cs

264 lines
10 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Nop.Core;
using Nop.Core.Domain.Catalog;
using Nop.Plugin.Misc.AuctionPlugin.Domains.Dtos;
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;
using Nop.Web.Framework.Mvc.Routing;
using Nop.Services.Catalog;
using Nop.Web.Framework;
using Nop.Services.Directory;
namespace Nop.Plugin.Misc.AuctionPlugin.Components;
public class AuctionPublicViewComponent : NopViewComponent
{
#region Fields
//private readonly IAddressService _addressService;
//private readonly IGenericAttributeService _genericAttributeService;
private readonly IWidgetPluginManager _widgetPluginManager;
private readonly IWorkContext _workContext;
private readonly IStoreContext _storeContext;
private readonly ICurrencyService _currencyService;
private readonly AuctionService _auctionService;
//private readonly AuctionSettings _auctionSettings;
private readonly ICustomerService _customerService;
private readonly IWebHelper _webHelper;
private readonly IProductService _productService;
private readonly ILogger _logger;
private readonly MyProductModelFactory _myProductModelFactory;
#endregion
#region Ctor
public AuctionPublicViewComponent(
//IAddressService addressService,
//IGenericAttributeService genericAttributeService,
IWidgetPluginManager widgetPluginManager,
IWorkContext workContext,
IStoreContext storeContext,
ICurrencyService currencyService,
AuctionService auctionService,
//AuctionSettings auctionSettings,
ICustomerService customerService,
IWebHelper webHelper,
IProductService productService,
MyProductModelFactory myProductModelFactory,
ILogger logger)
{
//_addressService = addressService;
//_genericAttributeService = genericAttributeService;
_widgetPluginManager = widgetPluginManager;
_workContext = workContext;
_storeContext = storeContext;
_currencyService = currencyService;
_auctionService = auctionService;
//_auctionSettings = auctionSettings;
_customerService = customerService;
_webHelper = webHelper;
_productService = productService;
_myProductModelFactory = myProductModelFactory;
_logger = logger;
}
#endregion
#region Methods
/// <summary>
/// Invoke the widget view component
/// </summary>
/// <param name="widgetZone">Widget zone</param>
/// <param name="additionalData">Additional parameters</param>
/// <returns>
/// A task that represents the asynchronous operation
/// The task result contains the view component result
/// </returns>
public async Task<IViewComponentResult> InvokeAsync(string widgetZone, object additionalData)
{
await _logger.InformationAsync("WidgetViewComponent called");
var customer = await _workContext.GetCurrentCustomerAsync();
var currency = await _workContext.GetWorkingCurrencyAsync();
var store = await _storeContext.GetCurrentStoreAsync();
//var allCurrencies = await _currencyService.GetAllCurrenciesAsync();
//var baseCurrency = allCurrencies.Where(x => x.Rate == 1);
await _logger.InformationAsync($"WidgetViewComponent customer: {customer.Email}");
//ensure that widget is active and enabled
if (!await _widgetPluginManager.IsPluginActiveAsync(AuctionDefaults.SystemName, customer))
return Content(string.Empty);
await _logger.InformationAsync("WidgetViewComponent widget active");
//if (!_auctionSettings.Enabled)
// return Content(string.Empty);
AuctionProductModel myWIPModel = new AuctionProductModel();
switch (additionalData)
{
case ProductOverviewModel productOverViewModel:
Console.WriteLine("The object is of type A.");
myWIPModel = await AuctionProductModel.CreateAsync(productOverViewModel, _myProductModelFactory, _productService);
break;
case ProductDetailsModel productDetailsModel:
Console.WriteLine("The object is of type B.");
myWIPModel = new AuctionProductModel(additionalData as ProductDetailsModel, _myProductModelFactory, _productService)!;
break;
default:
_logger.Error("The object is neither Overview nor DetailModel.");
break;
}
await _logger.InformationAsync($"WidgetViewComponent product: {myWIPModel.Name}");
//if (productDetailsModel is null)
//{
// await _logger.InformationAsync("WidgetViewComponent productdetailsmodel is null");
// return Content(string.Empty);
//}
if (!(widgetZone.Equals(PublicWidgetZones.ProductDetailsOverviewTop) || widgetZone.Equals(PublicWidgetZones.ProductBoxAddinfoAfter)))
{
await _logger.InformationAsync($"WidgetViewComponent is NOT in ProductDetailsTop now {widgetZone}");
return Content(string.Empty);
}
await _logger.InformationAsync("WidgetViewComponent called II");
//is it under Auction?
var productId = myWIPModel.Id;
//TODO: itt a ProductToAuctionMapping.Id-t kellene használni! - J.
var productToAuction = (await _auctionService.GetProductToAuctionDtosByProductIdAsync(productId)).FirstOrDefault();
if (productToAuction == null)
{
return Content(string.Empty);
}
var auctionDto = (await _auctionService.GetAuctionDtoByIdAsync(productToAuction.AuctionId, false, false));
auctionDto.ProductToAuctionDtos.Add(productToAuction);
var productBidBoxViewModel = new ProductBidBoxViewModel(auctionDto);
//List<ProductToAuctionMapping> productToAuctionId = await _auctionService.GetProductToAuctionByAuctionIdAndProductIdAsync(auctionId, productDetailsModel.Id);
var status = productToAuction.AuctionStatus;
//bool isActive = status == AuctionStatus.Active || status == AuctionStatus.FirstWarning || status == AuctionStatus.SecondWarning;
//bool isFirstWarning = status == AuctionStatus.FirstWarning;
var detailedAuctionDto = (await _auctionService.GetAuctionDtoByIdAsync(productToAuction.AuctionId, true, false));
ProductToAuctionDto nextProductToAuction;
ProductToAuctionDto lastProductToAuction;
var nextUrl = "";
var lastUrl = "";
var nextImageUrl = "";
var lastImageUrl = "";
var nextProductName = "";
var lastProductName = "";
if (productToAuction.SortIndex < detailedAuctionDto.ProductToAuctionDtos.Count)
{
nextProductToAuction = detailedAuctionDto.ProductToAuctionDtos.FirstOrDefault(x => x.SortIndex == productToAuction.SortIndex + 1);
if (nextProductToAuction != null)
{
var nextProductId = nextProductToAuction.ProductId;
var nextProduct = await _productService.GetProductByIdAsync(nextProductId);
var nextDetails = await _myProductModelFactory.PrepareProductDetailsModelAsync(nextProduct);
nextUrl = Url.RouteUrl<Product>(new { nextDetails.SeName }, _webHelper.GetCurrentRequestProtocol()).ToLowerInvariant();
nextImageUrl = nextDetails.DefaultPictureModel.FullSizeImageUrl;
nextProductName = nextDetails.Name;
}
}
else
{
nextProductToAuction = null;
}
if (productToAuction.SortIndex > 1)
{
lastProductToAuction = detailedAuctionDto.ProductToAuctionDtos.FirstOrDefault(x => x.SortIndex == productToAuction.SortIndex - 1);
if (lastProductToAuction != null)
{
var lastProductId = lastProductToAuction.ProductId;
var lastProduct = await _productService.GetProductByIdAsync(lastProductId);
var lastDetails = await _myProductModelFactory.PrepareProductDetailsModelAsync(lastProduct);
lastUrl = Url.RouteUrl<Product>(new { lastDetails.SeName }, _webHelper.GetCurrentRequestProtocol()).ToLowerInvariant();
lastImageUrl = lastDetails.DefaultPictureModel.FullSizeImageUrl;
lastProductName = lastDetails.Name;
}
}
else
{
lastProductToAuction = null;
}
productBidBoxViewModel.IsAdmin = await _customerService.IsAdminAsync(customer);
productBidBoxViewModel.IsGuest = await _customerService.IsGuestAsync(customer);
productBidBoxViewModel.AuctionClosed = auctionDto.Closed;
productBidBoxViewModel.AuctionStatus = status;
productBidBoxViewModel.WidgetZone = widgetZone;
//comes in WORKINGCURRENCY
productBidBoxViewModel.BasePrice = productToAuction.StartingPrice;
//comes in WORKINGCURRENCY
productBidBoxViewModel.CurrentPrice = productToAuction.CurrentPrice;
//productBidBoxViewModel.ProductToAuctionId = productToAuctionId.FirstOrDefault().Id;
//productBidBoxViewModel.AuctionId = auctionId;
productBidBoxViewModel.CustomerId = customer.Id;
productBidBoxViewModel.WorkingCurrency = currency;
productBidBoxViewModel.ProductId = productToAuction.ProductId;
//productBidBoxViewModel.NextProductUrl = Url.RouteUrl("Product", productDetailsModel.SeName);
productBidBoxViewModel.NextProductUrl = nextUrl;
productBidBoxViewModel.LastProductUrl = lastUrl;
productBidBoxViewModel.LastProductImageUrl = lastImageUrl;
productBidBoxViewModel.NextProductImageUrl = nextImageUrl;
productBidBoxViewModel.LastProductName = lastProductName;
productBidBoxViewModel.NextProductName = nextProductName;
//comes in HUF
productBidBoxViewModel.LicitStep = AuctionService.GetStepAmount(productToAuction.CurrentPrice); //add calculation
//comes IN HUF
productBidBoxViewModel.LicitStepInWorkingCurrency = productBidBoxViewModel.LicitStep * currency.Rate;
//comes in HUF
productBidBoxViewModel.NextBidPrice = AuctionService.GetNextBidPrice(productToAuction.CurrentPrice, productBidBoxViewModel.LicitStep, productToAuction.BidsCount > 0);
productBidBoxViewModel.NextBidPriceInWorkingCurrency = productBidBoxViewModel.NextBidPrice * currency.Rate;
if(productBidBoxViewModel.BasePrice != null)
{
//comes in WORKINGCURRENCY
productBidBoxViewModel.BasePriceInWorkingCurrency = productBidBoxViewModel.BasePrice;
}
else
{
//basprice null, we use the baseprice in PTA that comes in HUF
productBidBoxViewModel.BasePrice = productToAuction.StartingPrice * currency.Rate;
productBidBoxViewModel.BasePriceInWorkingCurrency = productBidBoxViewModel.BasePrice;
}
productBidBoxViewModel.CurrentPriceInWorkingCurrency = productBidBoxViewModel.CurrentPrice;
return View("~/Plugins/Misc.AuctionPlugin/Views/PublicProductBidBox.cshtml", productBidBoxViewModel);
}
#endregion
}