This commit is contained in:
Loretta 2024-11-19 10:10:50 +01:00
commit d7dffd1485
18 changed files with 278 additions and 41 deletions

View File

@ -118,8 +118,8 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Areas.Admin.Controllers
Data = new BidNotificationMessage
{
ProductName = viewModel.ProductName,
BidPrice = viewModel.BidPrice,
NextStepAmount = viewModel.NextStepAmount
BidPrice = viewModel.BidPrice.ToString(),
NextStepAmount = viewModel.NextStepAmount.ToString()
}
};
var jsonMessage = JsonConvert.SerializeObject(bid, Formatting.Indented,

View File

@ -8,9 +8,9 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Areas.Admin.Models
[NopResourceDisplayName("Name")]
public string ProductName { get; set; }
public int BidPrice { get; set; }
public decimal BidPrice { get; set; }
public int NextStepAmount { get; set; }
public decimal NextStepAmount { get; set; }
}

View File

@ -1,6 +1,9 @@
using Microsoft.AspNetCore.Mvc;
using Nop.Core;
using Nop.Plugin.Misc.AuctionPlugin.Domains.Dtos;
using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities;
using Nop.Plugin.Misc.AuctionPlugin.Models;
using Nop.Plugin.Misc.AuctionPlugin.Services;
using Nop.Services.Cms;
using Nop.Services.Common;
using Nop.Services.Logging;
@ -18,6 +21,7 @@ public class AuctionPublicViewComponent : NopViewComponent
protected readonly IGenericAttributeService _genericAttributeService;
protected readonly IWidgetPluginManager _widgetPluginManager;
protected readonly IWorkContext _workContext;
protected readonly AuctionService _auctionService;
protected readonly AuctionSettings _auctionSettings;
protected readonly ILogger _logger;
@ -29,6 +33,7 @@ public class AuctionPublicViewComponent : NopViewComponent
IGenericAttributeService genericAttributeService,
IWidgetPluginManager widgetPluginManager,
IWorkContext workContext,
AuctionService auctionService,
AuctionSettings auctionSettings,
ILogger logger)
{
@ -36,6 +41,7 @@ public class AuctionPublicViewComponent : NopViewComponent
_genericAttributeService = genericAttributeService;
_widgetPluginManager = widgetPluginManager;
_workContext = workContext;
_auctionService = auctionService;
_auctionSettings = auctionSettings;
_logger = logger;
}
@ -89,9 +95,42 @@ public class AuctionPublicViewComponent : NopViewComponent
}
await _logger.InformationAsync("WidgetViewComponent called II");
//is it under Auction?
List<ProductToAuctionMapping> auctionIds = await _auctionService.GetProductToAuctionsByProductIdAsync(productDetailsModel.Id);
int auctionId = 0;
if(auctionIds == null || auctionIds.Count == 0)
{
return Content(string.Empty);
}
if (auctionIds.Count > 1)
{
List<AuctionDto> openAuctionList = [];
//we have more auctions so we return the closest open
foreach (var auctionMapping in auctionIds)
{
var auction = await _auctionService.GetAuctionDtoByIdAsync(auctionMapping.AuctionId);
if (!auction.Closed)
{
openAuctionList.Add(auction);
}
}
//first auction that started or strats not earlier than yesterday
auctionId = openAuctionList.Where(x => x.StartDateUtc > DateTime.UtcNow.AddDays(-1)).OrderBy(x => x.StartDateUtc).FirstOrDefault().Id;
}
else
{
var valami = await _auctionService.GetAuctionDtoByIdAsync(auctionIds.FirstOrDefault().AuctionId);
auctionId = valami.Id;
}
productBidBoxViewModel.WidgetZone = widgetZone;
productBidBoxViewModel.BasePrice = productDetailsModel.ProductPrice.OldPriceValue;
productBidBoxViewModel.CurrentPrice = productDetailsModel.ProductPrice.PriceValue;
productBidBoxViewModel.AuctionId = auctionId;
productBidBoxViewModel.CustomerId = customer.Id;
productBidBoxViewModel.ProductId = productDetailsModel.Id;
productBidBoxViewModel.LicitStep = 50000; //add calculation

View File

@ -19,15 +19,30 @@ $(function () {
}, 100000);
});
}
connection.onclose(function () {
start();
});
window.sendMessageToServer = function (messageType, data) {
var messageWrapper = {
MessageType: messageType,
Data: data
};
connection.invoke("ReceiveMessageFromClient", messageWrapper)
.then(() => {
console.log("Message successfully sent to the server.");
})
.catch(err => {
console.error("Error sending message to the server:", err);
});
};
start();
});
// Function to send a message to the server

