-
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 3 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 | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,7 +3,7 @@ import Storage | |||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // MARK: - PersistedProduct Conversions | ||||||||||||||||||||||||
| extension PersistedProduct { | ||||||||||||||||||||||||
| public init(from posProduct: POSProduct) { | ||||||||||||||||||||||||
| init(from posProduct: POSProduct) { | ||||||||||||||||||||||||
| self.init( | ||||||||||||||||||||||||
| id: posProduct.productID, | ||||||||||||||||||||||||
| siteID: posProduct.siteID, | ||||||||||||||||||||||||
|
|
@@ -22,7 +22,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 +42,47 @@ extension PersistedProduct { | |||||||||||||||||||||||
| stockStatusKey: stockStatusKey | ||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| func toPOSProduct(db: GRDBDatabaseConnection) throws -> POSProduct { | ||||||||||||||||||||||||
| let images = try db.read { db in | ||||||||||||||||||||||||
| return try request(for: PersistedProduct.images).fetchAll(db) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| let attributes = try db.read { db in | ||||||||||||||||||||||||
| return try request(for: PersistedProduct.attributes).fetchAll(db) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
| let images = try db.read { db in | |
| return try request(for: PersistedProduct.images).fetchAll(db) | |
| } | |
| let attributes = try db.read { db in | |
| return try request(for: PersistedProduct.attributes).fetchAll(db) | |
| } | |
| 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) | |
| } |
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.
Both done in e3d7b85
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.
do you know if it's recommended to weak self in the db.write closure?
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 know of a recommendation.
The updates closure that we pass to write is non-escaping, so it can't be stored by GRDB, and we don't hold it beyond our function scope either.
I don't see any reason to make self weak here, at least not to avoid retain cycles.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ import Storage | |
|
|
||
| // MARK: - PersistedProductVariation Conversions | ||
| extension PersistedProductVariation { | ||
| public init(from posProductVariation: POSProductVariation) { | ||
| init(from posProductVariation: POSProductVariation) { | ||
| self.init( | ||
| id: posProductVariation.productVariationID, | ||
| siteID: posProductVariation.siteID, | ||
|
|
@@ -19,7 +19,7 @@ extension PersistedProductVariation { | |
| ) | ||
| } | ||
|
|
||
| public func toPOSProductVariation(attributes: [ProductVariationAttribute] = [], image: ProductImage? = nil) -> POSProductVariation { | ||
| func toPOSProductVariation(attributes: [ProductVariationAttribute] = [], image: ProductImage? = nil) -> POSProductVariation { | ||
| return POSProductVariation( | ||
| siteID: siteID, | ||
| productID: productID, | ||
|
|
@@ -36,19 +36,55 @@ extension PersistedProductVariation { | |
| stockStatusKey: stockStatusKey | ||
| ) | ||
| } | ||
|
|
||
| func toPOSProductVariation(db: GRDBDatabaseConnection) throws -> POSProductVariation { | ||
| let image = try db.read { db in | ||
| return try request(for: PersistedProductVariation.image).fetchOne(db) | ||
| } | ||
| let attributes = try db.read { db in | ||
| return try request(for: PersistedProductVariation.attributes).fetchAll(db) | ||
| } | ||
jaclync marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return toPOSProductVariation( | ||
| attributes: attributes.map { $0.toProductVariationAttribute() }, | ||
| image: image?.toProductImage() | ||
| ) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| // MARK: - POSProductVariation Storage Extensions | ||
| extension POSProductVariation { | ||
| public func save(to db: GRDBDatabaseConnection) throws { | ||
| try db.write { db in | ||
| let variation = PersistedProductVariation(from: self) | ||
| try variation.insert(db) | ||
|
|
||
| // Save related image if present | ||
| if let image = self.image { | ||
| let persistedImage = PersistedProductVariationImage(from: image, productVariationID: self.productVariationID) | ||
| try persistedImage.insert(db) | ||
| } | ||
|
|
||
| // Save related attributes | ||
| for attribute in self.attributes { | ||
| var persistedAttribute = PersistedProductVariationAttribute(from: attribute, productVariationID: self.productVariationID) | ||
|
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. nit: could be
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. No – |
||
| try persistedAttribute.insert(db) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // MARK: - PersistedProductVariationAttribute Conversions | ||
| extension PersistedProductVariationAttribute { | ||
| public init(from productVariationAttribute: ProductVariationAttribute, productVariationID: Int64) { | ||
| init(from productVariationAttribute: ProductVariationAttribute, productVariationID: Int64) { | ||
| self.init( | ||
| productVariationID: productVariationID, | ||
| name: productVariationAttribute.name, | ||
| option: productVariationAttribute.option | ||
| ) | ||
| } | ||
|
|
||
| public func toProductVariationAttribute() -> ProductVariationAttribute { | ||
| func toProductVariationAttribute() -> ProductVariationAttribute { | ||
| return ProductVariationAttribute( | ||
| id: id ?? 0, | ||
| name: name, | ||
|
|
||
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.