Skip to content

Commit edbdd33

Browse files
committed
WIP: order storage logic + tests
1 parent 6d71316 commit edbdd33

File tree

11 files changed

+183
-16
lines changed

11 files changed

+183
-16
lines changed

Networking/Networking/Model/Address.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@ public struct Address: Decodable {
1515
public let country: String
1616
public let phone: String?
1717
public let email: String?
18+
19+
/// Designated Initializer.
20+
///
21+
public init(firstName: String, lastName: String, company: String?, address1: String, address2: String?, city: String, state: String, postcode: String, country: String, phone: String?, email: String?) {
22+
self.firstName = firstName
23+
self.lastName = lastName
24+
self.company = company
25+
self.address1 = address1
26+
self.address2 = address2
27+
self.city = city
28+
self.state = state
29+
self.postcode = postcode
30+
self.country = country
31+
self.phone = phone
32+
self.email = email
33+
}
1834
}
1935

2036

Networking/Networking/Model/Order.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public struct Order: Decodable {
3232

3333
/// Order struct initializer.
3434
///
35-
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, paymentMethodTitle: String, items: [OrderItem], billingAddress: Address, shippingAddress: Address, coupons: [OrderCouponLine]) {
35+
public 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, paymentMethodTitle: String, items: [OrderItem], billingAddress: Address, shippingAddress: Address, coupons: [OrderCouponLine]) {
3636

3737
self.orderID = orderID
3838
self.parentID = parentID

Storage/Storage/Model/Order+CoreDataProperties.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ extension Order {
1212
@NSManaged public var parentID: Int64
1313
@NSManaged public var customerID: Int64
1414
@NSManaged public var number: String?
15-
@NSManaged public var status: String?
15+
@NSManaged public var status: String
1616
@NSManaged public var currency: String?
1717
@NSManaged public var customerNote: String?
18-
@NSManaged public var dateCreated: NSDate?
19-
@NSManaged public var dateModified: NSDate?
20-
@NSManaged public var datePaid: NSDate?
18+
@NSManaged public var dateCreated: Date?
19+
@NSManaged public var dateModified: Date?
20+
@NSManaged public var datePaid: Date?
2121
@NSManaged public var discountTotal: String?
2222
@NSManaged public var discountTax: String?
23-
@NSManaged public var shippingTotal: Int16
23+
@NSManaged public var shippingTotal: String?
2424
@NSManaged public var shippingTax: String?
2525
@NSManaged public var total: String?
2626
@NSManaged public var totalTax: String?

Storage/Storage/Model/OrderCoupon+CoreDataProperties.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ extension OrderCoupon {
1212
@NSManaged public var code: String?
1313
@NSManaged public var discount: String?
1414
@NSManaged public var discountTax: String?
15-
@NSManaged public var order: Order?
15+
@NSManaged public var order: Order
1616
}

Storage/Storage/Model/OrderItem+CoreDataProperties.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ extension OrderItem {
1919
@NSManaged public var total: String?
2020
@NSManaged public var totalTax: String?
2121
@NSManaged public var variationID: Int64
22-
@NSManaged public var order: Order?
22+
@NSManaged public var order: Order
2323
}

Storage/Storage/Model/WooCommerce.xcdatamodeld/Model.xcdatamodel/contents

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
<attribute name="parentID" optional="YES" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="YES" syncable="YES"/>
3737
<attribute name="paymentMethodTitle" optional="YES" attributeType="String" syncable="YES"/>
3838
<attribute name="shippingTax" optional="YES" attributeType="String" syncable="YES"/>
39-
<attribute name="shippingTotal" optional="YES" attributeType="Integer 16" defaultValueString="0" usesScalarValueType="YES" syncable="YES"/>
40-
<attribute name="status" optional="YES" attributeType="String" syncable="YES"/>
39+
<attribute name="shippingTotal" optional="YES" attributeType="String" syncable="YES"/>
40+
<attribute name="status" attributeType="String" syncable="YES"/>
4141
<attribute name="total" optional="YES" attributeType="String" syncable="YES"/>
4242
<attribute name="totalTax" optional="YES" attributeType="String" syncable="YES"/>
4343
<relationship name="billingAddress" optional="YES" maxCount="1" deletionRule="Cascade" destinationEntity="Address" inverseName="billingOrder" inverseEntity="Address" syncable="YES"/>
@@ -75,10 +75,10 @@
7575
</entity>
7676
<elements>
7777
<element name="Account" positionX="-218" positionY="132" width="128" height="120"/>
78-
<element name="Site" positionX="-216" positionY="285" width="128" height="120"/>
79-
<element name="Order" positionX="-45" positionY="54" width="128" height="358"/>
80-
<element name="OrderItem" positionX="216" positionY="72" width="128" height="223"/>
8178
<element name="Address" positionX="259" positionY="381" width="128" height="240"/>
79+
<element name="Order" positionX="-45" positionY="54" width="128" height="360"/>
8280
<element name="OrderCoupon" positionX="18" positionY="234" width="128" height="120"/>
81+
<element name="OrderItem" positionX="216" positionY="72" width="128" height="223"/>
82+
<element name="Site" positionX="-216" positionY="285" width="128" height="120"/>
8383
</elements>
8484
</model>

Storage/Storage/Tools/StorageType+Extensions.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,11 @@ public extension StorageType {
1818
let predicate = NSPredicate(format: "siteID = %ld", siteID)
1919
return firstObject(ofType: Site.self, matching: predicate)
2020
}
21+
22+
/// Retrieves the Stored Order.
23+
///
24+
public func loadOrder(orderID: Int) -> Order? {
25+
let predicate = NSPredicate(format: "orderID = %ld", orderID)
26+
return firstObject(ofType: Order.self, matching: predicate)
27+
}
2128
}

Yosemite/Yosemite.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
74A7688E20D45ED400F9D437 /* OrderStoreTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74A7688D20D45ED400F9D437 /* OrderStoreTests.swift */; };
1919
74A7689020D45F9300F9D437 /* OrderAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74A7688F20D45F9300F9D437 /* OrderAction.swift */; };
2020
74A7689220D47F9E00F9D437 /* orders.json in Resources */ = {isa = PBXBuildFile; fileRef = 74A7689120D47F9E00F9D437 /* orders.json */; };
21+
74D7F29B20F6A7FB0058B2F0 /* Order+ReadOnlyConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74D7F29A20F6A7FB0058B2F0 /* Order+ReadOnlyConvertible.swift */; };
2122
B505254C20EE6491008090F5 /* Site+ReadOnlyConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = B505254B20EE6491008090F5 /* Site+ReadOnlyConvertible.swift */; };
2223
B53D89E520E6C22B00F90866 /* Model.swift in Sources */ = {isa = PBXBuildFile; fileRef = B53D89E420E6C22B00F90866 /* Model.swift */; };
2324
B546CCF22093636A007CDA5F /* Yosemite.h in Headers */ = {isa = PBXBuildFile; fileRef = B546CCF12093636A007CDA5F /* Yosemite.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -68,6 +69,7 @@
6869
74A7688D20D45ED400F9D437 /* OrderStoreTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OrderStoreTests.swift; sourceTree = "<group>"; };
6970
74A7688F20D45F9300F9D437 /* OrderAction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OrderAction.swift; sourceTree = "<group>"; };
7071
74A7689120D47F9E00F9D437 /* orders.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = orders.json; sourceTree = "<group>"; };
72+
74D7F29A20F6A7FB0058B2F0 /* Order+ReadOnlyConvertible.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Order+ReadOnlyConvertible.swift"; sourceTree = "<group>"; };
7173
79402E7AD394EEB60C39A4B8 /* Pods-YosemiteTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-YosemiteTests.debug.xcconfig"; path = "../Pods/Target Support Files/Pods-YosemiteTests/Pods-YosemiteTests.debug.xcconfig"; sourceTree = "<group>"; };
7274
7F47E19203B04CF6BB5EA659 /* Pods-Yosemite.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Yosemite.debug.xcconfig"; path = "../Pods/Target Support Files/Pods-Yosemite/Pods-Yosemite.debug.xcconfig"; sourceTree = "<group>"; };
7375
93D120F57D5D14A10B3AFFFD /* Pods-Yosemite.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Yosemite.release.xcconfig"; path = "../Pods/Target Support Files/Pods-Yosemite/Pods-Yosemite.release.xcconfig"; sourceTree = "<group>"; };
@@ -134,6 +136,7 @@
134136
children = (
135137
B53D89E420E6C22B00F90866 /* Model.swift */,
136138
B5EED1A720F4F3CF00652449 /* Account+ReadOnlyConvertible.swift */,
139+
74D7F29A20F6A7FB0058B2F0 /* Order+ReadOnlyConvertible.swift */,
137140
B505254B20EE6491008090F5 /* Site+ReadOnlyConvertible.swift */,
138141
);
139142
path = Model;
@@ -503,6 +506,7 @@
503506
74A7689020D45F9300F9D437 /* OrderAction.swift in Sources */,
504507
7499936620EFBC7200CF01CD /* OrderNoteStore.swift in Sources */,
505508
B5C9DE182087FF0E006B910A /* Assert.swift in Sources */,
509+
74D7F29B20F6A7FB0058B2F0 /* Order+ReadOnlyConvertible.swift in Sources */,
506510
B5C9DE162087FF0E006B910A /* Store.swift in Sources */,
507511
B56C1EBE20EABD2B00D749F9 /* ResultsController.swift in Sources */,
508512
B5EED1A820F4F3CF00652449 /* Account+ReadOnlyConvertible.swift in Sources */,
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import Foundation
2+
import Storage
3+
4+
5+
// MARK: - Storage.order: ReadOnlyConvertible
6+
//
7+
extension Storage.Order: ReadOnlyConvertible {
8+
9+
/// Updates the Storage.Order with the a ReadOnly.
10+
///
11+
public func update(with order: Yosemite.Order) {
12+
orderID = Int64(order.orderID)
13+
parentID = Int64(order.parentID)
14+
customerID = Int64(order.customerID)
15+
number = order.number
16+
status = order.status.rawValue
17+
currency = order.currency
18+
customerNote = order.customerNote
19+
dateCreated = order.dateCreated
20+
dateModified = order.dateModified
21+
datePaid = order.datePaid
22+
discountTotal = order.discountTotal
23+
discountTax = order.discountTax
24+
shippingTotal = order.shippingTotal
25+
shippingTax = order.shippingTax
26+
total = order.total
27+
totalTax = order.totalTax
28+
paymentMethodTitle = order.paymentMethodTitle
29+
30+
// TODO: items, coupons, billing address, and shipping address
31+
}
32+
33+
/// Returns a ReadOnly version of the receiver.
34+
///
35+
public func toReadOnly() -> Yosemite.Order {
36+
return Order(orderID: Int(orderID),
37+
parentID: Int(parentID),
38+
customerID: Int(customerID),
39+
number: number ?? "",
40+
status: OrderStatus(rawValue: status),
41+
currency: currency ?? "",
42+
customerNote: customerNote ?? "",
43+
dateCreated: dateCreated ?? Date(),
44+
dateModified: dateModified ?? Date(),
45+
datePaid: datePaid ?? Date(),
46+
discountTotal: discountTotal ?? "",
47+
discountTax: discountTax ?? "",
48+
shippingTotal: shippingTotal ?? "",
49+
shippingTax: shippingTax ?? "",
50+
total: total ?? "",
51+
totalTax: totalTax ?? "",
52+
paymentMethodTitle: paymentMethodTitle ?? "",
53+
items: [],
54+
billingAddress: blankAddress(),
55+
shippingAddress: blankAddress(),
56+
coupons: [])
57+
58+
// TODO: ^^^^ items, coupons, billing address, and shipping address ^^^^
59+
}
60+
61+
private func blankAddress() -> Yosemite.Address {
62+
return Address(firstName: "",
63+
lastName: "",
64+
company: "",
65+
address1: "",
66+
address2: "",
67+
city: "",
68+
state: "",
69+
postcode: "",
70+
country: "",
71+
phone: "",
72+
email: "")
73+
}
74+
}

