From 0dd529e36d0846925237fb515d08be8c68f0cc6f Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 18 Nov 2024 12:54:06 +0100 Subject: [PATCH] assign product to the auction --- .../Components/AuctionAdminViewComponent.cs | 20 ++- .../AuctionPluginAdminController.cs | 43 ++++-- .../Models/ProductAssignToAuctionViewModel.cs | 5 +- .../AdminProductAuctionSettingsBox.cshtml | 127 ++++++++++++++---- .../Services/AuctionService.cs | 27 +++- 5 files changed, 171 insertions(+), 51 deletions(-) diff --git a/Nop.Plugin.Misc.AuctionPlugin/Areas/Admin/Components/AuctionAdminViewComponent.cs b/Nop.Plugin.Misc.AuctionPlugin/Areas/Admin/Components/AuctionAdminViewComponent.cs index 4205501..2bc7e76 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Areas/Admin/Components/AuctionAdminViewComponent.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Areas/Admin/Components/AuctionAdminViewComponent.cs @@ -13,6 +13,8 @@ using Nop.Web.Areas.Admin.Models.Orders; using Nop.Web.Framework.Components; using Nop.Web.Framework.Factories; using Nop.Web.Framework.Infrastructure; +using Nop.Web.Models.Catalog; +using System.Linq.Expressions; namespace Nop.Plugin.Misc.AuctionPlugin.Areas.Admin.Components @@ -25,7 +27,8 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Areas.Admin.Components protected readonly IProductAttributeService _productAttributeService; protected readonly IWidgetPluginManager _widgetPluginManager; protected readonly AuctionSettings _auctionSettings; - protected readonly IAuctionService _auctionService; + protected readonly AuctionService _auctionService; + protected readonly MyProductModelFactory _productModelFactory; @@ -39,13 +42,15 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Areas.Admin.Components IProductAttributeService productAttributeService, IWidgetPluginManager widgetPluginManager, AuctionSettings auctionSettings, - IAuctionService auctionService) : base(widgetModelFactory) + AuctionService auctionService, + MyProductModelFactory productModelFactory) : base(widgetModelFactory) { _logger = logger; _productAttributeService = productAttributeService; _widgetPluginManager = widgetPluginManager; _auctionSettings = auctionSettings; _auctionService = auctionService; + _productModelFactory = productModelFactory; } #endregion @@ -83,6 +88,8 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Areas.Admin.Components } var product = (additionalData as ProductModel); + //ProductDetailsModel detailsModel = await _productModelFactory.PrepareProductDetailsModelAsync(product.Price); + productId = product.Id; var auctions = await _auctionService.GetAllAuctionsAsync() ?? new List(); @@ -90,9 +97,14 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Areas.Admin.Components // Assign the auctions to the ViewBag ViewBag.Auctions = auctions; ProductAssignToAuctionViewModel viewModel = new ProductAssignToAuctionViewModel(); - viewModel.StartingPrice = product.BasepriceAmount; + viewModel.ProductId = productId; + viewModel.StartingPrice = product.OldPrice; + viewModel.BidPrice = product.OldPrice; + //viewModel.InAuctions = await _auctionService.GetAllAuctionsAsync() + viewModel.InAuctions = []; + await _logger.InformationAsync($"Admin auction widget called from {widgetZone}! II. "); - return View("~/Plugins/Misc.AuctionPlugin/Areas/Admin/Views/AdminProductAuctionSettingsBox.cshtml", productId.ToString()); + return View("~/Plugins/Misc.AuctionPlugin/Areas/Admin/Views/AdminProductAuctionSettingsBox.cshtml", viewModel); } #endregion diff --git a/Nop.Plugin.Misc.AuctionPlugin/Areas/Admin/Controllers/AuctionPluginAdminController.cs b/Nop.Plugin.Misc.AuctionPlugin/Areas/Admin/Controllers/AuctionPluginAdminController.cs index cf07f91..a241597 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Areas/Admin/Controllers/AuctionPluginAdminController.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Areas/Admin/Controllers/AuctionPluginAdminController.cs @@ -11,22 +11,24 @@ using Nop.Services.Logging; using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities; using Nop.Plugin.Misc.AuctionPlugin.Services; using Nop.Web.Framework.Models.DataTables; +using Nop.Plugin.Misc.AuctionPlugin.Models; +using AyCode.Core.Extensions; namespace Nop.Plugin.Misc.AuctionPlugin.Areas.Admin.Controllers; -[AutoValidateAntiforgeryToken] -[AuthorizeAdmin] +//[AutoValidateAntiforgeryToken] +//[AuthorizeAdmin] [Area(AreaNames.ADMIN)] public class AuctionPluginAdminController : BasePluginController { //private readonly SignalRservice _signalRservice; - private readonly ILocalizationService _localizationService; - private readonly INotificationService _notificationService; - private readonly ISettingService _settingService; - private readonly AuctionSettings _auctionSettings; - private readonly AuctionService _auctionService; - private readonly ILogger _logger; - + protected readonly ILocalizationService _localizationService; + protected readonly INotificationService _notificationService; + protected readonly ISettingService _settingService; + protected readonly AuctionSettings _auctionSettings; + protected readonly AuctionService _auctionService; + protected readonly ILogger _logger; + //public AuctionPluginAdminController(SignalRservice signalRservice) public AuctionPluginAdminController(ILocalizationService localizationService, INotificationService notificationService, ISettingService settingService, AuctionSettings auctionSettings, ILogger logger, AuctionService auctionService) { @@ -155,16 +157,31 @@ public class AuctionPluginAdminController : BasePluginController return View("~/Plugins/Misc.AuctionPlugin/Areas/Admin/Views/TestPage.cshtml", model); } + + public async Task AssignProductToAuction() + { + _logger.Information("AssignProductToAuction has been called"); + + + + var result = await _auctionService.GetProductToAuctionsByAuctionIdAsync(1); + + + return Ok(result.ToJson()); + } [HttpPost] - public async Task AssignProductToAuction(int productId, int auctionId) + public async Task AssignProductToAuction([FromBody] AssignAuctionRequestModel model) { - if (productId <= 0 || auctionId <= 0) + _logger.Information("AssignProductToAuction has been called"); + + if (Convert.ToInt32(model.ProductId) <= 0 || Convert.ToInt32(model.AuctionId) <= 0) return BadRequest(); - var result = await _auctionService.AssignProductToAuctionAsync(productId, auctionId); + var result = await _auctionService.AssignProductToAuctionAsync(Convert.ToInt32(model.ProductId), + Convert.ToDecimal(model.StartingPrice), Convert.ToDecimal(model.BidPrice), Convert.ToInt32(model.AuctionId)); if (result) - return Ok(); + return Ok("Baaaazdmeeeeeeeeg"); return StatusCode(500, "Error assigning product to auction."); } diff --git a/Nop.Plugin.Misc.AuctionPlugin/Areas/Admin/Models/ProductAssignToAuctionViewModel.cs b/Nop.Plugin.Misc.AuctionPlugin/Areas/Admin/Models/ProductAssignToAuctionViewModel.cs index a38ab99..833116b 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Areas/Admin/Models/ProductAssignToAuctionViewModel.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Areas/Admin/Models/ProductAssignToAuctionViewModel.cs @@ -1,4 +1,5 @@ -using Nop.Web.Framework.Models; +using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities; +using Nop.Web.Framework.Models; using System; using System.Collections.Generic; using System.Linq; @@ -10,7 +11,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Areas.Admin.Models public record ProductAssignToAuctionViewModel : BaseNopModel { public int ProductId { get; set; } - public int AuctionId { get; set; } + public List InAuctions { get; set; } public decimal StartingPrice { get; set; } public decimal BidPrice { get; set; } public decimal BidAmount { get; set; } diff --git a/Nop.Plugin.Misc.AuctionPlugin/Areas/Admin/Views/AdminProductAuctionSettingsBox.cshtml b/Nop.Plugin.Misc.AuctionPlugin/Areas/Admin/Views/AdminProductAuctionSettingsBox.cshtml index 827ab92..95e6301 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Areas/Admin/Views/AdminProductAuctionSettingsBox.cshtml +++ b/Nop.Plugin.Misc.AuctionPlugin/Areas/Admin/Views/AdminProductAuctionSettingsBox.cshtml @@ -2,45 +2,116 @@

