refactoring in progress v1.1...
This commit is contained in:
parent
583ed97539
commit
25b875224f
|
|
@ -15,13 +15,24 @@ public class AuctionBidDbTable : MgDbTableBase<AuctionBid>
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public IQueryable<AuctionBid> GetLastAuctionBidByProductToAuctionId(int productToAuctionId)
|
public IOrderedQueryable<AuctionBid> GetAllLastBidByAuctionId(int auctionId)
|
||||||
=> GetByProductToAuctionId(productToAuctionId).OrderByDescending(x => x.Id);
|
{
|
||||||
|
return GetAllByAuctionId(auctionId)
|
||||||
|
.OrderByDescending(x => x.Id)
|
||||||
|
.GroupBy(x => x.AuctionId, (_, bids) => bids.FirstOrDefault())
|
||||||
|
.OrderByDescending(percentGroup => percentGroup.Id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IOrderedQueryable<AuctionBid> GetLastAuctionBidByProductToAuctionId(int productToAuctionId)
|
||||||
|
=> GetAllByProductToAuctionId(productToAuctionId).OrderByDescending(x => x.Id);
|
||||||
|
|
||||||
public Task<int> GetBidsCountByProductToAuctionIdAsync(int productToAuctionId)
|
public Task<int> GetBidsCountByProductToAuctionIdAsync(int productToAuctionId)
|
||||||
=> Table.CountAsync(x => x.ProductAuctionMappingId == productToAuctionId);
|
=> Table.CountAsync(x => x.ProductAuctionMappingId == productToAuctionId);
|
||||||
|
|
||||||
public IQueryable<AuctionBid> GetByProductToAuctionId(int productToAuctionId)
|
public IQueryable<AuctionBid> GetAllByAuctionId(int auctionId)
|
||||||
|
=> Table.Where(x => x.AuctionId == auctionId);
|
||||||
|
|
||||||
|
public IQueryable<AuctionBid> GetAllByProductToAuctionId(int productToAuctionId)
|
||||||
=> Table.Where(x => x.ProductAuctionMappingId == productToAuctionId);
|
=> Table.Where(x => x.ProductAuctionMappingId == productToAuctionId);
|
||||||
|
|
||||||
public Task<bool> HasBidByProductToAuctionIdAsync(int productToAuctionId)
|
public Task<bool> HasBidByProductToAuctionIdAsync(int productToAuctionId)
|
||||||
|
|
|
||||||
|
|
@ -44,6 +44,10 @@ public class AuctionDbContext : MgDbContextBase, IAuctionDbSet<AuctionDbTable>,
|
||||||
// AuctionBids2 = _auctionBidRepository;
|
// AuctionBids2 = _auctionBidRepository;
|
||||||
//}
|
//}
|
||||||
|
|
||||||
|
public Task<List<AuctionBid>> GetAllLastBidByAuctionIdAsync(int auctionId) => AuctionBids.GetAllLastBidByAuctionId(auctionId).ToListAsync();
|
||||||
|
public Task<Dictionary<int, AuctionBid>> GetAllLastBidDictionaryByAuctionIdAsync(int auctionId)
|
||||||
|
=> AuctionBids.GetAllLastBidByAuctionId(auctionId).ToDictionaryAsync(x => x.ProductAuctionMappingId);
|
||||||
|
|
||||||
public async Task<List<ProductToAuctionMapping>> GetProductToAuctionsByProductIdAsync(int productId)
|
public async Task<List<ProductToAuctionMapping>> GetProductToAuctionsByProductIdAsync(int productId)
|
||||||
{
|
{
|
||||||
return [..await ProductToAuctions.GetByProductId(productId).ToListAsync()];
|
return [..await ProductToAuctions.GetByProductId(productId).ToListAsync()];
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Domains.Entities
|
||||||
{
|
{
|
||||||
public partial class AuctionBid : MgEntityBase, IAuctionBid
|
public partial class AuctionBid : MgEntityBase, IAuctionBid
|
||||||
{
|
{
|
||||||
|
public int AuctionId { get; set; }
|
||||||
public int ProductAuctionMappingId { get; set; }
|
public int ProductAuctionMappingId { get; set; }
|
||||||
|
|
||||||
public int CustomerId { get; set; }
|
public int CustomerId { get; set; }
|
||||||
|
|
|
||||||
|
|
@ -5,5 +5,5 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Domains.Entities.Interfaces;
|
||||||
|
|
||||||
public interface IAuctionBid : IAuctionBidDtoBase, ITimeStampInfo//, ISoftRemoveEntityInt
|
public interface IAuctionBid : IAuctionBidDtoBase, ITimeStampInfo//, ISoftRemoveEntityInt
|
||||||
{
|
{
|
||||||
|
public int AuctionId { get; set; }
|
||||||
}
|
}
|
||||||
|
|
@ -19,7 +19,7 @@ using Microsoft.IdentityModel.Tokens;
|
||||||
|
|
||||||
namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
|
namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
|
||||||
{
|
{
|
||||||
public class AuctionHub(SessionService sessionService, ILockService lockService, ILogger logger, IProductService productService, AuctionService auctionService, IWorkContext workContext, ICustomerService customerService, ICategoryService categoryService)
|
public class AuctionHub(SessionService sessionService, ILockService lockService, ILogger logger, IProductService productService, AuctionService auctionService, IWorkContext workContext, ICustomerService customerService)
|
||||||
: Hub<IAuctionHubClient>
|
: Hub<IAuctionHubClient>
|
||||||
{
|
{
|
||||||
public override async Task OnConnectedAsync()
|
public override async Task OnConnectedAsync()
|
||||||
|
|
@ -182,7 +182,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
|
||||||
|
|
||||||
var revertLastBid = await auctionService.RevertAuctionBidByProductToAuctionIdAsync(productToAuction.Id);
|
var revertLastBid = await auctionService.RevertAuctionBidByProductToAuctionIdAsync(productToAuction.Id);
|
||||||
|
|
||||||
if (revertLastBid == null) await ResetProductToAuction(productToAuction);
|
if (revertLastBid == null) await auctionService.ResetProductToAuction(productToAuction);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
productToAuction.BidsCount = await auctionService.GetBidsCountByProductToAuctionIdAsync(productToAuction.Id);
|
productToAuction.BidsCount = await auctionService.GetBidsCountByProductToAuctionIdAsync(productToAuction.Id);
|
||||||
|
|
@ -288,7 +288,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
|
||||||
//var responseType = ResponseType.ToAllClients;
|
//var responseType = ResponseType.ToAllClients;
|
||||||
await logger.InformationAsync($"AuctionHub.HandleProductToAuctionStatusChangedRequest(); ProductToAuctionMappingId: {auctionProductStatusRequest.ProductToAuctionId}; Status: {auctionProductStatusRequest.AuctionStatus}({(int)auctionProductStatusRequest.AuctionStatus})", null, customer);
|
await logger.InformationAsync($"AuctionHub.HandleProductToAuctionStatusChangedRequest(); ProductToAuctionMappingId: {auctionProductStatusRequest.ProductToAuctionId}; Status: {auctionProductStatusRequest.AuctionStatus}({(int)auctionProductStatusRequest.AuctionStatus})", null, customer);
|
||||||
|
|
||||||
var (auction, productToAuction, responseType) = await GetAndUpdateProductToAuctionStatusIfValidAsync(auctionProductStatusRequest.ProductToAuctionId, auctionProductStatusRequest.AuctionStatus, customer);
|
var (auction, productToAuction, responseType) = await auctionService.GetAndUpdateProductToAuctionStatusIfValidAsync(auctionProductStatusRequest.ProductToAuctionId, auctionProductStatusRequest.AuctionStatus, customer);
|
||||||
if (auction == null || productToAuction == null || responseType == ResponseType.None) return ResponseType.None;
|
if (auction == null || productToAuction == null || responseType == ResponseType.None) return ResponseType.None;
|
||||||
|
|
||||||
await SendStatusChangedNotificationAsync(customer, statusChangedMessageWrapper, productToAuction, responseType);
|
await SendStatusChangedNotificationAsync(customer, statusChangedMessageWrapper, productToAuction, responseType);
|
||||||
|
|
@ -307,85 +307,85 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
|
||||||
return ResponseType.Error;
|
return ResponseType.Error;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<ResponseType> UpdateProductToAuctionStatusIfValidAsync(AuctionStatus newProductToAuctionStatus, Auction auction, ProductToAuctionMapping productToAuction, Customer customer)
|
//private async Task<ResponseType> UpdateProductToAuctionStatusIfValidAsync(AuctionStatus newProductToAuctionStatus, Auction auction, ProductToAuctionMapping productToAuction, Customer customer)
|
||||||
{
|
//{
|
||||||
if (!IsValidRequestAuctionStatus(newProductToAuctionStatus, productToAuction.AuctionStatus))
|
// if (!IsValidRequestAuctionStatus(newProductToAuctionStatus, productToAuction.AuctionStatus))
|
||||||
{
|
// {
|
||||||
logger.Error($"AuctionHub.UpdateProductToAuctionStatusIfValidAsync(); RequestAuctionStatusIsValid() == false; newStatus: {newProductToAuctionStatus}; oldStatus: {productToAuction.AuctionStatus}", null, customer);
|
// logger.Error($"AuctionHub.UpdateProductToAuctionStatusIfValidAsync(); RequestAuctionStatusIsValid() == false; newStatus: {newProductToAuctionStatus}; oldStatus: {productToAuction.AuctionStatus}", null, customer);
|
||||||
|
|
||||||
return ResponseType.ToCaller;
|
// return ResponseType.ToCaller;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (newProductToAuctionStatus == AuctionStatus.None) await ResetProductToAuction(productToAuction, auction.CategoryId);
|
// if (newProductToAuctionStatus == AuctionStatus.None) await ResetProductToAuction(productToAuction, auction.CategoryId);
|
||||||
else
|
// else
|
||||||
{
|
// {
|
||||||
switch (newProductToAuctionStatus)
|
// switch (newProductToAuctionStatus)
|
||||||
{
|
// {
|
||||||
case AuctionStatus.Active:
|
// case AuctionStatus.Active:
|
||||||
productToAuction.AuctionStatus = AuctionStatus.Active;
|
// productToAuction.AuctionStatus = AuctionStatus.Active;
|
||||||
await UpdateProductCategoryIsFeaturedAsync(productToAuction.ProductId, auction.CategoryId, true);
|
// await UpdateProductCategoryIsFeaturedAsync(productToAuction.ProductId, auction.CategoryId, true);
|
||||||
break;
|
// break;
|
||||||
|
|
||||||
case AuctionStatus.FirstWarning:
|
// case AuctionStatus.FirstWarning:
|
||||||
case AuctionStatus.SecondWarning:
|
// case AuctionStatus.SecondWarning:
|
||||||
productToAuction.AuctionStatus = newProductToAuctionStatus;
|
// productToAuction.AuctionStatus = newProductToAuctionStatus;
|
||||||
break;
|
// break;
|
||||||
|
|
||||||
case AuctionStatus.Sold:
|
// case AuctionStatus.Sold:
|
||||||
var lastAuctionBid = await auctionService.GetLastAuctionBidByProductToAuctionIdAsync(productToAuction.Id);
|
// var lastAuctionBid = await auctionService.GetLastAuctionBidByProductToAuctionIdAsync(productToAuction.Id);
|
||||||
if (lastAuctionBid == null) productToAuction.AuctionStatus = AuctionStatus.NotSold;
|
// if (lastAuctionBid == null) productToAuction.AuctionStatus = AuctionStatus.NotSold;
|
||||||
else
|
// else
|
||||||
{
|
// {
|
||||||
productToAuction.AuctionStatus = AuctionStatus.Sold;
|
// productToAuction.AuctionStatus = AuctionStatus.Sold;
|
||||||
productToAuction.CurrentPrice = lastAuctionBid.BidPrice;
|
// productToAuction.CurrentPrice = lastAuctionBid.BidPrice;
|
||||||
productToAuction.WinnerCustomerId = lastAuctionBid.CustomerId;
|
// productToAuction.WinnerCustomerId = lastAuctionBid.CustomerId;
|
||||||
|
|
||||||
var placeOrderResult = await auctionService.CreateOrderForWinnerAsync(productToAuction);
|
// var placeOrderResult = await auctionService.CreateOrderForWinnerAsync(productToAuction);
|
||||||
if (placeOrderResult == null || placeOrderResult.Id == 0)
|
// if (placeOrderResult == null || placeOrderResult.Id == 0)
|
||||||
{
|
// {
|
||||||
logger.Error($"AuctionHub.UpdateProductToAuctionStatusIfValidAsync(); (placeOrderResult == null || placeOrderResult.Id == 0)", null, customer);
|
// logger.Error($"AuctionHub.UpdateProductToAuctionStatusIfValidAsync(); (placeOrderResult == null || placeOrderResult.Id == 0)", null, customer);
|
||||||
//return; //TODO: EGYELŐRE HAGYJUK LEZÁRNI AKKOR IS, HA NEM SIKERÜLT AZ ORDERPLACE()! - J.
|
// //return; //TODO: EGYELŐRE HAGYJUK LEZÁRNI AKKOR IS, HA NEM SIKERÜLT AZ ORDERPLACE()! - J.
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
await UpdateProductCategoryIsFeaturedAsync(productToAuction.ProductId, auction.CategoryId, false);
|
// await UpdateProductCategoryIsFeaturedAsync(productToAuction.ProductId, auction.CategoryId, false);
|
||||||
break;
|
// break;
|
||||||
|
|
||||||
case AuctionStatus.Pause:
|
// case AuctionStatus.Pause:
|
||||||
productToAuction.AuctionStatus = AuctionStatus.Pause;
|
// productToAuction.AuctionStatus = AuctionStatus.Pause;
|
||||||
break;
|
// break;
|
||||||
|
|
||||||
default:
|
// default:
|
||||||
logger.Error($"AuctionHub.UpdateProductToAuctionStatusIfValidAsync(); AuctionStatus not found; (newStatus == {newProductToAuctionStatus})", null, customer);
|
// logger.Error($"AuctionHub.UpdateProductToAuctionStatusIfValidAsync(); AuctionStatus not found; (newStatus == {newProductToAuctionStatus})", null, customer);
|
||||||
|
|
||||||
return ResponseType.ToCaller;
|
// return ResponseType.ToCaller;
|
||||||
}
|
// }
|
||||||
|
|
||||||
await auctionService.UpdateProductToAuctionMappingAsync(productToAuction);
|
// await auctionService.UpdateProductToAuctionMappingAsync(productToAuction);
|
||||||
}
|
// }
|
||||||
|
|
||||||
return ResponseType.ToAllClients;
|
// return ResponseType.ToAllClients;
|
||||||
}
|
//}
|
||||||
|
|
||||||
private async Task<(Auction auction, ProductToAuctionMapping productToAuction, ResponseType responseType)> GetAndUpdateProductToAuctionStatusIfValidAsync(int productToAuctionId, AuctionStatus newProductToAuctionStatus, Customer customer)
|
//private async Task<(Auction auction, ProductToAuctionMapping productToAuction, ResponseType responseType)> GetAndUpdateProductToAuctionStatusIfValidAsync(int productToAuctionId, AuctionStatus newProductToAuctionStatus, Customer customer)
|
||||||
{
|
//{
|
||||||
var productToAuction = await auctionService.GetProductToAuctionMappingByIdAsync(productToAuctionId);
|
// var productToAuction = await auctionService.GetProductToAuctionMappingByIdAsync(productToAuctionId);
|
||||||
if (productToAuction == null)
|
// if (productToAuction == null)
|
||||||
{
|
// {
|
||||||
logger.Error($"AuctionHub.GetAndUpdateProductToAuctionStatusIfValidAsync(); (productToAuction == null)", null, customer);
|
// logger.Error($"AuctionHub.GetAndUpdateProductToAuctionStatusIfValidAsync(); (productToAuction == null)", null, customer);
|
||||||
return (null, null, ResponseType.None);
|
// return (null, null, ResponseType.None);
|
||||||
}
|
// }
|
||||||
|
|
||||||
var auction = await auctionService.GetAuctionByIdAsync(productToAuction.AuctionId);
|
// var auction = await auctionService.GetAuctionByIdAsync(productToAuction.AuctionId);
|
||||||
if (auction == null || auction.Closed)
|
// if (auction == null || auction.Closed)
|
||||||
{
|
// {
|
||||||
logger.Error($"AuctionHub.GetAndUpdateProductToAuctionStatusIfValidAsync(); (auction == null || auction.Closed); Closed: {auction?.Closed}", null, customer);
|
// logger.Error($"AuctionHub.GetAndUpdateProductToAuctionStatusIfValidAsync(); (auction == null || auction.Closed); Closed: {auction?.Closed}", null, customer);
|
||||||
return (null, null, ResponseType.None);
|
// return (null, null, ResponseType.None);
|
||||||
}
|
// }
|
||||||
|
|
||||||
var responseType = await UpdateProductToAuctionStatusIfValidAsync(newProductToAuctionStatus, auction, productToAuction, customer);
|
// var responseType = await UpdateProductToAuctionStatusIfValidAsync(newProductToAuctionStatus, auction, productToAuction, customer);
|
||||||
return (auction, productToAuction, responseType);
|
// return (auction, productToAuction, responseType);
|
||||||
}
|
//}
|
||||||
|
|
||||||
private async Task SendStatusChangedNotificationAsync(Customer customer, MessageWrapper statusChangedMessageWrapper, ProductToAuctionMapping productToAuction, ResponseType responseType)
|
private async Task SendStatusChangedNotificationAsync(Customer customer, MessageWrapper statusChangedMessageWrapper, ProductToAuctionMapping productToAuction, ResponseType responseType)
|
||||||
{
|
{
|
||||||
|
|
@ -506,35 +506,35 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
|
||||||
}.ToJson();
|
}.ToJson();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task ResetProductToAuction(ProductToAuctionMapping productToAuction, int? categoryId = null)
|
//private async Task ResetProductToAuction(ProductToAuctionMapping productToAuction, int? categoryId = null)
|
||||||
{
|
//{
|
||||||
productToAuction.AuctionStatus = AuctionStatus.None;
|
// productToAuction.AuctionStatus = AuctionStatus.None;
|
||||||
await auctionService.ResetProductToAuctionAsync(productToAuction, productToAuction.StartingPrice);
|
// await auctionService.ResetProductToAuctionAsync(productToAuction, productToAuction.StartingPrice);
|
||||||
|
|
||||||
categoryId ??= (await auctionService.GetAuctionByIdAsync(productToAuction.AuctionId))?.CategoryId;
|
// categoryId ??= (await auctionService.GetAuctionByIdAsync(productToAuction.AuctionId))?.CategoryId;
|
||||||
await UpdateProductCategoryIsFeaturedAsync(productToAuction.ProductId, categoryId, false);
|
// await UpdateProductCategoryIsFeaturedAsync(productToAuction.ProductId, categoryId, false);
|
||||||
}
|
//}
|
||||||
|
|
||||||
private async Task UpdateProductCategoryIsFeaturedAsync(int productId, int? categoryId, bool isFeatured)
|
//private async Task UpdateProductCategoryIsFeaturedAsync(int productId, int? categoryId, bool isFeatured)
|
||||||
{
|
//{
|
||||||
//Leszarjuk ha elszáll, az aukció menjen tovább... - J.
|
// //Leszarjuk ha elszáll, az aukció menjen tovább... - J.
|
||||||
try
|
// try
|
||||||
{
|
// {
|
||||||
if (categoryId.GetValueOrDefault(0) == 0) return;
|
// if (categoryId.GetValueOrDefault(0) == 0) return;
|
||||||
|
|
||||||
var productCategory = (await categoryService.GetProductCategoriesByProductIdAsync(productId)).FirstOrDefault(x => x.CategoryId == categoryId);
|
// var productCategory = (await categoryService.GetProductCategoriesByProductIdAsync(productId)).FirstOrDefault(x => x.CategoryId == categoryId);
|
||||||
if (productCategory == null) return;
|
// if (productCategory == null) return;
|
||||||
|
|
||||||
if (productCategory.IsFeaturedProduct == isFeatured) return;
|
// if (productCategory.IsFeaturedProduct == isFeatured) return;
|
||||||
|
|
||||||
productCategory.IsFeaturedProduct = isFeatured;
|
// productCategory.IsFeaturedProduct = isFeatured;
|
||||||
await categoryService.UpdateProductCategoryAsync(productCategory);
|
// await categoryService.UpdateProductCategoryAsync(productCategory);
|
||||||
}
|
// }
|
||||||
catch (Exception ex)
|
// catch (Exception ex)
|
||||||
{
|
// {
|
||||||
logger.Error($"AuctionHub.UpdateProductCategoryIsFeaturedAsync(); categoryId: {categoryId}; productId: {productId}; isFeatured: {isFeatured}", ex);
|
// logger.Error($"AuctionHub.UpdateProductCategoryIsFeaturedAsync(); categoryId: {categoryId}; productId: {productId}; isFeatured: {isFeatured}", ex);
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
|
||||||
public async Task SendAuctionBidMessageAsync(MessageWrapper messageWrapper, ProductToAuctionMapping productToAuction, Product product, int customerId, ResponseType responseType)
|
public async Task SendAuctionBidMessageAsync(MessageWrapper messageWrapper, ProductToAuctionMapping productToAuction, Product product, int customerId, ResponseType responseType)
|
||||||
{
|
{
|
||||||
|
|
@ -559,6 +559,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
|
||||||
if (messageWrapper.HideToaster) await Clients.All.send(messageWrapperJson);
|
if (messageWrapper.HideToaster) await Clients.All.send(messageWrapperJson);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
//messageWrapperJObject["senderId" /*nameof(MessageWrapper.SenderId)*/] = "-1";
|
||||||
await Clients.Others.send(messageWrapperJson);
|
await Clients.Others.send(messageWrapperJson);
|
||||||
|
|
||||||
var messageWrapperJObject = JObject.Parse(messageWrapperJson);
|
var messageWrapperJObject = JObject.Parse(messageWrapperJson);
|
||||||
|
|
@ -584,26 +585,26 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
|
||||||
// else await Clients.All.send(messageWrapper.ToJson());
|
// else await Clients.All.send(messageWrapper.ToJson());
|
||||||
//}
|
//}
|
||||||
|
|
||||||
private static bool IsValidRequestAuctionStatus(AuctionStatus newStatus, AuctionStatus oldStatus)
|
//private static bool IsValidRequestAuctionStatus(AuctionStatus newStatus, AuctionStatus oldStatus)
|
||||||
{
|
//{
|
||||||
switch (oldStatus)
|
// switch (oldStatus)
|
||||||
{
|
// {
|
||||||
case AuctionStatus.None:
|
// case AuctionStatus.None:
|
||||||
return newStatus is AuctionStatus.Active or AuctionStatus.Pause;
|
// return newStatus is AuctionStatus.Active or AuctionStatus.Pause;
|
||||||
case AuctionStatus.Active:
|
// case AuctionStatus.Active:
|
||||||
return newStatus is AuctionStatus.FirstWarning or AuctionStatus.Pause;
|
// return newStatus is AuctionStatus.FirstWarning or AuctionStatus.Pause;
|
||||||
case AuctionStatus.FirstWarning:
|
// case AuctionStatus.FirstWarning:
|
||||||
return newStatus is AuctionStatus.SecondWarning or AuctionStatus.Pause;
|
// return newStatus is AuctionStatus.SecondWarning or AuctionStatus.Pause;
|
||||||
case AuctionStatus.SecondWarning:
|
// case AuctionStatus.SecondWarning:
|
||||||
return newStatus is AuctionStatus.Sold or AuctionStatus.Pause;
|
// return newStatus is AuctionStatus.Sold or AuctionStatus.Pause;
|
||||||
case AuctionStatus.Pause:
|
// case AuctionStatus.Pause:
|
||||||
return newStatus is AuctionStatus.None or AuctionStatus.Active;
|
// return newStatus is AuctionStatus.None or AuctionStatus.Active;
|
||||||
case AuctionStatus.Sold:
|
// case AuctionStatus.Sold:
|
||||||
case AuctionStatus.NotSold:
|
// case AuctionStatus.NotSold:
|
||||||
default:
|
// default:
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
|
||||||
private bool TryGetCurrentSessionItem(out SessionItem sessionItem)
|
private bool TryGetCurrentSessionItem(out SessionItem sessionItem)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -70,9 +70,17 @@ public class AuctionBackgroundService : MgBackgroundServiceBase
|
||||||
var auctionDto = new AuctionDto(auction);
|
var auctionDto = new AuctionDto(auction);
|
||||||
var productToAuctions = (await _auctionService.GetProductToAuctionsByAuctionIdAsync(auction.Id, false)).Where(x => x.AuctionStatus == AuctionStatus.None).ToList();
|
var productToAuctions = (await _auctionService.GetProductToAuctionsByAuctionIdAsync(auction.Id, false)).Where(x => x.AuctionStatus == AuctionStatus.None).ToList();
|
||||||
|
|
||||||
|
if (productToAuctions.Count == 0) continue;
|
||||||
|
|
||||||
|
var allLastBidbyPtaId = await _auctionService.GetAllLastBidDictionaryByAuctionIdAsync(auction.Id);
|
||||||
|
|
||||||
foreach (var productToAuction in productToAuctions)
|
foreach (var productToAuction in productToAuctions)
|
||||||
{
|
{
|
||||||
productToAuction.AuctionStatus = AuctionStatus.Active;
|
allLastBidbyPtaId.TryGetValue(productToAuction.Id, out var lastBid);
|
||||||
|
|
||||||
|
var responseType = await _auctionService.UpdateProductToAuctionStatusIfValidAsync(AuctionStatus.Active, auction, productToAuction, _auctionSystemCustomer, false);
|
||||||
|
|
||||||
|
if (responseType != ResponseType.None)
|
||||||
auctionDto.ProductToAuctionDtos.Add(new ProductToAuctionDto(productToAuction));
|
auctionDto.ProductToAuctionDtos.Add(new ProductToAuctionDto(productToAuction));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ using Nop.Services.Common;
|
||||||
using Nop.Services.Customers;
|
using Nop.Services.Customers;
|
||||||
using Nop.Services.Shipping;
|
using Nop.Services.Shipping;
|
||||||
using NUglify.Helpers;
|
using NUglify.Helpers;
|
||||||
|
using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities.Interfaces;
|
||||||
|
|
||||||
namespace Nop.Plugin.Misc.AuctionPlugin.Services;
|
namespace Nop.Plugin.Misc.AuctionPlugin.Services;
|
||||||
|
|
||||||
|
|
@ -39,8 +40,144 @@ public class AuctionService(
|
||||||
IAddressService addressService,
|
IAddressService addressService,
|
||||||
ICustomerService customerService,
|
ICustomerService customerService,
|
||||||
IOrderService orderService,
|
IOrderService orderService,
|
||||||
ILogger logger) : IAuctionService
|
ILogger logger,
|
||||||
|
ICategoryService categoryService) : IAuctionService
|
||||||
{
|
{
|
||||||
|
#region AuctionHub
|
||||||
|
|
||||||
|
public async Task<ResponseType> UpdateProductToAuctionStatusIfValidAsync(AuctionStatus newProductToAuctionStatus, Auction auction, ProductToAuctionMapping productToAuction, Customer customer, bool updateInDatabase = true)
|
||||||
|
{
|
||||||
|
if (!IsValidRequestAuctionStatus(newProductToAuctionStatus, productToAuction.AuctionStatus))
|
||||||
|
{
|
||||||
|
logger.Error($"AuctionHub.UpdateProductToAuctionStatusIfValidAsync(); RequestAuctionStatusIsValid() == false; newStatus: {newProductToAuctionStatus}; oldStatus: {productToAuction.AuctionStatus}", null, customer);
|
||||||
|
|
||||||
|
return ResponseType.ToCaller;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (newProductToAuctionStatus == AuctionStatus.None) await ResetProductToAuction(productToAuction, auction.CategoryId);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
switch (newProductToAuctionStatus)
|
||||||
|
{
|
||||||
|
case AuctionStatus.Active:
|
||||||
|
productToAuction.AuctionStatus = AuctionStatus.Active;
|
||||||
|
await UpdateProductCategoryIsFeaturedAsync(productToAuction.ProductId, auction.CategoryId, true);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case AuctionStatus.FirstWarning:
|
||||||
|
case AuctionStatus.SecondWarning:
|
||||||
|
productToAuction.AuctionStatus = newProductToAuctionStatus;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case AuctionStatus.Sold:
|
||||||
|
var lastAuctionBid = await GetLastAuctionBidByProductToAuctionIdAsync(productToAuction.Id);
|
||||||
|
if (lastAuctionBid == null) productToAuction.AuctionStatus = AuctionStatus.NotSold;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
productToAuction.AuctionStatus = AuctionStatus.Sold;
|
||||||
|
productToAuction.CurrentPrice = lastAuctionBid.BidPrice;
|
||||||
|
productToAuction.WinnerCustomerId = lastAuctionBid.CustomerId;
|
||||||
|
|
||||||
|
var placeOrderResult = await CreateOrderForWinnerAsync(productToAuction);
|
||||||
|
if (placeOrderResult == null || placeOrderResult.Id == 0)
|
||||||
|
{
|
||||||
|
logger.Error($"AuctionHub.UpdateProductToAuctionStatusIfValidAsync(); (placeOrderResult == null || placeOrderResult.Id == 0)", null, customer);
|
||||||
|
//return; //TODO: EGYELŐRE HAGYJUK LEZÁRNI AKKOR IS, HA NEM SIKERÜLT AZ ORDERPLACE()! - J.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await UpdateProductCategoryIsFeaturedAsync(productToAuction.ProductId, auction.CategoryId, false);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case AuctionStatus.Pause:
|
||||||
|
productToAuction.AuctionStatus = AuctionStatus.Pause;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
logger.Error($"AuctionHub.UpdateProductToAuctionStatusIfValidAsync(); AuctionStatus not found; (newStatus == {newProductToAuctionStatus})", null, customer);
|
||||||
|
|
||||||
|
return ResponseType.ToCaller;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (updateInDatabase)
|
||||||
|
await UpdateProductToAuctionMappingAsync(productToAuction);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ResponseType.ToAllClients;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<(Auction auction, ProductToAuctionMapping productToAuction, ResponseType responseType)> GetAndUpdateProductToAuctionStatusIfValidAsync(int productToAuctionId, AuctionStatus newProductToAuctionStatus, Customer customer)
|
||||||
|
{
|
||||||
|
var productToAuction = await GetProductToAuctionMappingByIdAsync(productToAuctionId);
|
||||||
|
if (productToAuction == null)
|
||||||
|
{
|
||||||
|
logger.Error($"AuctionHub.GetAndUpdateProductToAuctionStatusIfValidAsync(); (productToAuction == null)", null, customer);
|
||||||
|
return (null, null, ResponseType.None);
|
||||||
|
}
|
||||||
|
|
||||||
|
var auction = await GetAuctionByIdAsync(productToAuction.AuctionId);
|
||||||
|
if (auction == null || auction.Closed)
|
||||||
|
{
|
||||||
|
logger.Error($"AuctionHub.GetAndUpdateProductToAuctionStatusIfValidAsync(); (auction == null || auction.Closed); Closed: {auction?.Closed}", null, customer);
|
||||||
|
return (null, null, ResponseType.None);
|
||||||
|
}
|
||||||
|
|
||||||
|
var responseType = await UpdateProductToAuctionStatusIfValidAsync(newProductToAuctionStatus, auction, productToAuction, customer);
|
||||||
|
return (auction, productToAuction, responseType);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task ResetProductToAuction(ProductToAuctionMapping productToAuction, int? categoryId = null)
|
||||||
|
{
|
||||||
|
productToAuction.AuctionStatus = AuctionStatus.None;
|
||||||
|
await ResetProductToAuctionAsync(productToAuction, productToAuction.StartingPrice);
|
||||||
|
|
||||||
|
categoryId ??= (await GetAuctionByIdAsync(productToAuction.AuctionId))?.CategoryId;
|
||||||
|
await UpdateProductCategoryIsFeaturedAsync(productToAuction.ProductId, categoryId, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task UpdateProductCategoryIsFeaturedAsync(int productId, int? categoryId, bool isFeatured)
|
||||||
|
{
|
||||||
|
//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($"AuctionHub.UpdateProductCategoryIsFeaturedAsync(); categoryId: {categoryId}; productId: {productId}; isFeatured: {isFeatured}", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsValidRequestAuctionStatus(AuctionStatus newStatus, AuctionStatus oldStatus)
|
||||||
|
{
|
||||||
|
switch (oldStatus)
|
||||||
|
{
|
||||||
|
case AuctionStatus.None:
|
||||||
|
return newStatus is AuctionStatus.Active or AuctionStatus.Pause;
|
||||||
|
case AuctionStatus.Active:
|
||||||
|
return newStatus is AuctionStatus.FirstWarning or AuctionStatus.Pause;
|
||||||
|
case AuctionStatus.FirstWarning:
|
||||||
|
return newStatus is AuctionStatus.SecondWarning or AuctionStatus.Pause;
|
||||||
|
case AuctionStatus.SecondWarning:
|
||||||
|
return newStatus is AuctionStatus.Sold or AuctionStatus.Pause;
|
||||||
|
case AuctionStatus.Pause:
|
||||||
|
return newStatus is AuctionStatus.None or AuctionStatus.Active;
|
||||||
|
case AuctionStatus.Sold:
|
||||||
|
case AuctionStatus.NotSold:
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
|
|
||||||
public static decimal GetStepAmount(decimal currentPrice)
|
public static decimal GetStepAmount(decimal currentPrice)
|
||||||
|
|
@ -103,6 +240,12 @@ public class AuctionService(
|
||||||
public Task ResetProductToAuctionAsync(ProductToAuctionMapping productToAuction, decimal basePrice = 0)
|
public Task ResetProductToAuctionAsync(ProductToAuctionMapping productToAuction, decimal basePrice = 0)
|
||||||
=> ctx.ResetProductToAuctionAsync(productToAuction, basePrice);
|
=> ctx.ResetProductToAuctionAsync(productToAuction, basePrice);
|
||||||
|
|
||||||
|
public Task<List<AuctionBid>> GetAllLastBidByAuctionIdAsync(int auctionId)
|
||||||
|
=> ctx.GetAllLastBidByAuctionIdAsync(auctionId);
|
||||||
|
|
||||||
|
public Task<Dictionary<int, AuctionBid>> GetAllLastBidDictionaryByAuctionIdAsync(int auctionId)
|
||||||
|
=> ctx.GetAllLastBidDictionaryByAuctionIdAsync(auctionId);
|
||||||
|
|
||||||
public Task<AuctionBid> GetLastAuctionBidByProductToAuctionIdAsync(int productToAuctionId)
|
public Task<AuctionBid> GetLastAuctionBidByProductToAuctionIdAsync(int productToAuctionId)
|
||||||
=> ctx.GetLastAuctionBidByProductToAuctionId(productToAuctionId);
|
=> ctx.GetLastAuctionBidByProductToAuctionId(productToAuctionId);
|
||||||
|
|
||||||
|
|
@ -386,6 +529,7 @@ public class AuctionService(
|
||||||
}
|
}
|
||||||
|
|
||||||
#region auctions
|
#region auctions
|
||||||
|
|
||||||
public async Task InsertAuctionAsync(Auction auction)
|
public async Task InsertAuctionAsync(Auction auction)
|
||||||
{
|
{
|
||||||
await ctx.Auctions.InsertAsync(auction, false);
|
await ctx.Auctions.InsertAsync(auction, false);
|
||||||
|
|
@ -409,6 +553,7 @@ public class AuctionService(
|
||||||
=> ctx.ProductToAuctions.GetNotClosedItemsByAuctionId(auctionId).OrderBy(x => x.SortIndex).ToListAsync();
|
=> ctx.ProductToAuctions.GetNotClosedItemsByAuctionId(auctionId).OrderBy(x => x.SortIndex).ToListAsync();
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Dtos
|
#region Dtos
|
||||||
|
|
@ -420,7 +565,7 @@ public class AuctionService(
|
||||||
|
|
||||||
foreach (var auctionDtoProductToAuctionDto in auctionDto.ProductToAuctionDtos)
|
foreach (var auctionDtoProductToAuctionDto in auctionDto.ProductToAuctionDtos)
|
||||||
{
|
{
|
||||||
auctionDtoProductToAuctionDto.AuctionBidDtos.AddRange(await ctx.AuctionBids.GetByProductToAuctionId(auctionDtoProductToAuctionDto.Id).OrderByDescending(x => x.Id).Take(maxBidsCount).Select(x => new AuctionBidDto(x)).ToListAsync());
|
auctionDtoProductToAuctionDto.AuctionBidDtos.AddRange(await ctx.AuctionBids.GetAllByProductToAuctionId(auctionDtoProductToAuctionDto.Id).OrderByDescending(x => x.Id).Take(maxBidsCount).Select(x => new AuctionBidDto(x)).ToListAsync());
|
||||||
}
|
}
|
||||||
|
|
||||||
return auctionDto;
|
return auctionDto;
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,11 @@
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Nop.Core;
|
using Nop.Core;
|
||||||
|
using Nop.Core.Domain.Customers;
|
||||||
using Nop.Core.Domain.Orders;
|
using Nop.Core.Domain.Orders;
|
||||||
using Nop.Plugin.Misc.AuctionPlugin.Domains;
|
using Nop.Plugin.Misc.AuctionPlugin.Domains;
|
||||||
using Nop.Plugin.Misc.AuctionPlugin.Domains.Dtos;
|
using Nop.Plugin.Misc.AuctionPlugin.Domains.Dtos;
|
||||||
using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities;
|
using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities;
|
||||||
|
using Nop.Plugin.Misc.AuctionPlugin.Domains.Enums;
|
||||||
using Nop.Services.Orders;
|
using Nop.Services.Orders;
|
||||||
|
|
||||||
namespace Nop.Plugin.Misc.AuctionPlugin.Services;
|
namespace Nop.Plugin.Misc.AuctionPlugin.Services;
|
||||||
|
|
@ -14,7 +16,11 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Services;
|
||||||
public interface IAuctionService
|
public interface IAuctionService
|
||||||
{
|
{
|
||||||
//decimal GetStepAmount(decimal currentPrice);
|
//decimal GetStepAmount(decimal currentPrice);
|
||||||
|
Task<ResponseType> UpdateProductToAuctionStatusIfValidAsync(AuctionStatus newProductToAuctionStatus, Auction auction, ProductToAuctionMapping productToAuction, Customer customer, bool updateInDatabase = true);
|
||||||
|
Task<(Auction auction, ProductToAuctionMapping productToAuction, ResponseType responseType)> GetAndUpdateProductToAuctionStatusIfValidAsync(int productToAuctionId, AuctionStatus newProductToAuctionStatus, Customer customer);
|
||||||
|
Task ResetProductToAuction(ProductToAuctionMapping productToAuction, int? categoryId = null);
|
||||||
|
Task UpdateProductCategoryIsFeaturedAsync(int productId, int? categoryId, bool isFeatured);
|
||||||
|
bool IsValidRequestAuctionStatus(AuctionStatus newStatus, AuctionStatus oldStatus);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets all bids
|
/// Gets all bids
|
||||||
|
|
@ -29,6 +35,8 @@ public interface IAuctionService
|
||||||
Task<IPagedList<AuctionBid>> GetAllBidsAsync(int customerId = 0, int pageIndex = 0, int pageSize = int.MaxValue);
|
Task<IPagedList<AuctionBid>> GetAllBidsAsync(int customerId = 0, int pageIndex = 0, int pageSize = int.MaxValue);
|
||||||
|
|
||||||
Task<int> GetBidsCountByProductToAuctionIdAsync(int productToAuctionId);
|
Task<int> GetBidsCountByProductToAuctionIdAsync(int productToAuctionId);
|
||||||
|
Task<List<AuctionBid>> GetAllLastBidByAuctionIdAsync(int auctionId);
|
||||||
|
Task<Dictionary<int, AuctionBid>> GetAllLastBidDictionaryByAuctionIdAsync(int auctionId);
|
||||||
Task<bool> HasBidByProductToAuctionIdAsync(int productToAuctionId);
|
Task<bool> HasBidByProductToAuctionIdAsync(int productToAuctionId);
|
||||||
Task<AuctionBid> GetBidByIdAsync(int bidId);
|
Task<AuctionBid> GetBidByIdAsync(int bidId);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue