Skip to content

Commit 64b030a

Browse files
authored
[Woo POS][Local Catalog] Basic mapping from POS models to Persisted models (#16062)
2 parents 5205ada + 2159f0d commit 64b030a

13 files changed

+1145
-0
lines changed

Modules/Sources/Storage/GRDB/Model/PersistedProduct.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Foundation
22
import GRDB
33

4+
// periphery:ignore - TODO: remove ignore when populating database
45
public struct PersistedProduct: Codable {
56
public let id: Int64
67
public let siteID: Int64
@@ -48,6 +49,7 @@ public struct PersistedProduct: Codable {
4849
}
4950
}
5051

52+
// periphery:ignore - TODO: remove ignore when populating database
5153
extension PersistedProduct: FetchableRecord, PersistableRecord {
5254
public static var databaseTableName: String { "product" }
5355

@@ -67,8 +69,12 @@ extension PersistedProduct: FetchableRecord, PersistableRecord {
6769
static let stockQuantity = Column(CodingKeys.stockQuantity)
6870
static let stockStatusKey = Column(CodingKeys.stockStatusKey)
6971
}
72+
73+
public static let images = hasMany(PersistedProductImage.self)
74+
public static let attributes = hasMany(PersistedProductAttribute.self)
7075
}
7176

77+
// periphery:ignore - TODO: remove ignore when populating database
7278
private extension PersistedProduct {
7379
enum CodingKeys: String, CodingKey {
7480
case id

Modules/Sources/Storage/GRDB/Model/PersistedProductAttribute.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Foundation
22
import GRDB
33

4+
// periphery:ignore - TODO: remove ignore when populating database
45
public struct PersistedProductAttribute: Codable {
56
public private(set) var id: Int64?
67
public let productID: Int64
@@ -27,6 +28,7 @@ public struct PersistedProductAttribute: Codable {
2728
}
2829
}
2930

31+
// periphery:ignore - TODO: remove ignore when populating database
3032
extension PersistedProductAttribute: FetchableRecord, MutablePersistableRecord {
3133
public static var databaseTableName: String { "productAttribute" }
3234

@@ -46,6 +48,7 @@ extension PersistedProductAttribute: FetchableRecord, MutablePersistableRecord {
4648
}
4749

4850

51+
// periphery:ignore - TODO: remove ignore when populating database
4952
private extension PersistedProductAttribute {
5053
enum CodingKeys: String, CodingKey {
5154
case id

Modules/Sources/Storage/GRDB/Model/PersistedProductImage.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Foundation
22
import GRDB
33

4+
// periphery:ignore - TODO: remove ignore when populating database
45
public struct PersistedProductImage: Codable {
56
public let id: Int64
67
public let productID: Int64
@@ -27,6 +28,7 @@ public struct PersistedProductImage: Codable {
2728
}
2829
}
2930

31+
// periphery:ignore - TODO: remove ignore when populating database
3032
extension PersistedProductImage: FetchableRecord, PersistableRecord {
3133
public static var databaseTableName: String { "productImage" }
3234

@@ -42,6 +44,7 @@ extension PersistedProductImage: FetchableRecord, PersistableRecord {
4244
}
4345

4446

47+
// periphery:ignore - TODO: remove ignore when populating database
4548
private extension PersistedProductImage {
4649
enum CodingKeys: String, CodingKey {
4750
case id

Modules/Sources/Storage/GRDB/Model/PersistedProductVariation.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Foundation
22
import GRDB
33

4+
// periphery:ignore - TODO: remove ignore when populating database
45
public struct PersistedProductVariation: Codable {
56
public let id: Int64
67
public let siteID: Int64
@@ -39,6 +40,7 @@ public struct PersistedProductVariation: Codable {
3940
}
4041
}
4142

43+
// periphery:ignore - TODO: remove ignore when populating database
4244
extension PersistedProductVariation: FetchableRecord, PersistableRecord {
4345
public static var databaseTableName: String { "productVariation" }
4446

@@ -55,9 +57,13 @@ extension PersistedProductVariation: FetchableRecord, PersistableRecord {
5557
static let stockQuantity = Column(CodingKeys.stockQuantity)
5658
static let stockStatusKey = Column(CodingKeys.stockStatusKey)
5759
}
60+
61+
public static let attributes = hasMany(PersistedProductVariationAttribute.self).forKey("attributes")
62+
public static let image = hasOne(PersistedProductVariationImage.self).forKey("image")
5863
}
5964

6065

66+
// periphery:ignore - TODO: remove ignore when populating database
6167
private extension PersistedProductVariation {
6268
enum CodingKeys: String, CodingKey {
6369
case id

Modules/Sources/Storage/GRDB/Model/PersistedProductVariationAttribute.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Foundation
22
import GRDB
33

4+
// periphery:ignore - TODO: remove ignore when populating database
45
public struct PersistedProductVariationAttribute: Codable {
56
public private(set) var id: Int64?
67
public let productVariationID: Int64
@@ -18,6 +19,8 @@ public struct PersistedProductVariationAttribute: Codable {
1819
}
1920
}
2021

22+
// periphery:ignore - TODO: remove ignore when populating database
23+
// periphery:ignore - TODO: remove ignore when populating database
2124
extension PersistedProductVariationAttribute: FetchableRecord, MutablePersistableRecord {
2225
public static var databaseTableName: String { "productVariationAttribute" }
2326

@@ -34,6 +37,7 @@ extension PersistedProductVariationAttribute: FetchableRecord, MutablePersistabl
3437
}
3538

3639

40+
// periphery:ignore - TODO: remove ignore when populating database
3741
private extension PersistedProductVariationAttribute {
3842
enum CodingKeys: String, CodingKey {
3943
case id

Modules/Sources/Storage/GRDB/Model/PersistedProductVariationImage.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Foundation
22
import GRDB
33

4+
// periphery:ignore - TODO: remove ignore when populating database
45
public struct PersistedProductVariationImage: Codable {
56
public let id: Int64
67
public let productVariationID: Int64
@@ -27,6 +28,7 @@ public struct PersistedProductVariationImage: Codable {
2728
}
2829
}
2930

31+
// periphery:ignore - TODO: remove ignore when populating database
3032
extension PersistedProductVariationImage: FetchableRecord, PersistableRecord {
3133
public static var databaseTableName: String { "productVariationImage" }
3234

@@ -42,6 +44,7 @@ extension PersistedProductVariationImage: FetchableRecord, PersistableRecord {
4244
}
4345

4446

47+
// periphery:ignore - TODO: remove ignore when populating database
4548
private extension PersistedProductVariationImage {
4649
enum CodingKeys: String, CodingKey {
4750
case id

Modules/Sources/Storage/GRDB/Model/PersistedSite.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
import Foundation
22
import GRDB
33

4+
// periphery:ignore - TODO: remove ignore when populating database
45
public struct PersistedSite: Codable {
6+
// periphery:ignore - TODO: remove ignore when populating database
57
public let id: Int64
68

9+
// periphery:ignore - TODO: remove ignore when populating database
710
public init(id: Int64) {
811
self.id = id
912
}
1013
}
1114

15+
// periphery:ignore - TODO: remove ignore when populating database
1216
extension PersistedSite: FetchableRecord, PersistableRecord {
1317
public static var databaseTableName: String { "site" }
1418

1519
public enum Columns {
20+
// periphery:ignore - TODO: remove ignore when populating database
1621
static let id = Column(CodingKeys.id)
1722
}
1823
}
1924

25+
// periphery:ignore - TODO: remove ignore when populating database
2026
private extension PersistedSite {
2127
enum CodingKeys: String, CodingKey {
2228
case id

Modules/Sources/Storage/Protocols/Object.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public protocol Object: AnyObject {
1111

1212
/// Returns an instance of ObjectID: expected to identify the current instance, unequivocally.
1313
///
14+
// periphery:ignore - Used in tests, no changes but mysteriously stopped being ignored.
1415
var objectID: ObjectID { get }
1516

1617
/// Returns the receiver's Entity Name.

Modules/Sources/Yosemite/Model/Model.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,12 +350,19 @@ public typealias StorageWooShippingShipment = Storage.WooShippingShipment
350350
public typealias StorageWooShippingOriginAddress = Storage.WooShippingOriginAddress
351351

352352
// MARK: - GRDB Persisted Models
353+
// periphery:ignore - TODO: remove ignore when populating database
353354
public typealias PersistedSite = Storage.PersistedSite
355+
// periphery:ignore - TODO: remove ignore when populating database
354356
public typealias PersistedProduct = Storage.PersistedProduct
357+
// periphery:ignore - TODO: remove ignore when populating database
355358
public typealias PersistedProductAttribute = Storage.PersistedProductAttribute
359+
// periphery:ignore - TODO: remove ignore when populating database
356360
public typealias PersistedProductImage = Storage.PersistedProductImage
361+
// periphery:ignore - TODO: remove ignore when populating database
357362
public typealias PersistedProductVariation = Storage.PersistedProductVariation
363+
// periphery:ignore - TODO: remove ignore when populating database
358364
public typealias PersistedProductVariationAttribute = Storage.PersistedProductVariationAttribute
365+
// periphery:ignore - TODO: remove ignore when populating database
359366
public typealias PersistedProductVariationImage = Storage.PersistedProductVariationImage
360367

361368
// MARK: - Internal ReadOnly Models
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import Foundation
2+
import Storage
3+
4+
// MARK: - PersistedProduct Conversions
5+
// periphery:ignore - TODO: remove ignore when populating database
6+
extension PersistedProduct {
7+
init(from posProduct: POSProduct) {
8+
self.init(
9+
id: posProduct.productID,
10+
siteID: posProduct.siteID,
11+
name: posProduct.name,
12+
productTypeKey: posProduct.productTypeKey,
13+
fullDescription: posProduct.fullDescription,
14+
shortDescription: posProduct.shortDescription,
15+
sku: posProduct.sku,
16+
globalUniqueID: posProduct.globalUniqueID,
17+
price: posProduct.price,
18+
downloadable: posProduct.downloadable,
19+
parentID: posProduct.parentID,
20+
manageStock: posProduct.manageStock,
21+
stockQuantity: posProduct.stockQuantity,
22+
stockStatusKey: posProduct.stockStatusKey
23+
)
24+
}
25+
26+
func toPOSProduct(images: [ProductImage] = [], attributes: [ProductAttribute] = []) -> POSProduct {
27+
return POSProduct(
28+
siteID: siteID,
29+
productID: id,
30+
name: name,
31+
productTypeKey: productTypeKey,
32+
fullDescription: fullDescription,
33+
shortDescription: shortDescription,
34+
sku: sku,
35+
globalUniqueID: globalUniqueID,
36+
price: price,
37+
downloadable: downloadable,
38+
parentID: parentID,
39+
images: images,
40+
attributes: attributes,
41+
manageStock: manageStock,
42+
stockQuantity: stockQuantity,
43+
stockStatusKey: stockStatusKey
44+
)
45+
}
46+
47+
func toPOSProduct(db: GRDBDatabaseConnection) throws -> POSProduct {
48+
let (images, attributes) = try db.read { db in
49+
let images = try request(for: PersistedProduct.images).fetchAll(db)
50+
let attributes = try request(for: PersistedProduct.attributes).fetchAll(db)
51+
return (images, attributes)
52+
}
53+
54+
return toPOSProduct(
55+
images: images.map { $0.toProductImage() },
56+
attributes: attributes.map { $0.toProductAttribute(siteID: siteID) }
57+
)
58+
}
59+
60+
}
61+
62+
// MARK: - POSProduct Storage Extensions
63+
// periphery:ignore - TODO: remove ignore when populating database
64+
extension POSProduct {
65+
public func save(to db: GRDBDatabaseConnection) throws {
66+
try db.write { db in
67+
let product = PersistedProduct(from: self)
68+
try product.insert(db)
69+
70+
// Save related images
71+
for image in self.images {
72+
let persistedImage = PersistedProductImage(from: image, productID: self.productID)
73+
try persistedImage.insert(db)
74+
}
75+
76+
// Save related attributes
77+
for attribute in self.attributes {
78+
var persistedAttribute = PersistedProductAttribute(from: attribute, productID: self.productID)
79+
try persistedAttribute.insert(db)
80+
}
81+
}
82+
}
83+
}
84+
85+
// MARK: - PersistedProductAttribute Conversions
86+
// periphery:ignore - TODO: remove ignore when populating database
87+
extension PersistedProductAttribute {
88+
init(from productAttribute: ProductAttribute, productID: Int64) {
89+
self.init(
90+
productID: productID,
91+
name: productAttribute.name,
92+
position: Int64(productAttribute.position),
93+
visible: productAttribute.visible,
94+
variation: productAttribute.variation,
95+
options: productAttribute.options
96+
)
97+
}
98+
99+
func toProductAttribute(siteID: Int64) -> ProductAttribute {
100+
return ProductAttribute(
101+
siteID: siteID,
102+
attributeID: id ?? 0,
103+
name: name,
104+
position: Int(position),
105+
visible: visible,
106+
variation: variation,
107+
options: options
108+
)
109+
}
110+
}
111+
112+
// MARK: - PersistedProductImage Conversions
113+
// periphery:ignore - TODO: remove ignore when populating database
114+
extension PersistedProductImage {
115+
init(from productImage: ProductImage, productID: Int64) {
116+
self.init(
117+
id: productImage.imageID,
118+
productID: productID,
119+
dateCreated: productImage.dateCreated,
120+
dateModified: productImage.dateModified,
121+
src: productImage.src,
122+
name: productImage.name,
123+
alt: productImage.alt
124+
)
125+
}
126+
127+
func toProductImage() -> ProductImage {
128+
return ProductImage(
129+
imageID: id,
130+
dateCreated: dateCreated,
131+
dateModified: dateModified,
132+
src: src,
133+
name: name,
134+
alt: alt
135+
)
136+
}
137+
}

0 commit comments

Comments
 (0)