This commit is contained in:
Loretta 2024-11-27 16:25:14 +01:00
parent 354bdb6556
commit d9b4a7ffdd
6 changed files with 683 additions and 676 deletions

View File

@ -1,10 +1,7 @@
using ExCSS; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using Nop.Core; using Nop.Core;
using Nop.Core.Domain.Catalog; using Nop.Core.Domain.Catalog;
using Nop.Plugin.Misc.AuctionPlugin.Domains.Dtos; 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.Models;
using Nop.Plugin.Misc.AuctionPlugin.Services; using Nop.Plugin.Misc.AuctionPlugin.Services;
using Nop.Services.Cms; using Nop.Services.Cms;
@ -15,196 +12,198 @@ using Nop.Web.Framework.Components;
using Nop.Web.Framework.Infrastructure; using Nop.Web.Framework.Infrastructure;
using Nop.Web.Models.Catalog; using Nop.Web.Models.Catalog;
using Nop.Web.Framework.Mvc.Routing; using Nop.Web.Framework.Mvc.Routing;
using DocumentFormat.OpenXml.EMMA;
using Nop.Services.Catalog; using Nop.Services.Catalog;
namespace Nop.Plugin.Misc.AuctionPlugin.Components; namespace Nop.Plugin.Misc.AuctionPlugin.Components;
public class AuctionPublicViewComponent : NopViewComponent public class AuctionPublicViewComponent : NopViewComponent
{ {
#region Fields #region Fields
protected readonly IAddressService _addressService; //private readonly IAddressService _addressService;
protected readonly IGenericAttributeService _genericAttributeService; //private readonly IGenericAttributeService _genericAttributeService;
protected readonly IWidgetPluginManager _widgetPluginManager; private readonly IWidgetPluginManager _widgetPluginManager;
protected readonly IWorkContext _workContext; private readonly IWorkContext _workContext;
protected readonly AuctionService _auctionService; private readonly AuctionService _auctionService;
protected readonly AuctionSettings _auctionSettings; //private readonly AuctionSettings _auctionSettings;
protected readonly ICustomerService _customerService; private readonly ICustomerService _customerService;
protected readonly IWebHelper _webHelper; private readonly IWebHelper _webHelper;
protected readonly IProductService _productService; private readonly IProductService _productService;
protected readonly ILogger _logger; private readonly ILogger _logger;
protected readonly MyProductModelFactory _myProductModelFactory; private readonly MyProductModelFactory _myProductModelFactory;
#endregion #endregion
#region Ctor #region Ctor
public AuctionPublicViewComponent(IAddressService addressService, public AuctionPublicViewComponent(
IGenericAttributeService genericAttributeService, //IAddressService addressService,
IWidgetPluginManager widgetPluginManager, //IGenericAttributeService genericAttributeService,
IWorkContext workContext, IWidgetPluginManager widgetPluginManager,
AuctionService auctionService, IWorkContext workContext,
AuctionSettings auctionSettings, AuctionService auctionService,
ICustomerService customerService, //AuctionSettings auctionSettings,
IWebHelper webHelper, ICustomerService customerService,
IProductService productService, IWebHelper webHelper,
MyProductModelFactory myProductModelFactory, IProductService productService,
ILogger logger) MyProductModelFactory myProductModelFactory,
{ ILogger logger)
_addressService = addressService; {
_genericAttributeService = genericAttributeService; //_addressService = addressService;
_widgetPluginManager = widgetPluginManager; //_genericAttributeService = genericAttributeService;
_workContext = workContext; _widgetPluginManager = widgetPluginManager;
_auctionService = auctionService; _workContext = workContext;
_auctionSettings = auctionSettings; _auctionService = auctionService;
_customerService = customerService; //_auctionSettings = auctionSettings;
_webHelper = webHelper; _customerService = customerService;
_productService = productService; _webHelper = webHelper;
_myProductModelFactory = myProductModelFactory; _productService = productService;
_logger = logger; _myProductModelFactory = myProductModelFactory;
} _logger = logger;
}
#endregion #endregion
#region Methods #region Methods
/// <summary> /// <summary>
/// Invoke the widget view component /// Invoke the widget view component
/// </summary> /// </summary>
/// <param name="widgetZone">Widget zone</param> /// <param name="widgetZone">Widget zone</param>
/// <param name="additionalData">Additional parameters</param> /// <param name="additionalData">Additional parameters</param>
/// <returns> /// <returns>
/// A task that represents the asynchronous operation /// A task that represents the asynchronous operation
/// The task result contains the view component result /// The task result contains the view component result
/// </returns> /// </returns>
public async Task<IViewComponentResult> InvokeAsync(string widgetZone, object additionalData) public async Task<IViewComponentResult> InvokeAsync(string widgetZone, object additionalData)
{ {
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?
var productId = productDetailsModel.Id;
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);
AuctionStatus 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;
string nextUrl = "";
string lastUrl = "";
string nextImageUrl = "";
string lastImageUrl = "";
string nextProductName = "";
string lastProductName = "";
if (productToAuction.SortIndex < detailedAuctionDto.ProductToAuctionDtos.Count)
{
nextProductToAuction = detailedAuctionDto.ProductToAuctionDtos.Where(x => x.SortIndex == productToAuction.SortIndex + 1).FirstOrDefault();
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.SeName;
}
else
{
nextProductToAuction = null;
}
if (productToAuction.SortIndex > 1)
{
lastProductToAuction = detailedAuctionDto.ProductToAuctionDtos.Where(x => x.SortIndex == productToAuction.SortIndex - 1).FirstOrDefault();
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.SeName;
}
else
{
lastProductToAuction = null;
}
productBidBoxViewModel.IsAdmin = await _customerService.IsAdminAsync(customer); await _logger.InformationAsync("WidgetViewComponent called");
productBidBoxViewModel.IsGuest = await _customerService.IsGuestAsync(customer);
productBidBoxViewModel.AuctionClosed = auctionDto.Closed; //ensure that widget is active and enabled
productBidBoxViewModel.AuctionStatus = status; var customer = await _workContext.GetCurrentCustomerAsync();
productBidBoxViewModel.WidgetZone = widgetZone; await _logger.InformationAsync($"WidgetViewComponent customer: {customer.Email}");
productBidBoxViewModel.BasePrice = productDetailsModel.ProductPrice.OldPriceValue;
productBidBoxViewModel.CurrentPrice = productDetailsModel.ProductPrice.PriceValue; if (!await _widgetPluginManager.IsPluginActiveAsync(AuctionDefaults.SystemName, customer))
//productBidBoxViewModel.ProductToAuctionId = productToAuctionId.FirstOrDefault().Id; return Content(string.Empty);
//productBidBoxViewModel.AuctionId = auctionId;
productBidBoxViewModel.CustomerId = customer.Id; await _logger.InformationAsync("WidgetViewComponent widget active");
productBidBoxViewModel.ProductId = productDetailsModel.Id;
//productBidBoxViewModel.NextProductUrl = Url.RouteUrl("Product", productDetailsModel.SeName); //if (!_auctionSettings.Enabled)
productBidBoxViewModel.NextProductUrl = nextUrl; // return Content(string.Empty);
productBidBoxViewModel.LastProductUrl = lastUrl;
productBidBoxViewModel.LastProductImageUrl = lastImageUrl; var productDetailsModel = (additionalData as ProductDetailsModel)!;
productBidBoxViewModel.NextProductImageUrl = nextImageUrl;
productBidBoxViewModel.LastProductName = lastProductName; await _logger.InformationAsync($"WidgetViewComponent product: {productDetailsModel.Name}");
productBidBoxViewModel.NextProductName = nextProductName;
productBidBoxViewModel.LicitStep = AuctionService.GetStepAmount(productToAuction.CurrentPrice); //add calculation //if (productDetailsModel is null)
productBidBoxViewModel.NextBidPrice = productToAuction.CurrentPrice + productBidBoxViewModel.LicitStep; //{
// 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?
var productId = productDetailsModel.Id;
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.SeName;
}
}
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.SeName;
}
}
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;
productBidBoxViewModel.BasePrice = productDetailsModel.ProductPrice.OldPriceValue;
productBidBoxViewModel.CurrentPrice = productDetailsModel.ProductPrice.PriceValue;
//productBidBoxViewModel.ProductToAuctionId = productToAuctionId.FirstOrDefault().Id;
//productBidBoxViewModel.AuctionId = auctionId;
productBidBoxViewModel.CustomerId = customer.Id;
productBidBoxViewModel.ProductId = productDetailsModel.Id;
//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;
productBidBoxViewModel.LicitStep = AuctionService.GetStepAmount(productToAuction.CurrentPrice); //add calculation
productBidBoxViewModel.NextBidPrice = AuctionService.GetNextBidPrice(productToAuction.CurrentPrice, productBidBoxViewModel.LicitStep);
return View("~/Plugins/Misc.AuctionPlugin/Views/PublicProductBidBox.cshtml", productBidBoxViewModel); return View("~/Plugins/Misc.AuctionPlugin/Views/PublicProductBidBox.cshtml", productBidBoxViewModel);
} }
#endregion #endregion
} }

