@@ -71,7 +71,7 @@ private extension OrderStore {
7171
7272// MARK: - Persistance
7373//
74- extension OrderStore {
74+ private extension OrderStore {
7575
7676 /// Updates (OR Inserts) the specified ReadOnly Order Entity into the Storage Layer.
7777 ///
@@ -81,6 +81,8 @@ extension OrderStore {
8181 let storage = storageManager. viewStorage
8282 let storageOrder = storage. loadOrder ( orderID: readOnlyOrder. orderID) ?? storage. insertNewObject ( ofType: Storage . Order. self)
8383
84+ // TODO: items & coupons
85+
8486 storageOrder. update ( with: readOnlyOrder)
8587 storage. saveIfNeeded ( )
8688 }
@@ -91,12 +93,69 @@ extension OrderStore {
9193 assert ( Thread . isMainThread)
9294
9395 let storage = storageManager. viewStorage
94-
9596 for readOnlyOrder in readOnlyOrders {
9697 let storageOrder = storage. loadOrder ( orderID: readOnlyOrder. orderID) ?? storage. insertNewObject ( ofType: Storage . Order. self)
9798 storageOrder. update ( with: readOnlyOrder)
99+ handleOrderItems ( readOnlyOrder, storageOrder, storage)
100+ handleOrderCoupons ( readOnlyOrder, storageOrder, storage)
98101 }
99102
100103 storage. saveIfNeeded ( )
101104 }
105+
106+ /// Updates, inserts, and prunes the provided StorageOrder's items using the provided read-only Order's items
107+ ///
108+ func handleOrderItems( _ readOnlyOrder: Networking . Order , _ storageOrder: Storage . Order , _ storage: StorageType ) {
109+ guard !readOnlyOrder. items. isEmpty else {
110+ // No items in the read-only order, so remove all the items in Storage.Order
111+ storageOrder. items? . forEach { storageOrder. removeFromItems ( $0) }
112+ return
113+ }
114+
115+ // Upsert the items from the read-only order
116+ for readOnlyItem in readOnlyOrder. items {
117+ if let existingStorageItem = storage. loadOrderItem ( itemID: readOnlyItem. itemID) {
118+ existingStorageItem. update ( with: readOnlyItem)
119+ } else {
120+ let newStorageItem = storage. insertNewObject ( ofType: Storage . OrderItem. self)
121+ newStorageItem. update ( with: readOnlyItem)
122+ storageOrder. addToItems ( newStorageItem)
123+ }
124+ }
125+
126+ // Now, remove any objects that exist in storageOrder.items but not in readOnlyOrder.items
127+ storageOrder. items? . forEach ( { storageItem in
128+ if readOnlyOrder. items. first ( where: { $0. itemID == storageItem. itemID } ) == nil {
129+ storageOrder. removeFromItems ( storageItem)
130+ }
131+ } )
132+ }
133+
134+ /// Updates, inserts, and prunes the provided StorageOrder's coupons using the provided read-only Order's coupons
135+ ///
136+ func handleOrderCoupons( _ readOnlyOrder: Networking . Order , _ storageOrder: Storage . Order , _ storage: StorageType ) {
137+ guard !readOnlyOrder. coupons. isEmpty else {
138+ // No coupons in the read-only order, so remove all the coupons in Storage.Order
139+ storageOrder. coupons? . forEach { storageOrder. removeFromCoupons ( $0) }
140+ return
141+ }
142+
143+ // Upsert the coupons from the read-only order
144+ for readOnlyCoupon in readOnlyOrder. coupons {
145+ if let existingStorageCoupon = storage. loadCouponItem ( couponID: readOnlyCoupon. couponID) {
146+ existingStorageCoupon. update ( with: readOnlyCoupon)
147+ } else {
148+ let newStorageCoupon = storage. insertNewObject ( ofType: Storage . OrderCoupon. self)
149+ newStorageCoupon. update ( with: readOnlyCoupon)
150+ storageOrder. addToCoupons ( newStorageCoupon)
151+ }
152+ }
153+
154+ // Now, remove any objects that exist in storageOrder.coupons but not in readOnlyOrder.coupons
155+ storageOrder. coupons? . forEach ( { storageCoupon in
156+ if readOnlyOrder. coupons. first ( where: { $0. couponID == storageCoupon. couponID } ) == nil {
157+ storageOrder. removeFromCoupons ( storageCoupon)
158+ }
159+ } )
160+ }
102161}
0 commit comments