Browse Source

Merge d3d3658775 into 42abcad37e

pull/1416/merge
AndriiTokarskyi 3 years ago
committed by GitHub
parent
commit
4923df2052
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 30 deletions
  1. +15
    -6
      src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommandHandler.cs
  2. +4
    -12
      src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderDraftCommandHandler.cs
  3. +16
    -11
      src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs
  4. +1
    -1
      src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/OrderItem.cs

+ 15
- 6
src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommandHandler.cs View File

@ -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)
{
order.AddOrderItem(item.ProductId, item.ProductName, item.UnitPrice, item.Discount, item.PictureUrl, item.Units);
}
var order = new Order(
message.UserId,
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);


+ 4
- 12
src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderDraftCommandHandler.cs View File

@ -28,13 +28,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 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);
}
var order = Order.NewDraft(orderItems.ToList());
return Task.FromResult(OrderDraftDTO.FromOrder(order)); return Task.FromResult(OrderDraftDTO.FromOrder(order));
} }
@ -52,7 +49,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(),
@ -62,10 +59,5 @@
Total = order.GetTotal() Total = order.GetTotal()
}; };
} }
} }
} }

+ 16
- 11
src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs View File

@ -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)
.SingleOrDefault();
var existingOrderForProduct = _orderItems
.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);
} }
} }


+ 1
- 1
src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/OrderItem.cs View File

@ -42,7 +42,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…
Cancel
Save