Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Commit d3d3658

Browse files
Remove AddOrderItem method from Order class
1 parent 8f84bd3 commit d3d3658

File tree

4 files changed

+36
-30
lines changed

4 files changed

+36
-30
lines changed

src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommandHandler.cs

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands
1+
using System.Linq;
2+
3+
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands
24
{
35
using Domain.AggregatesModel.OrderAggregate;
46
using global::Ordering.API.Application.IntegrationEvents;
@@ -46,12 +48,19 @@ public async Task<bool> Handle(CreateOrderCommand message, CancellationToken can
4648
// methods and constructor so validations, invariants and business logic
4749
// make sure that consistency is preserved across the whole aggregate
4850
var address = new Address(message.Street, message.City, message.State, message.Country, message.ZipCode);
49-
var order = new Order(message.UserId, message.UserName, address, message.CardTypeId, message.CardNumber, message.CardSecurityNumber, message.CardHolderName, message.CardExpiration);
51+
var orderItems = message.OrderItems.Select(item => new OrderItem(item.ProductId, item.ProductName,
52+
item.UnitPrice, item.Discount, item.PictureUrl, item.Units));
5053

51-
foreach (var item in message.OrderItems)
52-
{
53-
order.AddOrderItem(item.ProductId, item.ProductName, item.UnitPrice, item.Discount, item.PictureUrl, item.Units);
54-
}
54+
var order = new Order(
55+
message.UserId,
56+
message.UserName,
57+
address,
58+
message.CardTypeId,
59+
message.CardNumber,
60+
message.CardSecurityNumber,
61+
message.CardHolderName,
62+
message.CardExpiration,
63+
orderItems.ToList());
5564

5665
_logger.LogInformation("----- Creating Order - Order: {@Order}", order);
5766

src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderDraftCommandHandler.cs

+4-12
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,10 @@ public CreateOrderDraftCommandHandler(IMediator mediator, IIdentityService iden
2929

3030
public Task<OrderDraftDTO> Handle(CreateOrderDraftCommand message, CancellationToken cancellationToken)
3131
{
32+
var orderItems = message.Items.Select(i => i.ToOrderItemDTO())
33+
.Select(item => new OrderItem(item.ProductId, item.ProductName, item.UnitPrice, item.Discount, item.PictureUrl, item.Units));
3234

33-
var order = Order.NewDraft();
34-
var orderItems = message.Items.Select(i => i.ToOrderItemDTO());
35-
foreach (var item in orderItems)
36-
{
37-
order.AddOrderItem(item.ProductId, item.ProductName, item.UnitPrice, item.Discount, item.PictureUrl, item.Units);
38-
}
35+
var order = Order.NewDraft(orderItems.ToList());
3936

4037
return Task.FromResult(OrderDraftDTO.FromOrder(order));
4138
}
@@ -53,7 +50,7 @@ public static OrderDraftDTO FromOrder(Order order)
5350
{
5451
OrderItems = order.OrderItems.Select(oi => new OrderItemDTO
5552
{
56-
Discount = oi.GetCurrentDiscount(),
53+
Discount = oi.GetDiscount(),
5754
ProductId = oi.ProductId,
5855
UnitPrice = oi.GetUnitPrice(),
5956
PictureUrl = oi.GetPictureUri(),
@@ -63,10 +60,5 @@ public static OrderDraftDTO FromOrder(Order order)
6360
Total = order.GetTotal()
6461
};
6562
}
66-
6763
}
68-
69-
70-
71-
7264
}

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

+16-11
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public class Order
2727
private string _description;
2828

2929

30-
3130
// 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
3231
private bool _isDraft;
3332

@@ -40,21 +39,27 @@ public class Order
4039

4140
private int? _paymentMethodId;
4241

43-
public static Order NewDraft()
42+
public static Order NewDraft(List<OrderItem> orderItems)
4443
{
45-
var order = new Order();
44+
var order = new Order(orderItems);
4645
order._isDraft = true;
4746
return order;
4847
}
4948

50-
protected Order()
49+
protected Order(List<OrderItem> orderItems)
5150
{
5251
_orderItems = new List<OrderItem>();
52+
53+
foreach (var item in orderItems)
54+
{
55+
AddOrderItem(item);
56+
}
57+
5358
_isDraft = false;
5459
}
5560

5661
public Order(string userId, string userName, Address address, int cardTypeId, string cardNumber, string cardSecurityNumber,
57-
string cardHolderName, DateTime cardExpiration, int? buyerId = null, int? paymentMethodId = null) : this()
62+
string cardHolderName, DateTime cardExpiration, List<OrderItem> orderItems, int? buyerId = null, int? paymentMethodId = null) : this(orderItems)
5863
{
5964
_buyerId = buyerId;
6065
_paymentMethodId = paymentMethodId;
@@ -72,27 +77,27 @@ public Order(string userId, string userName, Address address, int cardTypeId, st
7277
// This Order AggregateRoot's method "AddOrderitem()" should be the only way to add Items to the Order,
7378
// so any behavior (discounts, etc.) and validations are controlled by the AggregateRoot
7479
// in order to maintain consistency between the whole Aggregate.
75-
public void AddOrderItem(int productId, string productName, decimal unitPrice, decimal discount, string pictureUrl, int units = 1)
80+
private void AddOrderItem(OrderItem orderItem)
7681
{
77-
var existingOrderForProduct = _orderItems.Where(o => o.ProductId == productId)
78-
.SingleOrDefault();
82+
var existingOrderForProduct = _orderItems
83+
.SingleOrDefault(o => o.ProductId == orderItem.ProductId);
7984

8085
if (existingOrderForProduct != null)
8186
{
8287
//if previous line exist modify it with higher discount and units..
8388

84-
if (discount > existingOrderForProduct.GetCurrentDiscount())
89+
var discount = orderItem.GetDiscount();
90+
if (discount > existingOrderForProduct.GetDiscount())
8591
{
8692
existingOrderForProduct.SetNewDiscount(discount);
8793
}
8894

89-
existingOrderForProduct.AddUnits(units);
95+
existingOrderForProduct.AddUnits(orderItem.GetUnits());
9096
}
9197
else
9298
{
9399
//add validated new order item
94100

95-
var orderItem = new OrderItem(productId, productName, unitPrice, discount, pictureUrl, units);
96101
_orderItems.Add(orderItem);
97102
}
98103
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public OrderItem(int productId, string productName, decimal unitPrice, decimal d
4343

4444
public string GetPictureUri() => _pictureUrl;
4545

46-
public decimal GetCurrentDiscount()
46+
public decimal GetDiscount()
4747
{
4848
return _discount;
4949
}

0 commit comments

Comments
 (0)