Skip to content

Commit 99109bc

Browse files
committed
service to fetch coupons from remote
1 parent 38b15ce commit 99109bc

File tree

2 files changed

+43
-19
lines changed

2 files changed

+43
-19
lines changed

Networking/Networking/Remote/CouponsRemote.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,23 @@ public final class CouponsRemote: Remote, CouponsRemoteProtocol {
372372
}
373373
}
374374

375+
extension CouponsRemote {
376+
public func loadAllCoupons(for siteID: Int64,
377+
pageNumber: Int = CouponsRemote.Default.pageNumber,
378+
pageSize: Int = CouponsRemote.Default.pageSize) async throws -> [Coupon] {
379+
try await withCheckedThrowingContinuation { continuation in
380+
loadAllCoupons(for: siteID, pageNumber: pageNumber, pageSize: pageSize) { result in
381+
switch result {
382+
case .success(let coupons):
383+
continuation.resume(returning: coupons)
384+
case .failure(let error):
385+
continuation.resume(throwing: error)
386+
}
387+
}
388+
}
389+
}
390+
}
391+
375392
// MARK: - Constants
376393
//
377394
public extension CouponsRemote {
Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import protocol Networking.Network
22
import protocol Networking.ProductVariationsRemoteProtocol
3-
import class Networking.ProductsRemote
4-
import class Networking.ProductVariationsRemote
3+
import class Networking.CouponsRemote
54
import class Networking.AlamofireNetwork
65
import class WooFoundation.CurrencyFormatter
76
import class WooFoundation.CurrencySettings
@@ -10,8 +9,7 @@ import Storage
109
public final class PointOfSaleCouponService: PointOfSaleItemServiceProtocol {
1110
private var siteID: Int64
1211
private let currencyFormatter: CurrencyFormatter
13-
private let productsRemote: ProductsRemote
14-
private let variationRemote: ProductVariationsRemoteProtocol
12+
private let couponsRemote: CouponsRemote
1513
private let storage: StorageManagerType?
1614

1715
public init(siteID: Int64,
@@ -20,8 +18,7 @@ public final class PointOfSaleCouponService: PointOfSaleItemServiceProtocol {
2018
storage: StorageManagerType? = nil) {
2119
self.siteID = siteID
2220
self.currencyFormatter = CurrencyFormatter(currencySettings: currencySettings)
23-
self.productsRemote = ProductsRemote(network: network)
24-
self.variationRemote = ProductVariationsRemote(network: network)
21+
self.couponsRemote = CouponsRemote(network: network)
2522
self.storage = storage
2623
}
2724

@@ -38,26 +35,36 @@ public final class PointOfSaleCouponService: PointOfSaleItemServiceProtocol {
3835
// TODO:
3936
// gh-15326 - Return PagedItems<POSItem> instead.
4037
@MainActor
41-
public func providePointOfSaleCoupons() -> [POSItem] {
38+
public func providePointOfSaleCoupons() async -> [POSItem] {
4239
guard let storage = storage else {
4340
return []
4441
}
45-
let predicate = NSPredicate(format: "siteID == %lld", siteID)
46-
let descriptor = NSSortDescriptor(keyPath: \StorageCoupon.dateCreated,
47-
ascending: false)
48-
49-
let resultsController = ResultsController<StorageCoupon>(storageManager: storage,
50-
matching: predicate,
51-
sortedBy: [descriptor])
52-
42+
// #2 Remote
5343
do {
54-
try resultsController.performFetch()
55-
let storeCoupons = resultsController.fetchedObjects
56-
return mapCouponsToPOSItems(coupons: storeCoupons)
44+
let remoteCoupons = try await couponsRemote.loadAllCoupons(for: siteID)
45+
return mapCouponsToPOSItems(coupons: remoteCoupons)
5746
} catch {
5847
debugPrint(error)
5948
return []
6049
}
50+
51+
// #1 Storage
52+
// let predicate = NSPredicate(format: "siteID == %lld", siteID)
53+
// let descriptor = NSSortDescriptor(keyPath: \StorageCoupon.dateCreated,
54+
// ascending: false)
55+
//
56+
// let resultsController = ResultsController<StorageCoupon>(storageManager: storage,
57+
// matching: predicate,
58+
// sortedBy: [descriptor])
59+
//
60+
// do {
61+
// try resultsController.performFetch()
62+
// let storeCoupons = resultsController.fetchedObjects
63+
// return mapCouponsToPOSItems(coupons: storeCoupons)
64+
// } catch {
65+
// debugPrint(error)
66+
// return []
67+
// }
6168
}
6269

6370
private func mapCouponsToPOSItems(coupons: [Coupon]) -> [POSItem] {
@@ -73,7 +80,7 @@ public final class PointOfSaleCouponService: PointOfSaleItemServiceProtocol {
7380

7481
@MainActor
7582
public func providePointOfSaleItems(pageNumber: Int) async throws -> PagedItems<POSItem> {
76-
let coupons = providePointOfSaleCoupons()
83+
let coupons = await providePointOfSaleCoupons()
7784
return .init(items: coupons, hasMorePages: false)
7885
}
7986
}

0 commit comments

Comments
 (0)