137 lines
4.6 KiB
C#
137 lines
4.6 KiB
C#
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<IActionResult> 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<IActionResult> 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<IActionResult> LiveScreen(int auctionId)
|
|
{
|
|
|
|
var auctionDto = await _auctionService.GetAuctionDtoWithAuctionBids(auctionId, true);
|
|
var activeMapping = auctionDto?.ProductToAuctionDtos.MinBy(x => x.SortIndex);
|
|
var isAnyItemLive = activeMapping != null;
|
|
if (auctionDto == null)
|
|
{
|
|
return new RedirectResult("/", false);
|
|
}
|
|
|
|
var model = new LiveScreenViewModel(auctionDto);
|
|
Product product;
|
|
ProductDetailsModel productDetailsModel;
|
|
|
|
int activeProductId = 0;
|
|
int activeProductToAuctionId = 0;
|
|
decimal basePrice = 0;
|
|
decimal currentPrice = 0;
|
|
decimal nextStep = 0;
|
|
|
|
if (isAnyItemLive)
|
|
{
|
|
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);
|
|
}
|
|
} |