10 changed files with 109 additions and 2 deletions
@ -0,0 +1,8 @@ |
|||
using System.Collections.Generic; |
|||
|
|||
namespace EShopOnAbp.BasketService; |
|||
|
|||
public class OrderAcceptedEto |
|||
{ |
|||
public List<OrderItemEto> Items { get; set; } |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
using System; |
|||
|
|||
namespace EShopOnAbp.BasketService; |
|||
|
|||
public class OrderItemEto |
|||
{ |
|||
public Guid ProductId { get; set; } |
|||
public int Count { get; set; } |
|||
} |
|||
@ -0,0 +1,37 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using EShopOnAbp.BasketService; |
|||
using EShopOnAbp.CatalogService.Products; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Domain.Repositories; |
|||
using Volo.Abp.EventBus.Distributed; |
|||
using Volo.Abp.Uow; |
|||
|
|||
namespace EShopOnAbp.CatalogService; |
|||
|
|||
public class OrderHandler : IDistributedEventHandler<OrderAcceptedEto>, ITransientDependency |
|||
{ |
|||
private readonly IRepository<Product, Guid> _productRepository; |
|||
|
|||
public OrderHandler(IRepository<Product, Guid> productRepository) |
|||
{ |
|||
_productRepository = productRepository; |
|||
} |
|||
|
|||
[UnitOfWork] |
|||
public virtual async Task HandleEventAsync(OrderAcceptedEto eventData) |
|||
{ |
|||
foreach (var orderItemEto in eventData.Items) |
|||
{ |
|||
var product = await _productRepository.FindAsync(orderItemEto.ProductId); |
|||
if (product == null) |
|||
{ |
|||
continue; |
|||
} |
|||
|
|||
var remainingStockCount = product.StockCount - orderItemEto.Count; |
|||
product.SetStockCount(remainingStockCount); |
|||
await _productRepository.UpdateAsync(product); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue