This commit is contained in:
Adam 2024-11-30 15:22:28 +01:00
commit 3a5fe7792f
6 changed files with 48 additions and 23 deletions

View File

@ -20,6 +20,7 @@ public partial class Auction: MgEntityBase, IAuction
public DateTime? EndDateUtc { get; set; }
public bool Closed { get; set; }
public int? CategoryId { get; set; }
[SkipValuesOnUpdate]
public DateTime Created { get; set; }

View File

@ -6,5 +6,5 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Domains.Entities.Interfaces;
public interface IAuction : IAuctionDtoBase, ITimeStampInfo //, ISoftRemoveEntityInt
{
public int? CategoryId { get; set; }
}

View File

@ -26,7 +26,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
//- ha bid-elt 1x is, kerüljön a watch-ba
//- DbTransaction-t vhogy megcsinánli!
public class SignalRMessageHandler(ILogger logger, IProductService productService, AuctionService auctionService, IHubContext<AuctionHub> hubContext, IWorkContext workContext, ICustomerService customerService)
public class SignalRMessageHandler(ILogger logger, IProductService productService, AuctionService auctionService, IHubContext<AuctionHub> hubContext, IWorkContext workContext, ICustomerService customerService, ICategoryService categoryService)
{
private readonly Mutex _handleMessageMutex = new();
@ -132,13 +132,6 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
{
await logger.InformationAsync($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); ProductToAuctionMappingId: {auctionProductStatusRequest.ProductToAuctionId}; Status: {auctionProductStatusRequest.AuctionStatus}({(int)auctionProductStatusRequest.AuctionStatus})", null, customer);
var auction = await auctionService.GetAuctionDtoByProductToAuctionIdAsync(auctionProductStatusRequest.ProductToAuctionId, false);
if (auction == null || auction.Closed)
{
logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); (auction == null || auction.Closed); Closed: {auction?.Closed}", null, customer);
return;
}
var productToAuction = await auctionService.GetProductToAuctionMappingByIdAsync(auctionProductStatusRequest.ProductToAuctionId);
if (productToAuction == null)
{
@ -146,13 +139,20 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
return;
}
var auction = await auctionService.GetAuctionByIdAsync(productToAuction.AuctionId);
if (auction == null || auction.Closed)
{
logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); (auction == null || auction.Closed); Closed: {auction?.Closed}", null, customer);
return;
}
if (!IsValidRequestAuctionStatus(auctionProductStatusRequest.AuctionStatus, productToAuction.AuctionStatus))
{
logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); RequestAuctionStatusIsValid() == false; newStatus: {auctionProductStatusRequest.AuctionStatus}; oldStatus: {productToAuction.AuctionStatus}", null, customer);
return;
}
if (auctionProductStatusRequest.AuctionStatus == AuctionStatus.None) await ResetProductToAuction(productToAuction);
if (auctionProductStatusRequest.AuctionStatus == AuctionStatus.None) await ResetProductToAuction(productToAuction, auction.CategoryId);
else
{
switch (auctionProductStatusRequest.AuctionStatus)
@ -207,8 +207,11 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
SenderId = customer.Id,
Data = new ProductToAuctionStatusNotification(await auctionService.GetAuctionDtoByProductToAuctionIdAsync(productToAuction.Id, true), bidsCount, "EMPTY").ToJson()
};
await hubContext.Clients.All.SendAsync("send", productToauctionChangedNotification.ToJson());
var isFeaturedProduct = productToAuction.IsActiveItem || productToAuction.AuctionStatus == AuctionStatus.Pause;
await UpdateProductCategoryIsFeaturedAsync(isFeaturedProduct, auction.CategoryId, productToAuction.ProductId);
}
catch (Exception ex)
{
@ -245,7 +248,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
var product = await auctionService.GetProductById(bidRequestMessage.ProductId);
if (product == null) return;
if (IsValidBidPrice(product.Price, activeProductAuction.CurrentPrice, bidRequestMessage.BidPrice, customer)) return;
if (!IsValidBidPrice(product.Price, activeProductAuction.CurrentPrice, bidRequestMessage.BidPrice, customer)) return;
var auctionBid = bidRequestMessage.CreateMainEntity();
auctionBid.ProductAuctionMappingId = activeProductAuction.Id;
@ -307,10 +310,34 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
await hubContext.Clients.All.SendAsync("send", bidMessage.ToJson());
}
private Task ResetProductToAuction(ProductToAuctionMapping productToAuction)
private async Task ResetProductToAuction(ProductToAuctionMapping productToAuction, int? categoryId = null)
{
productToAuction.AuctionStatus = AuctionStatus.None;
return auctionService.ResetProductToAuctionAsync(productToAuction, productToAuction.StartingPrice);
await auctionService.ResetProductToAuctionAsync(productToAuction, productToAuction.StartingPrice);
categoryId ??= (await auctionService.GetAuctionByIdAsync(productToAuction.AuctionId))?.CategoryId;
await UpdateProductCategoryIsFeaturedAsync(false, categoryId, productToAuction.ProductId);
}
private async Task UpdateProductCategoryIsFeaturedAsync(bool isFeatured, int? categoryId, int productId)
{
//Leszarjuk ha elszáll, az aukció menjen tovább... - J.
try
{
if (categoryId.GetValueOrDefault(0) == 0) return;
var productCategory = (await categoryService.GetProductCategoriesByProductIdAsync(productId)).FirstOrDefault(x => x.CategoryId == categoryId);
if (productCategory == null) return;
if (productCategory.IsFeaturedProduct == isFeatured) return;
productCategory.IsFeaturedProduct = isFeatured;
await categoryService.UpdateProductCategoryAsync(productCategory);
}
catch (Exception ex)
{
logger.Error($"SignalRMessageHandler.UpdateProductCategoryIsFeaturedAsync(); categoryId: {categoryId}; productId: {productId}; isFeatured: {isFeatured}", ex);
}
}
private bool IsValidBidPrice(decimal productPrice, decimal productToAuctionCurrentPrice, decimal bidRequestPrice, Customer customer = null)
@ -318,10 +345,10 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
if (productPrice >= bidRequestPrice || productToAuctionCurrentPrice >= bidRequestPrice || bidRequestPrice != GetNextBidPrice(productToAuctionCurrentPrice))
{
logger.Warning($"SignalRMessageHandler.IsValidBidPrice(); (productPrice >= bidRequestPrice || productToAuctionCurrentPrice >= bidRequestPrice || bidRequestPrice != GetNextBidPrice(productToAuctionCurrentPrice)); productPrice: {productPrice}; productToAuctionCurrentPrice: {productToAuctionCurrentPrice}; bidRequestPrice: {bidRequestPrice}", null, customer);
return true;
return false;
}
return false;
return true;
}
private static decimal GetStepAmount(decimal currentBidPrice) => AuctionService.GetStepAmount(currentBidPrice);

View File

@ -182,12 +182,6 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Update="Areas\Admin\Components\AuctionAdminViewComponent.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
</ItemGroup>
<!-- This target execute after "Build" target -->
<Target Name="NopTarget" AfterTargets="Build">
<MSBuild Projects="@(ClearPluginAssemblies)" Properties="PluginPath=$(MSBuildProjectDirectory)\$(OutDir)" Targets="NopClear" />

View File

@ -417,9 +417,11 @@ public class AuctionService(
return auctionDto;
}
public async Task<Auction> GetAuctionByIdAsync(int auctionId) => await ctx.Auctions.GetByIdAsync(auctionId);
public async Task<AuctionDto> GetAuctionDtoByIdAsync(int auctionId, bool widthProducts, bool activeProductOnly)
{
var auction = await ctx.Auctions.GetByIdAsync(auctionId);
var auction = await GetAuctionByIdAsync(auctionId);
if (auction == null) return null;
var auctionDto = new AuctionDto(auction);

View File

@ -45,6 +45,7 @@ public interface IAuctionService
Task<AuctionDto> GetAuctionDtoWithAuctionBids(int auctionId, bool activeProductOnly, int maxBidsCount);
Task<List<ProductToAuctionMapping>> GetProductToAuctionsByAuctionIdAsync(int auctionId, bool onlyActiveItems);
Task<Auction> GetAuctionByIdAsync(int auctionId);
Task<AuctionDto> GetAuctionDtoByIdAsync(int auctionId, bool widthProducts, bool activeProductOnly);
Task<List<ProductToAuctionDto>> GetProductToAuctionDtosByAuctionId(int auctionId, bool activeProductOnly);