@@ -17,9 +17,6 @@ public struct Order: Decodable {
1717 public let dateModified : Date
1818 public let datePaid : Date ?
1919
20- private let dateCreatedString : String ?
21- private let dateModifiedString : String ?
22-
2320 public let discountTotal : String
2421 public let discountTax : String
2522 public let shippingTotal : String
@@ -31,7 +28,9 @@ public struct Order: Decodable {
3128 public let billingAddress : Address
3229 public let shippingAddress : Address
3330
34- init ( orderID: Int , parentID: Int , customerID: Int , number: String , status: OrderStatus , currency: String , customerNote: String ? , dateCreatedString: String ? , dateModifiedString: String ? , datePaid: Date ? , discountTotal: String , discountTax: String , shippingTotal: String , shippingTax: String , total: String , totalTax: String , items: [ OrderItem ] , billingAddress: Address , shippingAddress: Address ) {
31+ /// Order struct initializer.
32+ ///
33+ init ( orderID: Int , parentID: Int , customerID: Int , number: String , status: OrderStatus , currency: String , customerNote: String ? , dateCreated: Date , dateModified: Date , datePaid: Date ? , discountTotal: String , discountTax: String , shippingTotal: String , shippingTax: String , total: String , totalTax: String , items: [ OrderItem ] , billingAddress: Address , shippingAddress: Address ) {
3534 self . orderID = orderID
3635 self . parentID = parentID
3736 self . customerID = customerID
@@ -41,33 +40,10 @@ public struct Order: Decodable {
4140 self . currency = currency
4241 self . customerNote = customerNote
4342
44- self . dateCreatedString = dateCreatedString == nil ? " " : dateCreatedString
45- self . dateModifiedString = dateModifiedString
43+ self . dateCreated = dateCreated
44+ self . dateModified = dateModified
4645 self . datePaid = datePaid
4746
48- let format = ISO8601DateFormatter ( )
49- var dateCreated : Date ?
50- if let createdString = dateCreatedString {
51- dateCreated = format. date ( from: createdString)
52- }
53-
54- if let dateCreated = dateCreated {
55- self . dateCreated = dateCreated
56- } else {
57- self . dateCreated = Date ( )
58- }
59-
60- var dateModified : Date ?
61- if let modifiedString = dateModifiedString {
62- dateModified = format. date ( from: modifiedString)
63- }
64-
65- if let dateModified = dateModified {
66- self . dateModified = dateModified
67- } else {
68- self . dateModified = Date ( )
69- }
70-
7147 self . discountTotal = discountTotal
7248 self . discountTax = discountTax
7349 self . shippingTotal = shippingTotal
@@ -80,6 +56,9 @@ public struct Order: Decodable {
8056 self . shippingAddress = shippingAddress
8157 }
8258
59+
60+ /// The public initializer for Order.
61+ ///
8362 public init ( from decoder: Decoder ) throws {
8463 let container = try decoder. container ( keyedBy: CodingKeys . self)
8564 let orderID = try container. decode ( Int . self, forKey: . orderID)
@@ -91,8 +70,8 @@ public struct Order: Decodable {
9170 let currency = try container. decode ( String . self, forKey: . currency)
9271 let customerNote = try container. decode ( String . self, forKey: . customerNote)
9372
94- let dateCreatedString = try container. decodeIfPresent ( String . self, forKey: . dateCreatedString )
95- let dateModifiedString = try container. decodeIfPresent ( String . self, forKey: . dateModifiedString )
73+ let dateCreated = try container. decodeIfPresent ( Date . self, forKey: . dateCreated ) ?? Date ( )
74+ let dateModified = try container. decodeIfPresent ( Date . self, forKey: . dateModified ) ?? Date ( )
9675 let datePaid = try container. decodeIfPresent ( Date . self, forKey: . datePaid)
9776
9877 let discountTotal = try container. decode ( String . self, forKey: . discountTotal)
@@ -106,7 +85,7 @@ public struct Order: Decodable {
10685 let shippingAddress = try container. decode ( Address . self, forKey: . shippingAddress)
10786 let billingAddress = try container. decode ( Address . self, forKey: . billingAddress)
10887
109- self . init ( orderID: orderID, parentID: parentID, customerID: customerID, number: number, status: status, currency: currency, customerNote: customerNote, dateCreatedString : dateCreatedString , dateModifiedString : dateModifiedString , datePaid: datePaid, discountTotal: discountTotal, discountTax: discountTax, shippingTotal: shippingTotal, shippingTax: shippingTax, total: total, totalTax: totalTax, items: items, billingAddress: billingAddress, shippingAddress: shippingAddress)
88+ self . init ( orderID: orderID, parentID: parentID, customerID: customerID, number: number, status: status, currency: currency, customerNote: customerNote, dateCreated : dateCreated , dateModified : dateModified , datePaid: datePaid, discountTotal: discountTotal, discountTax: discountTax, shippingTotal: shippingTotal, shippingTax: shippingTax, total: total, totalTax: totalTax, items: items, billingAddress: billingAddress, shippingAddress: shippingAddress) // initialize the struct
11089 }
11190}
11291
@@ -125,8 +104,8 @@ private extension Order {
125104 case currency = " currency "
126105 case customerNote = " customer_note "
127106
128- case dateCreatedString = " date_created_gmt "
129- case dateModifiedString = " date_modified_gmt "
107+ case dateCreated = " date_created_gmt "
108+ case dateModified = " date_modified_gmt "
130109 case datePaid = " date_paid_gmt "
131110
132111 case discountTotal = " discount_total "
0 commit comments