-
Notifications
You must be signed in to change notification settings - Fork 121
[Woo POS][Local catalog] Add relationship mapping and saving for POS models #16063
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b37d574
f416265
627787d
e831e89
e9fc1ce
cdc2e9d
e3d7b85
435502f
b9c3b19
5c8d942
a9048a2
991e405
c523e48
2477ef4
604a3df
d0c1b2d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,8 +2,9 @@ import Foundation | |
| import Storage | ||
|
|
||
| // MARK: - PersistedProduct Conversions | ||
| // periphery:ignore - TODO: remove ignore when populating database | ||
| extension PersistedProduct { | ||
| public init(from posProduct: POSProduct) { | ||
| init(from posProduct: POSProduct) { | ||
| self.init( | ||
| id: posProduct.productID, | ||
| siteID: posProduct.siteID, | ||
|
|
@@ -22,7 +23,7 @@ extension PersistedProduct { | |
| ) | ||
| } | ||
|
|
||
| public func toPOSProduct(images: [ProductImage] = [], attributes: [ProductAttribute] = []) -> POSProduct { | ||
| func toPOSProduct(images: [ProductImage] = [], attributes: [ProductAttribute] = []) -> POSProduct { | ||
| return POSProduct( | ||
| siteID: siteID, | ||
| productID: id, | ||
|
|
@@ -42,11 +43,49 @@ extension PersistedProduct { | |
| stockStatusKey: stockStatusKey | ||
| ) | ||
| } | ||
|
|
||
| func toPOSProduct(db: GRDBDatabaseConnection) throws -> POSProduct { | ||
| let (images, attributes) = try db.read { db in | ||
| let images = try request(for: PersistedProduct.images).fetchAll(db) | ||
| let attributes = try request(for: PersistedProduct.attributes).fetchAll(db) | ||
| return (images, attributes) | ||
| } | ||
|
|
||
| return toPOSProduct( | ||
| images: images.map { $0.toProductImage() }, | ||
| attributes: attributes.map { $0.toProductAttribute(siteID: siteID) } | ||
| ) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| // MARK: - POSProduct Storage Extensions | ||
| // periphery:ignore - TODO: remove ignore when populating database | ||
| extension POSProduct { | ||
| public func save(to db: GRDBDatabaseConnection) throws { | ||
| try db.write { db in | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you know if it's recommended to weak self in the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know of a recommendation. The I don't see any reason to make |
||
| let product = PersistedProduct(from: self) | ||
| try product.insert(db) | ||
|
|
||
| // Save related images | ||
| for image in self.images { | ||
| let persistedImage = PersistedProductImage(from: image, productID: self.productID) | ||
| try persistedImage.insert(db) | ||
| } | ||
|
|
||
| // Save related attributes | ||
| for attribute in self.attributes { | ||
| var persistedAttribute = PersistedProductAttribute(from: attribute, productID: self.productID) | ||
| try persistedAttribute.insert(db) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // MARK: - PersistedProductAttribute Conversions | ||
| // periphery:ignore - TODO: remove ignore when populating database | ||
| extension PersistedProductAttribute { | ||
| public init(from productAttribute: ProductAttribute, productID: Int64) { | ||
| init(from productAttribute: ProductAttribute, productID: Int64) { | ||
| self.init( | ||
| productID: productID, | ||
| name: productAttribute.name, | ||
|
|
@@ -57,7 +96,7 @@ extension PersistedProductAttribute { | |
| ) | ||
| } | ||
|
|
||
| public func toProductAttribute(siteID: Int64) -> ProductAttribute { | ||
| func toProductAttribute(siteID: Int64) -> ProductAttribute { | ||
| return ProductAttribute( | ||
| siteID: siteID, | ||
| attributeID: id ?? 0, | ||
|
|
@@ -71,8 +110,9 @@ extension PersistedProductAttribute { | |
| } | ||
|
|
||
| // MARK: - PersistedProductImage Conversions | ||
| // periphery:ignore - TODO: remove ignore when populating database | ||
| extension PersistedProductImage { | ||
| public init(from productImage: ProductImage, productID: Int64) { | ||
| init(from productImage: ProductImage, productID: Int64) { | ||
| self.init( | ||
| id: productImage.imageID, | ||
| productID: productID, | ||
|
|
@@ -84,7 +124,7 @@ extension PersistedProductImage { | |
| ) | ||
| } | ||
|
|
||
| public func toProductImage() -> ProductImage { | ||
| func toProductImage() -> ProductImage { | ||
| return ProductImage( | ||
| imageID: id, | ||
| dateCreated: dateCreated, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❓ what are the use cases for fetching products, then their relationships separately, as this method seems to do? I'd think the products (with some filter) are fetched including the relationships?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think they are – relationships are just foreign keys. If you fetch a record from SQLite, you'll get the id for looking up the relationship, but it won't walk the graph for you. I'll double check that GRDB doesn't do it for us, but I don't think it does.