Skip to content

Commit c89cc06

Browse files
committed
Add new methods to order aggregate root and modify saga process
1 parent 7f23ef1 commit c89cc06

7 files changed

Lines changed: 122 additions & 37 deletions

File tree

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,5 @@ public Saga(DbContext dbContext)
1717
public abstract TEntity FindSagaById(int id);
1818

1919
public abstract Task<bool> SaveChangesAsync();
20-
//{
21-
// var ctx = context ?? _dbContext;
22-
// var result = await ctx.SaveChangesAsync();
23-
// return result > 0;
24-
//}
2520
}
2621
}

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

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.eShopOnContainers.Services.Ordering.Domain.Seedwork;
22
using Ordering.Domain.Events;
3+
using Ordering.Domain.Exceptions;
34
using System;
45
using System.Collections.Generic;
56
using System.Linq;
@@ -93,18 +94,38 @@ public void SetBuyerId(int id)
9394
_buyerId = id;
9495
}
9596

96-
public void SetOrderStatusId(int id)
97+
#region Status Changes
98+
public void SetSubmitedStatus()
9799
{
98-
_orderStatusId = id;
100+
_orderStatusId = OrderStatus.Submited.Id;
101+
}
102+
103+
public void SetAwaitingValidationStatus()
104+
{
105+
if (_orderStatusId != OrderStatus.Submited.Id)
106+
{
107+
StatusChangeException();
108+
}
109+
110+
_orderStatusId = OrderStatus.AwaitingValidation.Id;
111+
112+
AddDomainEvent(new OrderStatusChangedToAwaitingValidationDomainEvent(Id, OrderItems));
99113
}
100114

101-
public void SetOrderStockConfirmed(IEnumerable<int> orderStockNotConfirmedItems = null)
115+
public void SetStockConfirmedStatus(IEnumerable<int> orderStockNotConfirmedItems = null)
102116
{
103-
if(orderStockNotConfirmedItems is null)
117+
if (_orderStatusId != OrderStatus.AwaitingValidation.Id)
104118
{
105-
OrderStatus = OrderStatus.StockValidated;
119+
StatusChangeException();
120+
}
121+
122+
if (orderStockNotConfirmedItems is null)
123+
{
124+
OrderStatus = OrderStatus.StockConfirmed;
106125

107126
_description = "All the items were confirmed with available stock.";
127+
128+
AddDomainEvent(new OrderStatusChangedToStockConfirmedDomainEvent(Id));
108129
}
109130
else
110131
{
@@ -117,10 +138,36 @@ public void SetOrderStockConfirmed(IEnumerable<int> orderStockNotConfirmedItems
117138
var itemsStockNotConfirmedDescription = string.Join(", ", itemsStockNotConfirmedProductNames);
118139
_description = $"The product items don't have stock: ({itemsStockNotConfirmedDescription}).";
119140
}
141+
}
142+
143+
public void SetPaidStatus()
144+
{
145+
if (_orderStatusId != OrderStatus.StockConfirmed.Id)
146+
{
147+
StatusChangeException();
148+
}
149+
150+
_orderStatusId = OrderStatus.Paid.Id;
151+
_description = "The payment was performed at a simulated \"American Bank checking bank account endinf on XX35071\"";
152+
153+
AddDomainEvent(new OrderStatusChangedToPaidDomainEvent(Id, OrderItems));
154+
}
155+
156+
public void SetShippedStatus()
157+
{
158+
if (_orderStatusId != OrderStatus.Paid.Id)
159+
{
160+
StatusChangeException();
161+
}
120162

121-
AddDomainEvent(new OrderStockConfirmedDomainEvent(Id, OrderStatus));
163+
_orderStatusId = OrderStatus.Shipped.Id;
164+
_description = "";
165+
166+
//Call Domain Event
122167
}
123168

169+
#endregion
170+
124171
private void AddOrderStartedDomainEvent(string userId, int cardTypeId, string cardNumber,
125172
string cardSecurityNumber, string cardHolderName, DateTime cardExpiration)
126173
{
@@ -130,6 +177,11 @@ private void AddOrderStartedDomainEvent(string userId, int cardTypeId, string ca
130177

131178
this.AddDomainEvent(orderStartedDomainEvent);
132179
}
180+
181+
private void StatusChangeException()
182+
{
183+
throw new OrderingDomainException("Not able to process order event. Reason: no valid order status change");
184+
}
133185
}
134186
}
135187

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class OrderStatus
1212
{
1313
public static OrderStatus Submited = new OrderStatus(1, nameof(Submited).ToLowerInvariant());
1414
public static OrderStatus AwaitingValidation = new OrderStatus(2, nameof(AwaitingValidation).ToLowerInvariant());
15-
public static OrderStatus StockValidated = new OrderStatus(3, nameof(StockValidated).ToLowerInvariant());
15+
public static OrderStatus StockConfirmed = new OrderStatus(3, nameof(StockConfirmed).ToLowerInvariant());
1616
public static OrderStatus Paid = new OrderStatus(4, nameof(Paid).ToLowerInvariant());
1717
public static OrderStatus Shipped = new OrderStatus(5, nameof(Shipped).ToLowerInvariant());
1818
public static OrderStatus Cancelled = new OrderStatus(6, nameof(Cancelled).ToLowerInvariant());
@@ -27,7 +27,7 @@ public OrderStatus(int id, string name)
2727
}
2828