View File

@ -18,4 +18,5 @@ public class AuctionDbTable: MgDbTableBase<Auction>
{
return GetAllAsync(auctions => auctions.OrderByDescending(x => x.StartDateUtc), _ => default);
}
}

View File

@ -17,4 +17,9 @@ public class ProductToAuctionDbTable: MgDbTableBase<ProductToAuctionMapping>
{
return Table.Where(x => x.AuctionId == auctionId && x.ProductId == productId);
}
public IQueryable<ProductToAuctionMapping> GetByProductId(int productId)
{
return Table.Where(x => x.ProductId == productId);
}
}

View File

@ -10,7 +10,7 @@ public class AuctionBidDto : IAuctionBidDto
public int CustomerId { get; set; }
public int ProductId { get; set; }
public bool IsWinner { get; set; }
public int BidPrice { get; set; }
public decimal BidPrice { get; set; }
public AuctionBidDto()

View File

@ -11,5 +11,5 @@ public interface IAuctionBidDtoBase : IMgModelDtoBase
public int ProductId { get; set; }
public bool IsWinner { get; set; }
public int BidPrice { get; set; }
public decimal BidPrice { get; set; }
}

View File

@ -15,7 +15,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Domains.Entities
public int ProductId { get; set; }
public bool IsWinner { get; set; }
public int BidPrice { get; set; }
public decimal BidPrice { get; set; }
[SkipValuesOnUpdate]
public DateTime Created { get; set; }

View File

@ -2,29 +2,36 @@
using Microsoft.AspNetCore.SignalR;
using System.Threading.Tasks;
using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities.Interfaces;
using Nop.Plugin.Misc.AuctionPlugin.Services;
using Nop.Services.Logging;
using Nop.Plugin.Misc.AuctionPlugin.Hubs.Messages;
using Nop.Plugin.Misc.AuctionPlugin.Models;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using Nop.Web.Models.Media;
using Nop.Services.Catalog;
using Nop.Plugin.Misc.AuctionPlugin.Services;
using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities;
using Newtonsoft.Json.Linq;
namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
{
public class AuctionHub : Hub<IAuctionHubClient>
{
private readonly ILogger _logger;
private IAuctionService _auctionService;
protected readonly SignalRMessageHandler _signalRMessageHandler;
ILogger _logger;
//HubCallerContext _hubCallerContext;
public AuctionHub(IAuctionService auctionService, ILogger logger)
public AuctionHub(ILogger logger, SignalRMessageHandler signalRMessageHandler)
{
_logger = logger;
_auctionService = auctionService;
_logger = logger;
_signalRMessageHandler = signalRMessageHandler;
}
public override async Task OnConnectedAsync()
public override async Task OnConnectedAsync()
{
//await _logger.InformationAsync($"Caller connected: id{_hubCallerContext.ConnectionId}");
var userId = Context.ConnectionId;
await _logger.InformationAsync($"Caller connected with id: {userId}");
@ -33,12 +40,11 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
{
await _logger.InformationAsync($"Caller connected with name: {userName}");
}
await base.OnConnectedAsync();
}
public async Task ReceiveMessageFromClient(string message)
public async Task ReceiveRegularMessageFromClient(string message)
{
await _logger.InformationAsync(message);
// Broadcast the message received from the client to all clients
@ -47,15 +53,14 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
//await _signalRservice.TestHub();
}
//public async Task Send(string announcement)
//{
// await _logger.InformationAsync($" Hub Send method called with messgae: {announcement}");
// await Clients.All.SendAsync("send", announcement);
//}
public async Task SendPriceToUsers(string message)
public async Task ReceiveMessageFromClient(MessageWrapper message)
{
await Clients.All.SendPrice(message); //SendAsync(nameof(IAuctionHubClient.SendPrice), message);
// Log the message type and data
await _logger.InformationAsync($"Received message of type: {message.MessageType}");
await _signalRMessageHandler.HandleMessage(message);
}
}
}
}

View File

@ -9,7 +9,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs.Messages
public class BidNotificationMessage
{
public string ProductName { get; set; }
public int BidPrice { get; set; }
public int NextStepAmount { get; set; }
public string BidPrice { get; set; }
public string NextStepAmount { get; set; }
}
}

View File