@T("Plugins.Misc.AuctionPlugin.AssignToAuction")

-
- - - @* *@ +
+

Assign Product to Auction

+ + + -
- - -
+ + - - + +
+ +
diff --git a/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs b/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs index 15d31a5..fa5d0dc 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs @@ -6,6 +6,7 @@ using Nop.Plugin.Misc.AuctionPlugin.Domains; using Nop.Plugin.Misc.AuctionPlugin.Domains.DataLayer; using Nop.Plugin.Misc.AuctionPlugin.Domains.Dtos; using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities; +using Nop.Services.Logging; namespace Nop.Plugin.Misc.AuctionPlugin.Services; @@ -36,6 +37,7 @@ public class AuctionService : IAuctionService protected readonly IShortTermCacheManager _shortTermCacheManager; protected readonly IStaticCacheManager _staticCacheManager; protected readonly IWorkContext _workContext; + protected readonly ILogger _logger; #endregion #region Ctor @@ -46,7 +48,14 @@ public class AuctionService : IAuctionService /// /// Short term cache manager /// Cache manager - public AuctionService(AuctionDbContext ctx, IRepository auctionRepository, IRepository productToAuctionRepository, IShortTermCacheManager shortTermCacheManager, IStaticCacheManager staticCacheManager, IWorkContext workContext) + public AuctionService( + AuctionDbContext ctx, + IRepository auctionRepository, + IRepository productToAuctionRepository, + IShortTermCacheManager shortTermCacheManager, + IStaticCacheManager staticCacheManager, + IWorkContext workContext, + ILogger logger) { _ctx = ctx; //_customerBidRepository = customerBidRepository; @@ -55,6 +64,7 @@ public class AuctionService : IAuctionService _shortTermCacheManager = shortTermCacheManager; _staticCacheManager = staticCacheManager; _workContext = workContext; + _logger = logger; } #endregion @@ -171,7 +181,7 @@ public class AuctionService : IAuctionService { return new AuctionBidDto(await _ctx.AuctionBids.GetByIdAsync(auctionBidId)); } - public async Task AssignProductToAuctionAsync(int productId, int auctionId) + public async Task AssignProductToAuctionAsync(int productId, decimal startingPrice, decimal bidPrice, int auctionId) { var auction = await _ctx.Auctions.GetByIdAsync(auctionId); if (auction == null) @@ -180,10 +190,19 @@ public class AuctionService : IAuctionService var mapping = new ProductToAuctionMapping { ProductId = productId, + StartingPrice = Convert.ToInt32(startingPrice), + BidPrice = Convert.ToInt32(bidPrice), + BidAmount = 0, + AuctionStatus = Domains.Enums.AuctionStatus.Active, AuctionId = auctionId }; - - await _productToAuctionRepository.InsertAsync(mapping); + try + { + await _ctx.ProductToAuctions.InsertAsync(mapping); + } + catch (Exception ex) { + await _logger.InformationAsync(ex.ToString()); + } return true; }