diff --git a/Nop.Plugin.Misc.AuctionPlugin/Areas/Admin/Controllers/AuctionPluginAdminController.cs b/Nop.Plugin.Misc.AuctionPlugin/Areas/Admin/Controllers/AuctionPluginAdminController.cs index a241597..ed978e8 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Areas/Admin/Controllers/AuctionPluginAdminController.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Areas/Admin/Controllers/AuctionPluginAdminController.cs @@ -98,6 +98,7 @@ public class AuctionPluginAdminController : BasePluginController public async Task AuctionList() { await _logger.InformationAsync("AnnouncementList called!"); + var model = new AuctionViewModel(); return View("~/Plugins/Misc.AuctionPlugin/Areas/Admin/Views/AuctionList.cshtml", model); } @@ -112,6 +113,10 @@ public class AuctionPluginAdminController : BasePluginController objOfAuctionDomain.EndDateUtc = viewModel.EndDateUtc; objOfAuctionDomain.Closed = viewModel.Closed; objOfAuctionDomain.Created = DateTime.UtcNow; + + //TODO: ezt kell majd használni! - J. + //var objOfAuctionDomain = viewModel.AuctionDto.CreateMainEntity(); + await _auctionService.InsertAuctionAsync(objOfAuctionDomain); //if (viewModel.IsActive == true) @@ -162,10 +167,7 @@ public class AuctionPluginAdminController : BasePluginController { _logger.Information("AssignProductToAuction has been called"); - - var result = await _auctionService.GetProductToAuctionsByAuctionIdAsync(1); - return Ok(result.ToJson()); } @@ -180,10 +182,7 @@ public class AuctionPluginAdminController : BasePluginController 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("Baaaazdmeeeeeeeeg"); - return StatusCode(500, "Error assigning product to auction."); + return result != null ? Ok("Baaaazdmeeeeeeeeg") : StatusCode(500, "Error assigning product to auction."); } - } diff --git a/Nop.Plugin.Misc.AuctionPlugin/Areas/Admin/Models/AuctionViewModel.cs b/Nop.Plugin.Misc.AuctionPlugin/Areas/Admin/Models/AuctionViewModel.cs index 739bd91..1f85d54 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Areas/Admin/Models/AuctionViewModel.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Areas/Admin/Models/AuctionViewModel.cs @@ -1,4 +1,5 @@ -using Nop.Plugin.Misc.AuctionPlugin.Domains.Enums; +using Nop.Plugin.Misc.AuctionPlugin.Domains.Dtos; +using Nop.Plugin.Misc.AuctionPlugin.Domains.Enums; using Nop.Web.Framework.Models; using Nop.Web.Framework.Mvc.ModelBinding; @@ -8,6 +9,8 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Areas.Admin.Models { public string PageTitle { get; set; } + public AuctionDto AuctionDto { get; set; } + [NopResourceDisplayName("Name")] public string AuctionName { get; set; } public AuctionType AuctionType { 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 95e6301..a8bbc18 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Areas/Admin/Views/AdminProductAuctionSettingsBox.cshtml +++ b/Nop.Plugin.Misc.AuctionPlugin/Areas/Admin/Views/AdminProductAuctionSettingsBox.cshtml @@ -50,14 +50,16 @@ }; addAntiForgeryToken(postData); - console.log("postData: " + JSON.stringify(postData)); + var jsonData = JSON.stringify(postData); + console.log("postData: " + jsonData); + debugger; // Perform AJAX call $.ajax({ url: "/Admin/AuctionPluginAdmin/AssignProductToAuction", // Update to match your endpoint type: "POST", contentType: "application/json", - data: JSON.stringify(postData), + data: jsonData, traditional: true, success: function (response) { alert("Product successfully assigned to the auction!"); diff --git a/Nop.Plugin.Misc.AuctionPlugin/Domains/Dtos/AuctionBidDto.cs b/Nop.Plugin.Misc.AuctionPlugin/Domains/Dtos/AuctionBidDto.cs index 3d5862d..a5ba16c 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Domains/Dtos/AuctionBidDto.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Domains/Dtos/AuctionBidDto.cs @@ -19,6 +19,8 @@ public class AuctionBidDto : IAuctionBidDto public AuctionBidDto(AuctionBid auctionBid) { + if (auctionBid == null) return; + Id = auctionBid.Id; ProductAuctionMappingId = auctionBid.ProductAuctionMappingId; CustomerId = auctionBid.CustomerId; diff --git a/Nop.Plugin.Misc.AuctionPlugin/Domains/Dtos/AuctionDto.cs b/Nop.Plugin.Misc.AuctionPlugin/Domains/Dtos/AuctionDto.cs index a3f5017..827bb58 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Domains/Dtos/AuctionDto.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Domains/Dtos/AuctionDto.cs @@ -1,13 +1,17 @@ using Nop.Plugin.Misc.AuctionPlugin.Domains.Dtos.Interfaces; using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities; using Nop.Plugin.Misc.AuctionPlugin.Domains.Enums; +using Nop.Web.Framework.Mvc.ModelBinding; namespace Nop.Plugin.Misc.AuctionPlugin.Domains.Dtos; public class AuctionDto : IAuctionDto { public int Id { get; set; } + + [NopResourceDisplayName("Name")] public string AuctionName { get; set; } + public AuctionType AuctionType { get; set; } public DateTime StartDateUtc { get; set; } public DateTime? EndDateUtc { get; set; } @@ -21,6 +25,8 @@ public class AuctionDto : IAuctionDto public AuctionDto(Auction auction) { + if (auction == null) return; + Id = auction.Id; AuctionName = auction.AuctionName; AuctionType = auction.AuctionType; diff --git a/Nop.Plugin.Misc.AuctionPlugin/Domains/Dtos/ProductToAuctionDto.cs b/Nop.Plugin.Misc.AuctionPlugin/Domains/Dtos/ProductToAuctionDto.cs index 6eb16d7..f43c70b 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Domains/Dtos/ProductToAuctionDto.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Domains/Dtos/ProductToAuctionDto.cs @@ -24,6 +24,8 @@ public class ProductToAuctionDto : IProductToAuctionDto public ProductToAuctionDto(ProductToAuctionMapping productToAuction) { + if (productToAuction == null) return; + Id = productToAuction.Id; ProductId = productToAuction.ProductId; AuctionId = productToAuction.AuctionId; diff --git a/Nop.Plugin.Misc.AuctionPlugin/Domains/Enums/AuctionStatus.cs b/Nop.Plugin.Misc.AuctionPlugin/Domains/Enums/AuctionStatus.cs index 5374a6c..6d846aa 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Domains/Enums/AuctionStatus.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Domains/Enums/AuctionStatus.cs @@ -1,11 +1,22 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Domains.Enums; +//public enum AuctionStatus : byte +//{ +// None = 0, +// Active = 5, +// FirstWarning = 10, +// SecondWarning = 15, +// SoldOut = 20, +// NotSold = 25 +//} + +[Flags] public enum AuctionStatus : byte { None = 0, - Active = 5, - FirstWarning = 10, - SecondWarning = 15, - SoldOut = 20, - NotSold = 25 + Active = 1, + FirstWarning = 2, + SecondWarning = 4, + SoldOut = 8, + NotSold = 16 } \ No newline at end of file diff --git a/Nop.Plugin.Misc.AuctionPlugin/Hubs/SignalRMessageHandler.cs b/Nop.Plugin.Misc.AuctionPlugin/Hubs/SignalRMessageHandler.cs index 585751d..d76f3b9 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Hubs/SignalRMessageHandler.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Hubs/SignalRMessageHandler.cs @@ -30,6 +30,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs { switch (message.MessageType) { + //nameof(IAuctionHubClient.SendPrice) case "BidRequestMessage": await HandleBidRequest(message.Data); break; @@ -52,6 +53,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs { await _logger.InformationAsync("Deserialization returned null. Message data might not match the expected structure."); } + bidRequestMessage = a; } catch (Exception ex) @@ -60,66 +62,72 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs } //AuctionBidRequest bidRequestMessage = new AuctionBidRequest(); - if (bidRequestMessage != null) + try { - 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) + if (bidRequestMessage != null) { - //validate the bidprice amount + 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)); - //set new price - product.Price = Convert.ToDecimal(bidRequestMessage.BidPrice); - - } - - List 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 + if (product != null) { - ProductName = bidRequestMessage.ProductId, - BidPrice = bidRequestMessage.BidPrice, - NextStepAmount = "50000" - } - }; - var jsonMessage = JsonConvert.SerializeObject(bid, Formatting.Indented, - new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() } - ); + //validate the bidprice amount - await _hubContext.Clients.All.SendAsync("send", jsonMessage); + + //set new price + product.Price = Convert.ToDecimal(bidRequestMessage.BidPrice); + + } + + List 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 + { + await _auctionService.InsertBidAsync(auctionBid); + } + catch (Exception e) + { + _logger.Error($"MessageHandling InsertBidAsync error: {e.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); + } + } + catch (Exception ex) + { + _logger.Error($"MessageHandling error: {ex.ToString()}"); } - } } } diff --git a/Nop.Plugin.Misc.AuctionPlugin/Models/AssignAuctionRequestModel.cs b/Nop.Plugin.Misc.AuctionPlugin/Models/AssignAuctionRequestModel.cs index 5f074d4..8337603 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Models/AssignAuctionRequestModel.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Models/AssignAuctionRequestModel.cs @@ -3,11 +3,15 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Nop.Plugin.Misc.AuctionPlugin.Domains.Dtos; namespace Nop.Plugin.Misc.AuctionPlugin.Models { - public class AssignAuctionRequestModel + //TODO: Miért string minden property? - J. + public class AssignAuctionRequestModel //: AuctionDto { + //vagy az alábbi - J. + //public AuctionDto AuctionDto { get; set; } public string ProductId { get; set; } public string StartingPrice { get; set; } diff --git a/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs b/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs index 544c1a0..33f7792 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs @@ -162,25 +162,43 @@ public class AuctionService : IAuctionService #region Dtots public async Task GetAuctionDtoByIdAsync(int auctionId) { - return new AuctionDto(await _ctx.Auctions.GetByIdAsync(auctionId)); + var auction = await _ctx.Auctions.GetByIdAsync(auctionId); + return auction == null ? null : new AuctionDto(auction); } public async Task GetAuctionDtoWithProductByIdAsync(int auctionId, int productId) { - var auctionModel = new AuctionDto(await _ctx.Auctions.GetByIdAsync(auctionId)); - auctionModel.ProductToAuctionDtos.AddRange(await _ctx.ProductToAuctions.GetByAuctionAndProductId(auctionId, productId).Select(x => new ProductToAuctionDto(x)).ToListAsync()); + var auction = await _ctx.Auctions.GetByIdAsync(auctionId); + if (auction == null) return null; - return auctionModel; + var auctionDto = new AuctionDto(auction); + auctionDto.ProductToAuctionDtos.AddRange(await _ctx.ProductToAuctions.GetByAuctionAndProductId(auctionId, productId).Select(x => new ProductToAuctionDto(x)).ToListAsync()); + + return auctionDto; } public async Task GetProductToAuctionDtoByIdAsync(int productToAuctionId) { - return new ProductToAuctionDto(await _ctx.ProductToAuctions.GetByIdAsync(productToAuctionId)); + var productToAuction = await _ctx.ProductToAuctions.GetByIdAsync(productToAuctionId); + return productToAuction == null ? null : new ProductToAuctionDto(productToAuction); + } + + public async Task GetAuctionDtoByProductToAuctionIdAsync(int productToAuctionId, bool includeProductToAuctionDto = true) + { + var productTouctionDto = await GetProductToAuctionDtoByIdAsync(productToAuctionId); + if (productTouctionDto == null) return null; + + var auctionDto = await GetAuctionDtoByIdAsync(productTouctionDto.AuctionId); //Ez sosem lehet null! - J. + + if (includeProductToAuctionDto) auctionDto.ProductToAuctionDtos.Add(productTouctionDto); + return auctionDto; } public async Task GetAuctionBidDtoByIdAsync(int auctionBidId) { - return new AuctionBidDto(await _ctx.AuctionBids.GetByIdAsync(auctionBidId)); + var auctionBid = await _ctx.AuctionBids.GetByIdAsync(auctionBidId); + + return auctionBid == null ? null : new AuctionBidDto(auctionBid); } public async Task> GetProductToAuctionsByProductIdAsync(int productId) @@ -193,42 +211,35 @@ public class AuctionService : IAuctionService return new List(await _ctx.ProductToAuctions.GetByAuctionAndProductId(auctionId, productId).ToListAsync()); } - public async Task AssignProductToAuctionAsync(int productId, decimal startingPrice, decimal bidPrice, int auctionId) + public async Task AssignProductToAuctionAsync(int productId, decimal startingPrice, decimal bidPrice, int auctionId) { - var auction = await _ctx.Auctions.GetByIdAsync(auctionId); + var auction = await GetAuctionDtoByIdAsync(auctionId); if (auction == null) - return false; - //check if it is added already + return null; - var productToAuction = _ctx.ProductToAuctions.GetByAuctionAndProductId(auctionId, productId); - if (productToAuction == null) + var existedProductToAuction = (await GetProductToAuctionByAuctionIdAndProductIdAsync(auctionId, productId)).FirstOrDefault(); + if (existedProductToAuction != null) return existedProductToAuction; + + var mapping = new ProductToAuctionMapping { + ProductId = productId, + StartingPrice = startingPrice, + BidPrice = bidPrice, + ProductAmount = 0, + AuctionStatus = Domains.Enums.AuctionStatus.Active, //Ez miért Active alapból? - J. + AuctionId = auctionId + }; - var mapping = new ProductToAuctionMapping - { - ProductId = productId, - StartingPrice = startingPrice, - BidPrice = bidPrice, - ProductAmount = 0, - AuctionStatus = Domains.Enums.AuctionStatus.Active, - AuctionId = auctionId - }; - try - { - await _ctx.ProductToAuctions.InsertAsync(mapping); - } - catch (Exception ex) - { - await _logger.InformationAsync(ex.ToString()); - } - - return true; - } - else + try { - //already added - return false; + await _ctx.ProductToAuctions.InsertAsync(mapping); } + catch (Exception ex) + { + await _logger.InformationAsync(ex.ToString()); + } + + return mapping; } #endregion Dtos diff --git a/Nop.Plugin.Misc.AuctionPlugin/Services/IAuctionService.cs b/Nop.Plugin.Misc.AuctionPlugin/Services/IAuctionService.cs index 0411441..aa7ffb1 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Services/IAuctionService.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Services/IAuctionService.cs @@ -46,9 +46,8 @@ public interface IAuctionService Task GetAuctionBidDtoByIdAsync(int auctionBidId); + Task AssignProductToAuctionAsync(int productId, decimal startingPrice, decimal bidPrice, int auctionId); Task> GetProductToAuctionsByProductIdAsync(int productId); Task> GetProductToAuctionByAuctionIdAndProductIdAsync(int auctionId, int productId); - - Task AssignProductToAuctionAsync(int productId, decimal startingPrice, decimal bidPrice, int auctionId); } \ No newline at end of file