@@ -27,7 +27,6 @@ public class Order
27
27
private string _description ;
28
28
29
29
30
-
31
30
// 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
32
31
private bool _isDraft ;
33
32
@@ -40,21 +39,27 @@ public class Order
40
39
41
40
private int ? _paymentMethodId ;
42
41
43
- public static Order NewDraft ( )
42
+ public static Order NewDraft ( List < OrderItem > orderItems )
44
43
{
45
- var order = new Order ( ) ;
44
+ var order = new Order ( orderItems ) ;
46
45
order . _isDraft = true ;
47
46
return order ;
48
47
}
49
48
50
- protected Order ( )
49
+ protected Order ( List < OrderItem > orderItems )
51
50
{
52
51
_orderItems = new List < OrderItem > ( ) ;
52
+
53
+ foreach ( var item in orderItems )
54
+ {
55
+ AddOrderItem ( item ) ;
56
+ }
57
+
53
58
_isDraft = false ;
54
59
}
55
60
56
61
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 )
58
63
{
59
64
_buyerId = buyerId ;
60
65
_paymentMethodId = paymentMethodId ;
@@ -72,27 +77,27 @@ public Order(string userId, string userName, Address address, int cardTypeId, st
72
77
// This Order AggregateRoot's method "AddOrderitem()" should be the only way to add Items to the Order,
73
78
// so any behavior (discounts, etc.) and validations are controlled by the AggregateRoot
74
79
// 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 )
76
81
{
77
- var existingOrderForProduct = _orderItems . Where ( o => o . ProductId == productId )
78
- . SingleOrDefault ( ) ;
82
+ var existingOrderForProduct = _orderItems
83
+ . SingleOrDefault ( o => o . ProductId == orderItem . ProductId ) ;
79
84
80
85
if ( existingOrderForProduct != null )
81
86
{
82
87
//if previous line exist modify it with higher discount and units..
83
88
84
- if ( discount > existingOrderForProduct . GetCurrentDiscount ( ) )
89
+ var discount = orderItem . GetDiscount ( ) ;
90
+ if ( discount > existingOrderForProduct . GetDiscount ( ) )
85
91
{
86
92
existingOrderForProduct . SetNewDiscount ( discount ) ;
87
93
}
88
94
89
- existingOrderForProduct . AddUnits ( units ) ;
95
+ existingOrderForProduct . AddUnits ( orderItem . GetUnits ( ) ) ;
90
96
}
91
97
else
92
98
{
93
99
//add validated new order item
94
100
95
- var orderItem = new OrderItem ( productId , productName , unitPrice , discount , pictureUrl , units ) ;
96
101
_orderItems . Add ( orderItem ) ;
97
102
}
98
103
}
0 commit comments