diff --git a/services/ordering/src/EShopOnAbp.OrderingService.Domain.Shared/Buyers/BuyerEto.cs b/services/ordering/src/EShopOnAbp.OrderingService.Domain.Shared/Buyers/BuyerEto.cs new file mode 100644 index 00000000..cc43c92c --- /dev/null +++ b/services/ordering/src/EShopOnAbp.OrderingService.Domain.Shared/Buyers/BuyerEto.cs @@ -0,0 +1,13 @@ +using System; +using Volo.Abp.Domain.Entities.Events.Distributed; + +namespace EShopOnAbp.OrderingService.Buyers; + +public class BuyerEto : EtoBase +{ + public Guid BuyerId { get; set; } + public string BuyerEmail { get; set; } + public string PaymentType { get; set; } + public int PaymentTypeId { get; set; } + +} \ No newline at end of file diff --git a/services/ordering/src/EShopOnAbp.OrderingService.Domain.Shared/Orders/OrderItemEto.cs b/services/ordering/src/EShopOnAbp.OrderingService.Domain.Shared/Orders/OrderItemEto.cs index 049fda07..83bf27fc 100644 --- a/services/ordering/src/EShopOnAbp.OrderingService.Domain.Shared/Orders/OrderItemEto.cs +++ b/services/ordering/src/EShopOnAbp.OrderingService.Domain.Shared/Orders/OrderItemEto.cs @@ -1,15 +1,16 @@ using System; +using Volo.Abp.Domain.Entities.Events.Distributed; namespace EShopOnAbp.OrderingService.Orders { - public class OrderItemEto + public class OrderItemEto : EtoBase { public Guid ProductId { get; set; } - + public string ProductCode { get; set; } public string ProductName { get; set; } public string PictureUrl { get; set; } public decimal UnitPrice { get; set; } public decimal Discount { get; set; } public int Units { get; set; } } -} +} \ No newline at end of file diff --git a/services/ordering/src/EShopOnAbp.OrderingService.Domain.Shared/Orders/OrderPlacedEto.cs b/services/ordering/src/EShopOnAbp.OrderingService.Domain.Shared/Orders/OrderPlacedEto.cs new file mode 100644 index 00000000..4ead8fbc --- /dev/null +++ b/services/ordering/src/EShopOnAbp.OrderingService.Domain.Shared/Orders/OrderPlacedEto.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using EShopOnAbp.OrderingService.Buyers; +using Volo.Abp.Domain.Entities.Events.Distributed; +using Volo.Abp.EventBus; + +namespace EShopOnAbp.OrderingService.Orders; + +[EventName("EShopOnAbp.Order.Placed")] +public class OrderPlacedEto : EtoBase +{ + public Guid OrderId { get; set; } + public BuyerEto Buyer { get; set; } + public DateTime OrderDate { get; set; } + public List Items { get; set; } = new(); +} \ No newline at end of file diff --git a/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/OrderManager.cs b/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/OrderManager.cs index b16302f0..d5de4077 100644 --- a/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/OrderManager.cs +++ b/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/OrderManager.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using EShopOnAbp.OrderingService.Buyers; using Volo.Abp.Domain.Services; +using Volo.Abp.EventBus.Distributed; namespace EShopOnAbp.OrderingService.Orders; @@ -10,11 +11,16 @@ public class OrderManager : DomainService { private readonly IOrderRepository _orderRepository; private readonly IBuyerRepository _buyerRepository; + private readonly IDistributedEventBus _distributedEventBus; - public OrderManager(IOrderRepository orderRepository, IBuyerRepository buyerRepository) + public OrderManager( + IOrderRepository orderRepository, + IBuyerRepository buyerRepository, + IDistributedEventBus distributedEventBus) { _orderRepository = orderRepository; _buyerRepository = buyerRepository; + _distributedEventBus = distributedEventBus; } public async Task CreateOrderAsync( @@ -62,6 +68,48 @@ public class OrderManager : DomainService ); } - return await _orderRepository.InsertAsync(order); + var placedOrder = await _orderRepository.InsertAsync(order, true); + + // Publish Order placed event + await _distributedEventBus.PublishAsync(new OrderPlacedEto + { + OrderId = placedOrder.Id, + OrderDate = placedOrder.OrderDate, + Buyer = GetBuyerEto(buyer), + Items = GetProductItemEtoList(order.OrderItems) + }); + + return placedOrder; + } + + private BuyerEto GetBuyerEto(Buyer buyer) + { + return new BuyerEto() + { + BuyerEmail = buyer.Email, + BuyerId = buyer.Id, + PaymentType = buyer.PaymentType.Name, + PaymentTypeId = buyer.PaymentType.Id + }; + } + + private List GetProductItemEtoList(List orderItems) + { + List etoList = new List(); + foreach (var oItem in orderItems) + { + etoList.Add(new OrderItemEto() + { + Discount = oItem.Discount, + PictureUrl = oItem.PictureUrl, + ProductCode = oItem.ProductCode, + ProductId = oItem.ProductId, + ProductName = oItem.ProductName, + UnitPrice = oItem.UnitPrice, + Units = oItem.Units + }); + } + + return etoList; } } \ No newline at end of file