Skip to content

Commit 00e2634

Browse files
committed
Add Confirm Grade period Handler and add new items to Orders status
1 parent 1a9adad commit 00e2634

12 files changed

Lines changed: 147 additions & 28 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace Ordering.API.Application.IntegrationCommands.Commands
2+
{
3+
using System.Collections.Generic;
4+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
5+
6+
public class ConfirmOrderStockCommandMsg : IntegrationEvent
7+
{
8+
public int OrderId { get; }
9+
public IEnumerable<OrderStockItem> OrderStockItem { get; }
10+
11+
public ConfirmOrderStockCommandMsg(int orderId,
12+
IEnumerable<OrderStockItem> orderStockItem)
13+
{
14+
OrderId = orderId;
15+
OrderStockItem = orderStockItem;
16+
}
17+
}
18+
19+
public class OrderStockItem
20+
{
21+
public int ProductId { get; }
22+
public int Units { get; }
23+
24+
public OrderStockItem(int productId, int units)
25+
{
26+
ProductId = productId;
27+
Units = units;
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace Ordering.API.Application.IntegrationEvents.EventHandling
2+
{
3+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
4+
using System;
5+
using System.Threading.Tasks;
6+
using Events;
7+
8+
public class OrderStockConfirmedIntegrationEventHandler : IIntegrationEventHandler<OrderStockConfirmedIntegrationEvent>
9+
{
10+
public async Task Handle(OrderStockConfirmedIntegrationEvent @event)
11+
{
12+
throw new NotImplementedException();
13+
}
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace Ordering.API.Application.IntegrationEvents.EventHandling
2+
{
3+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
4+
using System;
5+
using System.Threading.Tasks;
6+
using Events;
7+
8+
public class OrderStockNotConfirmedIntegrationEventHandler : IIntegrationEventHandler<OrderStockNotConfirmedIntegrationEvent>
9+
{
10+
public async Task Handle(OrderStockNotConfirmedIntegrationEvent @event)
11+
{
12+
throw new NotImplementedException();
13+
}
14+
}
15+
}

src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStartedIntegrationEvent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ public class OrderStartedIntegrationEvent : IntegrationEvent
1616
public OrderStartedIntegrationEvent(string userId) =>
1717
UserId = userId;
1818
}
19-
}
19+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Ordering.API.Application.IntegrationEvents.Events
2+
{
3+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
4+
5+
public class OrderStockConfirmedIntegrationEvent : IntegrationEvent
6+
{
7+
public int OrderId { get; }
8+
9+
public OrderStockConfirmedIntegrationEvent(int orderId) => OrderId = orderId;
10+
}
11+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace Ordering.API.Application.IntegrationEvents.Events
2+
{
3+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
4+
5+
public class OrderStockNotConfirmedIntegrationEvent : IntegrationEvent
6+
{
7+
public int OrderId { get; }
8+
9+
//public IEnumerable<Item> { get; }
10+
11+
public OrderStockNotConfirmedIntegrationEvent(int orderId)
12+
{
13+
OrderId = orderId;
14+
}
15+
}
16+
}

src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
using Ordering.API.Application.IntegrationCommands.Commands;
1010
using Ordering.Domain.Exceptions;
1111
using System;
12+
using System.Collections.Generic;
13+
using System.Linq;
14+
using System.Reflection;
1215
using System.Threading.Tasks;
16+
using Ordering.API.Application.IntegrationEvents;
1317
using Ordering.API.Application.IntegrationEvents.Events;
1418

1519
namespace Ordering.API.Application.Sagas
@@ -29,14 +33,16 @@ public class OrderProcessSaga : Saga<Order>,
2933
{
3034
private readonly IMediator _mediator;
3135
private readonly Func<Owned<OrderingContext>> _dbContextFactory;
36+
private readonly IOrderingIntegrationEventService _orderingIntegrationEventService;
3237

3338
public OrderProcessSaga(
3439
Func<Owned<OrderingContext>> dbContextFactory, OrderingContext orderingContext,
35-
IMediator mediator)
40+
IMediator mediator, IOrderingIntegrationEventService orderingIntegrationEventService)
3641
: base(orderingContext)
3742
{
3843
_dbContextFactory = dbContextFactory;
3944
_mediator = mediator;
45+
_orderingIntegrationEventService = orderingIntegrationEventService;
4046
}
4147

4248
/// <summary>
@@ -77,14 +83,22 @@ public async Task Handle(ConfirmGracePeriodCommandMsg command)
7783
var orderSaga = FindSagaById(command.OrderId);
7884
CheckValidSagaId(orderSaga);
7985

80-
// TODO: This handler should change to Integration command handler type once command bus is implemented
86+
if (orderSaga.OrderStatus != OrderStatus.Cancelled)
87+
{
88+
orderSaga.SetOrderStatusId(OrderStatus.AwaitingValidation.Id);
89+
await SaveChangesAsync();
90+
91+
var orderStockList = orderSaga.OrderItems
92+
.Select(orderItem => new OrderStockItem(orderItem.ProductId, orderItem.GetUnits()));
8193

82-
// TODO: If order status is not cancelled, change state to awaitingValidation and
83-
// send ConfirmOrderStockCommandMsg to Inventory api
84-
94+
//Create Integration Event to be published through the Event Bus
95+
var confirmOrderStockEvent = new ConfirmOrderStockCommandMsg(orderSaga.Id, orderStockList);
96+
97+
// Publish through the Event Bus and mark the saved event as published
98+
await _orderingIntegrationEventService.PublishThroughEventBusAsync(confirmOrderStockEvent);
99+
}
85100
}
86101

87-
88102
/// <summary>
89103
/// Handler which processes the command when
90104
/// customer executes cancel order from app

src/Services/Ordering/Ordering.API/Infrastructure/OrderingContextSeed.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@ public static async Task SeedAsync(IApplicationBuilder applicationBuilder)
3131

3232
if (!context.OrderStatus.Any())
3333
{
34-
context.OrderStatus.Add(OrderStatus.Canceled);
35-
context.OrderStatus.Add(OrderStatus.InProcess);
34+
context.OrderStatus.Add(OrderStatus.Submited);
35+
context.OrderStatus.Add(OrderStatus.AwaitingValidation);
36+
context.OrderStatus.Add(OrderStatus.StockValidated);
37+
context.OrderStatus.Add(OrderStatus.Paid);
3638
context.OrderStatus.Add(OrderStatus.Shipped);
39+
context.OrderStatus.Add(OrderStatus.Cancelled);
3740
}
3841

3942
await context.SaveChangesAsync();

src/Services/Ordering/Ordering.API/Startup.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-

2-
3-
using Ordering.API.Application.IntegrationCommands.Commands;
4-
using Ordering.API.Application.IntegrationEvents.EventHandling;
5-
using Ordering.API.Application.Sagas;
6-
7-
namespace Microsoft.eShopOnContainers.Services.Ordering.API
1+
namespace Microsoft.eShopOnContainers.Services.Ordering.API
82
{
93
using AspNetCore.Http;
104
using Autofac;
115
using Autofac.Extensions.DependencyInjection;
126
using global::Ordering.API.Application.IntegrationEvents;
137
using global::Ordering.API.Application.IntegrationEvents.EventHandling;
148
using global::Ordering.API.Infrastructure.Middlewares;
9+
using global::Ordering.API.Application.IntegrationCommands.Commands;
10+
using global::Ordering.API.Application.IntegrationEvents.Events;
11+
using global::Ordering.API.Application.Sagas;
1512
using Infrastructure;
1613
using Infrastructure.Auth;
1714
using Infrastructure.AutofacModules;
@@ -131,6 +128,10 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
131128
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
132129
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
133130
services.AddTransient<UserCheckoutAcceptedIntegrationEventHandler>();
131+
services.AddTransient<IIntegrationEventHandler<ConfirmGracePeriodCommandMsg>, OrderProcessSaga>();
132+
services.AddTransient<IIntegrationEventHandler<SubmitOrderCommandMsg>, OrderProcessSaga>();
133+
services.AddTransient<OrderStockConfirmedIntegrationEventHandler>();
134+
services.AddTransient<OrderStockNotConfirmedIntegrationEventHandler>();
134135
services.AddOptions();
135136

136137
//configure autofac
@@ -179,11 +180,14 @@ private void ConfigureEventBus(IApplicationBuilder app)
179180
"UserCheckoutAccepted",
180181
() => app.ApplicationServices.GetRequiredService<UserCheckoutAcceptedIntegrationEventHandler>());
181182

182-
eventBus.Subscribe<ConfirmGracePeriodCommandMsg, IIntegrationEventHandler<ConfirmGracePeriodCommandMsg>>(
183-
() => app.ApplicationServices
184-
.GetService<IIntegrationEventHandler<ConfirmGracePeriodCommandMsg>>()
185-
);
183+
eventBus.Subscribe<ConfirmGracePeriodCommandMsg, IIntegrationEventHandler<ConfirmGracePeriodCommandMsg>>
184+
(() => app.ApplicationServices.GetRequiredService<IIntegrationEventHandler<ConfirmGracePeriodCommandMsg>>());
185+
186+
eventBus.Subscribe<OrderStockConfirmedIntegrationEvent, OrderStockConfirmedIntegrationEventHandler>
187+
(() => app.ApplicationServices.GetRequiredService<OrderStockConfirmedIntegrationEventHandler>());
186188

189+
eventBus.Subscribe<OrderStockNotConfirmedIntegrationEvent, OrderStockNotConfirmedIntegrationEventHandler>
190+
(() => app.ApplicationServices.GetRequiredService<OrderStockNotConfirmedIntegrationEventHandler>());
187191
}
188192

189193
protected virtual void ConfigureAuth(IApplicationBuilder app)

src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public Order(Address address, int cardTypeId, string cardNumber, string cardSecu
4646
_orderItems = new List<OrderItem>();
4747
_buyerId = buyerId;
4848
_paymentMethodId = paymentMethodId;
49-
_orderStatusId = OrderStatus.InProcess.Id;
49+
_orderStatusId = OrderStatus.Submited.Id;
5050
_orderDate = DateTime.UtcNow;
5151
Address = address;
5252

@@ -94,6 +94,11 @@ public void SetBuyerId(int id)
9494
_buyerId = id;
9595
}
9696

97+
public void SetOrderStatusId(int id)
98+
{
99+
_orderStatusId = id;
100+
}
101+
97102
private void AddOrderStartedDomainEvent(int cardTypeId, string cardNumber,
98103
string cardSecurityNumber, string cardHolderName, DateTime cardExpiration)
99104
{

0 commit comments

Comments
 (0)