Yosemite/Yosemite/Stores/OrderStore.swift

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Foundation
22
import Networking
3+
import Storage
34

45

56
// MARK: - OrderStore
@@ -32,19 +33,20 @@ public class OrderStore: Store {
3233

3334
// MARK: - Services!
3435
//
35-
extension OrderStore {
36+
private extension OrderStore {
3637

3738
/// Retrieves the orders associated with a given Site ID (if any!).
3839
///
3940
func retrieveOrders(siteId: Int, onCompletion: @escaping ([Order]?, Error?) -> Void) {
4041
let remote = OrdersRemote(network: network)
4142

42-
remote.loadAllOrders(for: siteId) { (orders, error) in
43+
remote.loadAllOrders(for: siteId) { [weak self] (orders, error) in
4344
guard let orders = orders else {
4445
onCompletion(nil, error)
4546
return
4647
}
4748

49+
self?.upsertStoredOrders(readOnlyOrders: orders)
4850
onCompletion(orders, nil)
4951
}
5052
}
@@ -54,13 +56,47 @@ extension OrderStore {
5456
func retrieveOrder(siteId: Int, orderId: Int, onCompletion: @escaping (Order?, Error?) -> Void) {
5557
let remote = OrdersRemote(network: network)
5658

57-
remote.loadOrder(for: siteId, orderID: orderId) { (order, error) in
59+
remote.loadOrder(for: siteId, orderID: orderId) { [weak self] (order, error) in
5860
guard let order = order else {
5961
onCompletion(nil, error)
6062
return
6163
}
6264

65+
self?.upsertStoredOrder(readOnlyOrder: order)
6366
onCompletion(order, nil)
6467
}
6568
}
6669
}
70+
71+
72+
// MARK: - Persistance
73+
//
74+
extension OrderStore {
75+
76+
/// Updates (OR Inserts) the specified ReadOnly Order Entity into the Storage Layer.
77+
///
78+
func upsertStoredOrder(readOnlyOrder: Networking.Order) {
79+
assert(Thread.isMainThread)
80+
81+
let storage = storageManager.viewStorage
82+
let storageOrder = storage.loadOrder(orderID: readOnlyOrder.orderID) ?? storage.insertNewObject(ofType: Storage.Order.self)
83+
84+
storageOrder.update(with: readOnlyOrder)
85+
storage.saveIfNeeded()
86+
}
87+
88+
/// Updates (OR Inserts) the specified ReadOnly Order Entities into the Storage Layer.
89+
///
90+
func upsertStoredOrders(readOnlyOrders: [Networking.Order]) {
91+
assert(Thread.isMainThread)
92+
93+
let storage = storageManager.viewStorage
94+
95+
for readOnlyOrder in readOnlyOrders {
96+
let storageOrder = storage.loadOrder(orderID: readOnlyOrder.orderID) ?? storage.insertNewObject(ofType: Storage.Order.self)
97+
storageOrder.update(with: readOnlyOrder)
98+
}
99+
100+
storage.saveIfNeeded()
101+
}
102+
}

0 commit comments

Comments
 (0)