Skip to content

Commit b37d574

Browse files
committed
Add attribute and image associations
1 parent ff03881 commit b37d574

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ extension PersistedProduct: FetchableRecord, PersistableRecord {
6767
static let stockQuantity = Column(CodingKeys.stockQuantity)
6868
static let stockStatusKey = Column(CodingKeys.stockStatusKey)
6969
}
70+
71+
public static let images = hasMany(PersistedProductImage.self)
72+
public static let attributes = hasMany(PersistedProductAttribute.self)
7073
}
7174

7275
private extension PersistedProduct {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ extension PersistedProductVariation: FetchableRecord, PersistableRecord {
5555
static let stockQuantity = Column(CodingKeys.stockQuantity)
5656
static let stockStatusKey = Column(CodingKeys.stockStatusKey)
5757
}
58+
59+
public static let attributes = hasMany(PersistedProductVariationAttribute.self)
60+
public static let image = hasOne(PersistedProductVariationImage.self)
5861
}
5962

6063

Modules/Sources/Yosemite/Model/Storage/PersistedProduct+Conversions.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ extension PersistedProduct {
4242
stockStatusKey: stockStatusKey
4343
)
4444
}
45+
46+
public func toPOSProduct(db: GRDBDatabaseConnection) throws -> POSProduct {
47+
let images = try db.read { db in
48+
return try request(for: PersistedProduct.images).fetchAll(db)
49+
}
50+
let attributes = try db.read { db in
51+
return try request(for: PersistedProduct.attributes).fetchAll(db)
52+
}
53+
return toPOSProduct(
54+
images: images.map { $0.toProductImage() },
55+
attributes: attributes.map { $0.toProductAttribute(siteID: siteID) }
56+
)
57+
}
4558
}
4659

4760
// MARK: - PersistedProductAttribute Conversions

Modules/Sources/Yosemite/Model/Storage/PersistedProductVariation+Conversions.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ extension PersistedProductVariation {
3636
stockStatusKey: stockStatusKey
3737
)
3838
}
39+
40+
public func toPOSProductVariation(db: GRDBDatabaseConnection) throws -> POSProductVariation {
41+
let image = try db.read { db in
42+
return try request(for: PersistedProductVariation.image).fetchOne(db)
43+
}
44+
let attributes = try db.read { db in
45+
return try request(for: PersistedProductVariation.attributes).fetchAll(db)
46+
}
47+
return toPOSProductVariation(
48+
attributes: attributes.map { $0.toProductVariationAttribute() },
49+
image: image?.toProductImage()
50+
)
51+
}
3952
}
4053

4154
// MARK: - PersistedProductVariationAttribute Conversions

0 commit comments

Comments
 (0)