2929
public static IEnumerable<OrderStatus> List() =>
30-
new[] { Submited, AwaitingValidation, StockValidated, Paid, Shipped, Cancelled };
30+
new[] { Submited, AwaitingValidation, StockConfirmed, Paid, Shipped, Cancelled };
3131

3232
public static OrderStatus FromName(string name)
3333
{
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace Ordering.Domain.Events
2+
{
3+
using MediatR;
4+
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
5+
using System.Collections.Generic;
6+
7+
/// <summary>
8+
/// Event used when the grace period order is confirmed
9+
/// </summary>
10+
public class OrderStatusChangedToAwaitingValidationDomainEvent
11+
: IAsyncNotification
12+
{
13+
public int OrderId { get; }
14+
public IEnumerable<OrderItem> OrderItems { get; }
15+
16+
public OrderStatusChangedToAwaitingValidationDomainEvent(int orderId,
17+
IEnumerable<OrderItem> orderItems)
18+
{
19+
OrderId = orderId;
20+
OrderItems = orderItems;
21+
}
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace Ordering.Domain.Events
2+
{
3+
using MediatR;
4+
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
5+
using System.Collections.Generic;
6+
7+
/// <summary>
8+
/// Event used when the order is paid
9+
/// </summary>
10+
public class OrderStatusChangedToPaidDomainEvent
11+
: IAsyncNotification
12+
{
13+
public int OrderId { get; }
14+
public IEnumerable<OrderItem> OrderItems { get; }
15+
16+
public OrderStatusChangedToPaidDomainEvent(int orderId,
17+
IEnumerable<OrderItem> orderItems)
18+
{
19+
OrderId = orderId;
20+
OrderItems = orderItems;
21+
}
22+
}
23+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace Ordering.Domain.Events
2+
{
3+
using MediatR;
4+
5+
/// <summary>
6+
/// Event used when the order stock items are confirmed
7+
/// </summary>
8+
public class OrderStatusChangedToStockConfirmedDomainEvent
9+
: IAsyncNotification
10+
{
11+
public int OrderId { get; }
12+
13+
public OrderStatusChangedToStockConfirmedDomainEvent(int orderId)
14+
=> OrderId = orderId;
15+
}
16+
}

src/Services/Ordering/Ordering.Domain/Events/OrderStockMethodVerifiedDomainEvent.cs

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)