Add new methods to order aggregate root and modify saga process
This commit is contained in:
parent
f65ce1b9ee
commit
7c27ac498b
@ -17,10 +17,5 @@ namespace Ordering.API.Application.Sagas
|
||||
public abstract TEntity FindSagaById(int id);
|
||||
|
||||
public abstract Task<bool> SaveChangesAsync();
|
||||
//{
|
||||
// var ctx = context ?? _dbContext;
|
||||
// var result = await ctx.SaveChangesAsync();
|
||||
// return result > 0;
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.Seedwork;
|
||||
using Ordering.Domain.Events;
|
||||
using Ordering.Domain.Exceptions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -93,18 +94,38 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
|
||||
_buyerId = id;
|
||||
}
|
||||
|
||||
public void SetOrderStatusId(int id)
|
||||
#region Status Changes
|
||||
public void SetSubmitedStatus()
|
||||
{
|
||||
_orderStatusId = id;
|
||||
_orderStatusId = OrderStatus.Submited.Id;
|
||||
}
|
||||
|
||||
public void SetOrderStockConfirmed(IEnumerable<int> orderStockNotConfirmedItems = null)
|
||||
public void SetAwaitingValidationStatus()
|
||||
{
|
||||
if(orderStockNotConfirmedItems is null)
|
||||
if (_orderStatusId != OrderStatus.Submited.Id)
|
||||
{
|
||||
OrderStatus = OrderStatus.StockValidated;
|
||||
StatusChangeException();
|
||||
}
|
||||
|
||||
_orderStatusId = OrderStatus.AwaitingValidation.Id;
|
||||
|
||||
AddDomainEvent(new OrderStatusChangedToAwaitingValidationDomainEvent(Id, OrderItems));
|
||||
}
|
||||
|
||||
public void SetStockConfirmedStatus(IEnumerable<int> orderStockNotConfirmedItems = null)
|
||||
{
|
||||
if (_orderStatusId != OrderStatus.AwaitingValidation.Id)
|
||||
{
|
||||
StatusChangeException();
|
||||
}
|
||||
|
||||
if (orderStockNotConfirmedItems is null)
|
||||
{
|
||||
OrderStatus = OrderStatus.StockConfirmed;
|
||||
|
||||
_description = "All the items were confirmed with available stock.";
|
||||
|
||||
AddDomainEvent(new OrderStatusChangedToStockConfirmedDomainEvent(Id));
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -117,10 +138,36 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
|
||||
var itemsStockNotConfirmedDescription = string.Join(", ", itemsStockNotConfirmedProductNames);
|
||||
_description = $"The product items don't have stock: ({itemsStockNotConfirmedDescription}).";
|
||||
}
|
||||
|
||||
AddDomainEvent(new OrderStockConfirmedDomainEvent(Id, OrderStatus));
|
||||
}
|
||||
|
||||
public void SetPaidStatus()
|
||||
{
|
||||
if (_orderStatusId != OrderStatus.StockConfirmed.Id)
|
||||
{
|
||||
StatusChangeException();
|
||||
}
|
||||
|
||||
_orderStatusId = OrderStatus.Paid.Id;
|
||||
_description = "The payment was performed at a simulated \"American Bank checking bank account endinf on XX35071\"";
|
||||
|
||||
AddDomainEvent(new OrderStatusChangedToPaidDomainEvent(Id, OrderItems));
|
||||
}
|
||||
|
||||
public void SetShippedStatus()
|
||||
{
|
||||
if (_orderStatusId != OrderStatus.Paid.Id)
|
||||
{
|
||||
StatusChangeException();
|
||||
}
|
||||
|
||||
_orderStatusId = OrderStatus.Shipped.Id;
|
||||
_description = "";
|
||||
|
||||
//Call Domain Event
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private void AddOrderStartedDomainEvent(string userId, int cardTypeId, string cardNumber,
|
||||
string cardSecurityNumber, string cardHolderName, DateTime cardExpiration)
|
||||
{
|
||||
@ -130,6 +177,11 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
|
||||
|
||||
this.AddDomainEvent(orderStartedDomainEvent);
|
||||
}
|
||||
|
||||
private void StatusChangeException()
|
||||
{
|
||||
throw new OrderingDomainException("Not able to process order event. Reason: no valid order status change");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -12,7 +12,7 @@
|
||||
{
|
||||
public static OrderStatus Submited = new OrderStatus(1, nameof(Submited).ToLowerInvariant());
|
||||
public static OrderStatus AwaitingValidation = new OrderStatus(2, nameof(AwaitingValidation).ToLowerInvariant());
|
||||
public static OrderStatus StockValidated = new OrderStatus(3, nameof(StockValidated).ToLowerInvariant());
|
||||
public static OrderStatus StockConfirmed = new OrderStatus(3, nameof(StockConfirmed).ToLowerInvariant());
|
||||
public static OrderStatus Paid = new OrderStatus(4, nameof(Paid).ToLowerInvariant());
|
||||
public static OrderStatus Shipped = new OrderStatus(5, nameof(Shipped).ToLowerInvariant());
|
||||
public static OrderStatus Cancelled = new OrderStatus(6, nameof(Cancelled).ToLowerInvariant());
|
||||
@ -27,7 +27,7 @@
|
||||
}
|
||||
|
||||
public static IEnumerable<OrderStatus> List() =>
|
||||
new[] { Submited, AwaitingValidation, StockValidated, Paid, Shipped, Cancelled };
|
||||
new[] { Submited, AwaitingValidation, StockConfirmed, Paid, Shipped, Cancelled };
|
||||
|
||||
public static OrderStatus FromName(string name)
|
||||
{
|
||||
|
@ -0,0 +1,23 @@
|
||||
namespace Ordering.Domain.Events
|
||||
{
|
||||
using MediatR;
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
|
||||
using System.Collections.Generic;
|
||||
|
||||
/// <summary>
|
||||
/// Event used when the grace period order is confirmed
|
||||
/// </summary>
|
||||
public class OrderStatusChangedToAwaitingValidationDomainEvent
|
||||
: IAsyncNotification
|
||||
{
|
||||
public int OrderId { get; }
|
||||
public IEnumerable<OrderItem> OrderItems { get; }
|
||||
|
||||
public OrderStatusChangedToAwaitingValidationDomainEvent(int orderId,
|
||||
IEnumerable<OrderItem> orderItems)
|
||||
{
|
||||
OrderId = orderId;
|
||||
OrderItems = orderItems;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
namespace Ordering.Domain.Events
|
||||
{
|
||||
using MediatR;
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
|
||||
using System.Collections.Generic;
|
||||
|
||||
/// <summary>
|
||||
/// Event used when the order is paid
|
||||
/// </summary>
|
||||
public class OrderStatusChangedToPaidDomainEvent
|
||||
: IAsyncNotification
|
||||
{
|
||||
public int OrderId { get; }
|
||||
public IEnumerable<OrderItem> OrderItems { get; }
|
||||
|
||||
public OrderStatusChangedToPaidDomainEvent(int orderId,
|
||||
IEnumerable<OrderItem> orderItems)
|
||||
{
|
||||
OrderId = orderId;
|
||||
OrderItems = orderItems;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
namespace Ordering.Domain.Events
|
||||
{
|
||||
using MediatR;
|
||||
|
||||
/// <summary>
|
||||
/// Event used when the order stock items are confirmed
|
||||
/// </summary>
|
||||
public class OrderStatusChangedToStockConfirmedDomainEvent
|
||||
: IAsyncNotification
|
||||
{
|
||||
public int OrderId { get; }
|
||||
|
||||
public OrderStatusChangedToStockConfirmedDomainEvent(int orderId)
|
||||
=> OrderId = orderId;
|
||||
}
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
|
||||
|
||||
namespace Ordering.Domain.Events
|
||||
{
|
||||
using MediatR;
|
||||
using System.Collections.Generic;
|
||||
|
||||
/// <summary>
|
||||
/// Event used when the order stock items are verified
|
||||
/// </summary>
|
||||
public class OrderStockConfirmedDomainEvent
|
||||
: IAsyncNotification
|
||||
{
|
||||
public int OrderId { get; }
|
||||
public OrderStatus OrderStatus { get; }
|
||||
|
||||
public OrderStockConfirmedDomainEvent(int orderId,
|
||||
OrderStatus orderStatus)
|
||||
{
|
||||
OrderId = orderId;
|
||||
OrderStatus = orderStatus;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user