@ -0,0 +1,125 @@
using Nop.Plugin.Misc.AuctionPlugin.Hubs.Messages;
using Newtonsoft.Json.Linq;
using Nop.Plugin.Misc.AuctionPlugin.Models;
using Nop.Plugin.Misc.AuctionPlugin.Services;
using Nop.Services.Catalog;
using Newtonsoft.Json;
using Nop.Services.Logging;
using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities;
using Newtonsoft.Json.Serialization;
using Microsoft.AspNetCore.SignalR;
namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
{
public class SignalRMessageHandler
{
protected readonly ILogger _logger;
protected readonly IProductService _productService;
protected readonly AuctionService _auctionService;
private IHubContext<AuctionHub> _hubContext;
public SignalRMessageHandler(ILogger logger, IProductService productService, AuctionService auctionService, IHubContext<AuctionHub> hubContext)
{
_logger = logger;
_productService = productService;
_auctionService = auctionService;
_hubContext = hubContext;
}
public async Task HandleMessage(MessageWrapper message)
{
switch (message.MessageType)
{
case "BidRequestMessage":
await HandleBidRequest(message.Data);
break;
// Add other message types here
default:
await _logger.ErrorAsync("Unknown message type");
break;
}
}
private async Task HandleBidRequest(object data)
{
AuctionBidRequest bidRequestMessage = new AuctionBidRequest();
try
{
var a = JsonConvert.DeserializeObject<AuctionBidRequest>(data.ToString());
//var b = (message.Data as JObject)?.ToObject<AuctionBidRequest>();
if (a == null)
{
await _logger.InformationAsync("Deserialization returned null. Message data might not match the expected structure.");
}
bidRequestMessage = a;
}
catch (Exception ex)
{
await _logger.ErrorAsync("Error during deserialization of AuctionBidRequest", ex);
}
//AuctionBidRequest bidRequestMessage = new AuctionBidRequest();
if (bidRequestMessage != null)
{
await _logger.InformationAsync($"Bid received: - Auction:{bidRequestMessage.AuctionId} Product: {bidRequestMessage.ProductId} - Bid: {bidRequestMessage.BidPrice} - Customer: {bidRequestMessage.CustomerId}");
//get product
var product = await _productService.GetProductByIdAsync(Convert.ToInt32(bidRequestMessage.ProductId));
if (product != null)
{
//validate the bidprice amount
//set new price
product.Price = Convert.ToDecimal(bidRequestMessage.BidPrice);
}
List<ProductToAuctionMapping> mapping = await _auctionService.GetProductToAuctionByAuctionIdAndProductIdAsync(Convert.ToInt32(bidRequestMessage.AuctionId), Convert.ToInt32(bidRequestMessage.ProductId));
AuctionBid auctionBid = new AuctionBid();
auctionBid.ProductId = Convert.ToInt32(bidRequestMessage.ProductId);
auctionBid.CustomerId = Convert.ToInt32(bidRequestMessage.CustomerId);
auctionBid.BidPrice = Convert.ToDecimal(bidRequestMessage.BidPrice);
auctionBid.ProductAuctionMappingId = mapping.FirstOrDefault().Id;
//save bid
try
{
var result = _auctionService.InsertBidAsync(auctionBid);
}
catch(Exception ex)
{
_logger.Error($"MessageHandling error: {ex.ToString()}");
}
//update product
await _productService.UpdateProductAsync(product);
// Optionally broadcast to all clients
var bid = new MessageWrapper
{
MessageType = "bidNotification",
Data = new BidNotificationMessage
{
ProductName = bidRequestMessage.ProductId,
BidPrice = bidRequestMessage.BidPrice,
NextStepAmount = "50000"
}
};
var jsonMessage = JsonConvert.SerializeObject(bid, Formatting.Indented,
new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }
);
await _hubContext.Clients.All.SendAsync("send", jsonMessage);
}
}
}
}

View File

@ -65,6 +65,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Infrastructure
services.AddScoped<AuctionDbContext>();
services.AddScoped<MyProductModelFactory>();
services.AddScoped<SignalRMessageHandler>();
}
/// <summary>

View File

@ -6,11 +6,12 @@ using System.Threading.Tasks;
namespace Nop.Plugin.Misc.AuctionPlugin.Models
{
internal class AuctionBidRequest
public class AuctionBidRequest
{
public decimal BidPrice { get; set; }
public int ProductId { get; set; }
public int CustomerId { get; set; }
public string AuctionId { get; set; }
public string BidPrice { get; set; }
public string ProductId { get; set; }
public string CustomerId { get; set; }
}
}

View File