View File

@ -323,11 +323,8 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
} }
private static decimal GetStepAmount(decimal currentBidPrice) => AuctionService.GetStepAmount(currentBidPrice); private static decimal GetStepAmount(decimal currentBidPrice) => AuctionService.GetStepAmount(currentBidPrice);
private static decimal GetNextBidPrice(decimal currentBidPrice) => GetNextBidPrice(currentBidPrice, GetStepAmount(currentBidPrice)); private static decimal GetNextBidPrice(decimal currentBidPrice) => AuctionService.GetNextBidPrice(currentBidPrice, GetStepAmount(currentBidPrice));
private static decimal GetNextBidPrice(decimal currentBidPrice, decimal stepAmount) private static decimal GetNextBidPrice(decimal currentBidPrice, decimal stepAmount) => AuctionService.GetNextBidPrice(currentBidPrice, stepAmount);
{
return currentBidPrice + stepAmount;
}
private static bool IsValidRequestAuctionStatus(AuctionStatus newStatus, AuctionStatus oldStatus) private static bool IsValidRequestAuctionStatus(AuctionStatus newStatus, AuctionStatus oldStatus)
{ {

View File

@ -1,7 +1,6 @@
using Newtonsoft.Json; using Newtonsoft.Json;
using Nop.Plugin.Misc.AuctionPlugin.Domains.Dtos; using Nop.Plugin.Misc.AuctionPlugin.Domains.Dtos;
using Nop.Web.Framework.Models; using Nop.Web.Framework.Models;
using Nop.Web.Framework.Mvc.ModelBinding;
namespace Nop.Plugin.Misc.AuctionPlugin.Models namespace Nop.Plugin.Misc.AuctionPlugin.Models
{ {
@ -29,11 +28,14 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Models
public AuctionPublicInfoModel(AuctionDto auctionDto) : this() public AuctionPublicInfoModel(AuctionDto auctionDto) : this()
{ {
AuctionDto = auctionDto; AuctionDto = auctionDto;
FirstProductToAuction = AuctionDto.ProductToAuctionDtos.First();
StartDate = AuctionDto.StartDateUtc;
FirstProductToAuction = AuctionDto.ProductToAuctionDtos.FirstOrDefault();
if (FirstProductToAuction == null) return;
ProductId = FirstProductToAuction.ProductId; ProductId = FirstProductToAuction.ProductId;
ProductToAuctionMappingId = FirstProductToAuction.Id; ProductToAuctionMappingId = FirstProductToAuction.Id;
StartDate = AuctionDto.StartDateUtc;
IsActive = FirstProductToAuction.IsActiveItem; IsActive = FirstProductToAuction.IsActiveItem;
} }
} }

View File

@ -27,7 +27,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Models
public int AuctionId { get; set; } public int AuctionId { get; set; }
public bool AuctionClosed { get; set; } public bool AuctionClosed { get; set; }
public bool IsItemActive => FirstProductToAuction.IsActiveItem; public bool IsItemActive => FirstProductToAuction?.IsActiveItem ?? false;
public AuctionStatus AuctionStatus { get; set; } public AuctionStatus AuctionStatus { get; set; }
public int ProductId { get; set; } public int ProductId { get; set; }
@ -62,9 +62,11 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Models
public ProductBidBoxViewModel(AuctionDto auctionDto) : this() public ProductBidBoxViewModel(AuctionDto auctionDto) : this()
{ {
AuctionDto = auctionDto; AuctionDto = auctionDto;
FirstProductToAuction = AuctionDto.ProductToAuctionDtos.First();
AuctionId = auctionDto.Id; AuctionId = auctionDto.Id;
FirstProductToAuction = AuctionDto.ProductToAuctionDtos.FirstOrDefault();
if (FirstProductToAuction == null) return;
ProductId = FirstProductToAuction.ProductId; ProductId = FirstProductToAuction.ProductId;
ProductToAuctionId = FirstProductToAuction.Id; ProductToAuctionId = FirstProductToAuction.Id;
} }

View File

@ -95,6 +95,12 @@ public class AuctionService : IAuctionService
//200 000 000 Ft fölött 20 000 000 Ft-tal //200 000 000 Ft fölött 20 000 000 Ft-tal
} }
public static decimal GetNextBidPrice(decimal currentBidPrice) => GetNextBidPrice(currentBidPrice, GetStepAmount(currentBidPrice));
public static decimal GetNextBidPrice(decimal currentBidPrice, decimal stepAmount)
{
return currentBidPrice + stepAmount;
}
public async Task ResetProductToAuctionByProductId(int productId) public async Task ResetProductToAuctionByProductId(int productId)
=> await ResetProductToAuctionAsync(await _ctx.ProductToAuctions.GetByProductId(productId).FirstOrDefaultAsync()); => await ResetProductToAuctionAsync(await _ctx.ProductToAuctions.GetByProductId(productId).FirstOrDefaultAsync());