Remove AddOrderItem method from Order class
This commit is contained in:
parent
8f84bd3d09
commit
d3d3658775
@ -1,4 +1,6 @@
|
|||||||
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands
|
||||||
{
|
{
|
||||||
using Domain.AggregatesModel.OrderAggregate;
|
using Domain.AggregatesModel.OrderAggregate;
|
||||||
using global::Ordering.API.Application.IntegrationEvents;
|
using global::Ordering.API.Application.IntegrationEvents;
|
||||||
@ -46,12 +48,19 @@
|
|||||||
// methods and constructor so validations, invariants and business logic
|
// methods and constructor so validations, invariants and business logic
|
||||||
// make sure that consistency is preserved across the whole aggregate
|
// make sure that consistency is preserved across the whole aggregate
|
||||||
var address = new Address(message.Street, message.City, message.State, message.Country, message.ZipCode);
|
var address = new Address(message.Street, message.City, message.State, message.Country, message.ZipCode);
|
||||||
var order = new Order(message.UserId, message.UserName, address, message.CardTypeId, message.CardNumber, message.CardSecurityNumber, message.CardHolderName, message.CardExpiration);
|
var orderItems = message.OrderItems.Select(item => new OrderItem(item.ProductId, item.ProductName,
|
||||||
|
item.UnitPrice, item.Discount, item.PictureUrl, item.Units));
|
||||||
|
|
||||||
foreach (var item in message.OrderItems)
|
var order = new Order(
|
||||||
{
|
message.UserId,
|
||||||
order.AddOrderItem(item.ProductId, item.ProductName, item.UnitPrice, item.Discount, item.PictureUrl, item.Units);
|
message.UserName,
|
||||||
}
|
address,
|
||||||
|
message.CardTypeId,
|
||||||
|
message.CardNumber,
|
||||||
|
message.CardSecurityNumber,
|
||||||
|
message.CardHolderName,
|
||||||
|
message.CardExpiration,
|
||||||
|
orderItems.ToList());
|
||||||
|
|
||||||
_logger.LogInformation("----- Creating Order - Order: {@Order}", order);
|
_logger.LogInformation("----- Creating Order - Order: {@Order}", order);
|
||||||
|
|
||||||
|
@ -29,13 +29,10 @@
|
|||||||
|
|
||||||
public Task<OrderDraftDTO> Handle(CreateOrderDraftCommand message, CancellationToken cancellationToken)
|
public Task<OrderDraftDTO> Handle(CreateOrderDraftCommand message, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
|
var orderItems = message.Items.Select(i => i.ToOrderItemDTO())
|
||||||
|
.Select(item => new OrderItem(item.ProductId, item.ProductName, item.UnitPrice, item.Discount, item.PictureUrl, item.Units));
|
||||||
|
|
||||||
var order = Order.NewDraft();
|
var order = Order.NewDraft(orderItems.ToList());
|
||||||
var orderItems = message.Items.Select(i => i.ToOrderItemDTO());
|
|
||||||
foreach (var item in orderItems)
|
|
||||||
{
|
|
||||||
order.AddOrderItem(item.ProductId, item.ProductName, item.UnitPrice, item.Discount, item.PictureUrl, item.Units);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Task.FromResult(OrderDraftDTO.FromOrder(order));
|
return Task.FromResult(OrderDraftDTO.FromOrder(order));
|
||||||
}
|
}
|
||||||
@ -53,7 +50,7 @@
|
|||||||
{
|
{
|
||||||
OrderItems = order.OrderItems.Select(oi => new OrderItemDTO
|
OrderItems = order.OrderItems.Select(oi => new OrderItemDTO
|
||||||
{
|
{
|
||||||
Discount = oi.GetCurrentDiscount(),
|
Discount = oi.GetDiscount(),
|
||||||
ProductId = oi.ProductId,
|
ProductId = oi.ProductId,
|
||||||
UnitPrice = oi.GetUnitPrice(),
|
UnitPrice = oi.GetUnitPrice(),
|
||||||
PictureUrl = oi.GetPictureUri(),
|
PictureUrl = oi.GetPictureUri(),
|
||||||
@ -63,10 +60,5 @@
|
|||||||
Total = order.GetTotal()
|
Total = order.GetTotal()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,6 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
|
|||||||
private string _description;
|
private string _description;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Draft orders have this set to true. Currently we don't check anywhere the draft status of an Order, but we could do it if needed
|
// Draft orders have this set to true. Currently we don't check anywhere the draft status of an Order, but we could do it if needed
|
||||||
private bool _isDraft;
|
private bool _isDraft;
|
||||||
|
|
||||||
@ -40,21 +39,27 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
|
|||||||
|
|
||||||
private int? _paymentMethodId;
|
private int? _paymentMethodId;
|
||||||
|
|
||||||
public static Order NewDraft()
|
public static Order NewDraft(List<OrderItem> orderItems)
|
||||||
{
|
{
|
||||||
var order = new Order();
|
var order = new Order(orderItems);
|
||||||
order._isDraft = true;
|
order._isDraft = true;
|
||||||
return order;
|
return order;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Order()
|
protected Order(List<OrderItem> orderItems)
|
||||||
{
|
{
|
||||||
_orderItems = new List<OrderItem>();
|
_orderItems = new List<OrderItem>();
|
||||||
|
|
||||||
|
foreach (var item in orderItems)
|
||||||
|
{
|
||||||
|
AddOrderItem(item);
|
||||||
|
}
|
||||||
|
|
||||||
_isDraft = false;
|
_isDraft = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Order(string userId, string userName, Address address, int cardTypeId, string cardNumber, string cardSecurityNumber,
|
public Order(string userId, string userName, Address address, int cardTypeId, string cardNumber, string cardSecurityNumber,
|
||||||
string cardHolderName, DateTime cardExpiration, int? buyerId = null, int? paymentMethodId = null) : this()
|
string cardHolderName, DateTime cardExpiration, List<OrderItem> orderItems, int? buyerId = null, int? paymentMethodId = null) : this(orderItems)
|
||||||
{
|
{
|
||||||
_buyerId = buyerId;
|
_buyerId = buyerId;
|
||||||
_paymentMethodId = paymentMethodId;
|
_paymentMethodId = paymentMethodId;
|
||||||
@ -72,27 +77,27 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
|
|||||||
// This Order AggregateRoot's method "AddOrderitem()" should be the only way to add Items to the Order,
|
// This Order AggregateRoot's method "AddOrderitem()" should be the only way to add Items to the Order,
|
||||||
// so any behavior (discounts, etc.) and validations are controlled by the AggregateRoot
|
// so any behavior (discounts, etc.) and validations are controlled by the AggregateRoot
|
||||||
// in order to maintain consistency between the whole Aggregate.
|
// in order to maintain consistency between the whole Aggregate.
|
||||||
public void AddOrderItem(int productId, string productName, decimal unitPrice, decimal discount, string pictureUrl, int units = 1)
|
private void AddOrderItem(OrderItem orderItem)
|
||||||
{
|
{
|
||||||
var existingOrderForProduct = _orderItems.Where(o => o.ProductId == productId)
|
var existingOrderForProduct = _orderItems
|
||||||
.SingleOrDefault();
|
.SingleOrDefault(o => o.ProductId == orderItem.ProductId);
|
||||||
|
|
||||||
if (existingOrderForProduct != null)
|
if (existingOrderForProduct != null)
|
||||||
{
|
{
|
||||||
//if previous line exist modify it with higher discount and units..
|
//if previous line exist modify it with higher discount and units..
|
||||||
|
|
||||||
if (discount > existingOrderForProduct.GetCurrentDiscount())
|
var discount = orderItem.GetDiscount();
|
||||||
|
if (discount > existingOrderForProduct.GetDiscount())
|
||||||
{
|
{
|
||||||
existingOrderForProduct.SetNewDiscount(discount);
|
existingOrderForProduct.SetNewDiscount(discount);
|
||||||
}
|
}
|
||||||
|
|
||||||
existingOrderForProduct.AddUnits(units);
|
existingOrderForProduct.AddUnits(orderItem.GetUnits());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
//add validated new order item
|
//add validated new order item
|
||||||
|
|
||||||
var orderItem = new OrderItem(productId, productName, unitPrice, discount, pictureUrl, units);
|
|
||||||
_orderItems.Add(orderItem);
|
_orderItems.Add(orderItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
|
|||||||
|
|
||||||
public string GetPictureUri() => _pictureUrl;
|
public string GetPictureUri() => _pictureUrl;
|
||||||
|
|
||||||
public decimal GetCurrentDiscount()
|
public decimal GetDiscount()
|
||||||
{
|
{
|
||||||
return _discount;
|
return _discount;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user