@ -9,6 +9,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Models
{
public record ProductBidBoxViewModel: BaseNopModel
{
public int AuctionId { get; set; }
public int ProductId { get; set; }
public int CustomerId { get; set; }

View File

@ -50,11 +50,11 @@ public class AuctionService : IAuctionService
/// <param name="shortTermCacheManager">Short term cache manager</param>
/// <param name="staticCacheManager">Cache manager</param>
public AuctionService(
AuctionDbContext ctx,
AuctionDbContext ctx,
//IRepository<Auction> auctionRepository,
//IRepository<ProductToAuctionMapping> productToAuctionRepository,
IShortTermCacheManager shortTermCacheManager,
IStaticCacheManager staticCacheManager,
IShortTermCacheManager shortTermCacheManager,
IStaticCacheManager staticCacheManager,
IWorkContext workContext,
ILogger logger)
{
@ -201,7 +201,17 @@ public class AuctionService : IAuctionService
return auctionBid == null ? null : new AuctionBidDto(auctionBid);
}
public async Task<ProductToAuctionMapping> AssignProductToAuctionAsync(int productId, decimal startingPrice, decimal bidPrice, int auctionId)
public async Task<List<ProductToAuctionMapping>> GetProductToAuctionsByProductIdAsync(int productId)
{
return new List<ProductToAuctionMapping>(await _ctx.ProductToAuctions.GetByProductId(productId).ToListAsync());
}
public async Task<List<ProductToAuctionMapping>> GetProductToAuctionByAuctionIdAndProductIdAsync(int auctionId, int productId)
{
return new List<ProductToAuctionMapping>(await _ctx.ProductToAuctions.GetByAuctionAndProductId(auctionId, productId).ToListAsync());
}
public async Task<bool> AssignProductToAuctionAsync(int productId, decimal startingPrice, decimal bidPrice, int auctionId)
{
var auction = await GetAuctionDtoByIdAsync(auctionId);
if (auction == null)

View File

@ -35,8 +35,11 @@ public interface IAuctionService
Task<IList<Auction>> GetAllAuctionsAsync();
Task<List<ProductToAuctionMapping>> GetProductToAuctionsByAuctionIdAsync(int auctionId, bool onlyActiveItems = false);
Task<AuctionDto> GetAuctionDtoByIdAsync(int auctionId);
Task<AuctionDto> GetAuctionDtoWithProductByIdAsync(int auctionId, int productId);
Task<ProductToAuctionDto> GetProductToAuctionDtoByIdAsync(int productToAuctionId);
@ -44,4 +47,9 @@ public interface IAuctionService
Task<AuctionBidDto> GetAuctionBidDtoByIdAsync(int auctionBidId);
Task<ProductToAuctionMapping> AssignProductToAuctionAsync(int productId, decimal startingPrice, decimal bidPrice, int auctionId);
Task<List<ProductToAuctionMapping>> GetProductToAuctionsByProductIdAsync(int productId);
Task<List<ProductToAuctionMapping>> GetProductToAuctionByAuctionIdAndProductIdAsync(int auctionId, int productId);
Task<bool> AssignProductToAuctionAsync(int productId, decimal startingPrice, decimal bidPrice, int auctionId);
}

View File

@ -30,7 +30,9 @@
</button>
</div>
</div>
<button id="signalRBidButton" class="btn btn-success">
SignalRBidButton
</button>
<button id="testButton" class="btn btn-success">
TestButton
@ -40,11 +42,16 @@
</div>
<script>
$(document).ready(function () {
var pageViewModel = @Html.Raw(Json.Serialize(Model));
console.log(pageViewModel);
console.log(pageViewModel.WidgetZone);
console.log(typeof sendMessageToServer);
$('#bidButton').click(function () {
const bidPrice = $('#bidPrice').val();
@ -62,6 +69,7 @@
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({
auctionId: pageViewModel.AuctionId,
bidPrice: bidPrice,
customerId: dataObject.CustomerId,
productId: dataObject.ProductId
@ -75,6 +83,19 @@
}
});
});
$("#signalRBidButton").on("click", function () {
var bidMessage = {
AuctionId: pageViewModel.AuctionId.toFixed(),
BidPrice: pageViewModel.BidPrice.toFixed(2),
ProductId: pageViewModel.ProductId.toFixed(),
CustomerId: pageViewModel.CustomerId.toFixed()
};
sendMessageToServer("BidRequestMessage", bidMessage);
});
$('#testButton').click(function () {
$.ajax({
url: '/Auction/RefreshAuctionWidget', // Controller endpoint
@ -94,5 +115,10 @@
});
});
});
</script>