using AyCode.Core.Extensions; using Microsoft.AspNetCore.Mvc; using Nop.Core.Domain.Catalog; using Nop.Data; using Nop.Plugin.Misc.AuctionPlugin.Areas.Admin.Models; 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.Catalog; using Nop.Services.Logging; using Nop.Web.Areas.Admin.Factories; using Nop.Web.Framework.Controllers; using Nop.Web.Models.Catalog; namespace Nop.Plugin.Misc.AuctionPlugin.Controllers; public class AuctionController : BasePluginController { protected readonly AuctionService _auctionService; protected readonly ILogger _logger; protected readonly IProductService _productService; protected readonly MyProductModelFactory _productModelFactory; public AuctionController(AuctionService auctionService, ILogger logger, IProductService productService, MyProductModelFactory productModelFactory) { _auctionService = auctionService; _logger = logger; _productService = productService; _productModelFactory = productModelFactory; } // GET public IActionResult Index() { //var a = new Auction(); return View(); } [HttpPost] public async Task PlaceBid([FromBody] AuctionBid model) { await _logger.InformationAsync("PlaceBid called"); //if (model.BidPrice < CurrentPrice + LicitStep) //{ // return BadRequest("Bid price is too low."); //} // kéne valami visszajelzés await _auctionService.InsertBidAsync(model); bool bidSuccess = true; if (bidSuccess) { return Ok(new { message = "Your bid was successfully placed!" }); } else { return BadRequest("Failed to place bid. Please try again."); } } [HttpPost] public async Task RefreshAuctionWidget([FromBody] RefreshWidgetRequest request) { await _logger.InformationAsync($"Refresh auction widget called from {request.WidgetZone} with data of {request.ProductId}"); var product = await _productService.GetProductByIdAsync(request.ProductId); ProductDetailsModel detailsModel = await _productModelFactory.PrepareProductDetailsModelAsync(product); return ViewComponent("AuctionPublic", new { widgetZone = request.WidgetZone, additionalData = detailsModel }); } public async Task LiveScreen(int auctionId) { var auctionDto = await _auctionService.GetAuctionDtoByIdAsync(auctionId, true); //var auctionDto = _auctionService.GetAuctionDtoWithAuctionBids(int auctionId, bool activeProductOnly) //auctionDto.ProductToAuctionDtos.AddRange(productToAuctionDtoMappings); bool isAnyItemLive = auctionDto.ProductToAuctionDtos.Any(x => x.AuctionStatus == AuctionStatus.Active); var model = new LiveScreenViewModel(auctionDto); Product product; ProductDetailsModel productDetailsModel; ProductToAuctionDto activeMapping; int activeProductId = 0; int activeProductToAuctionId = 0; decimal basePrice = 0; decimal currentPrice = 0; decimal nextStep = 0; if (isAnyItemLive) { activeMapping = auctionDto.ProductToAuctionDtos.Where(x => x.AuctionStatus == AuctionStatus.Active).FirstOrDefault(); product = await _productService.GetProductByIdAsync(activeMapping.ProductId); activeProductId = activeMapping.ProductId; activeProductToAuctionId = activeMapping.Id; productDetailsModel = await _productModelFactory.PrepareProductDetailsModelAsync(product); basePrice = activeMapping.StartingPrice; currentPrice = activeMapping.CurrentPrice; nextStep = AuctionService.GetStepAmount(currentPrice); } else { activeMapping = null; product = null; activeProductId = 0; activeProductToAuctionId = 0; productDetailsModel = null; } //I need product data //create a more detailed model model.IsAnyItemActive = isAnyItemLive; model.AuctionId = auctionId; model.CurrentProductToAuction = activeMapping; model.ActiveProductId = activeProductId; model.ActiveProductToAuctionId = activeProductToAuctionId; model.ActiveProductDetails = productDetailsModel; model.BasePrice = basePrice; model.CurrentPrice = currentPrice; model.LicitStep = nextStep; model.BasePrice = basePrice; model.CurrentPrice = currentPrice; return View("~/Plugins/Misc.AuctionPlugin/Views/LiveScreen.cshtml", model); } }