@@ -3,7 +3,7 @@ import Storage
33
44// MARK: - PersistedProduct Conversions
55extension PersistedProduct {
6- public init ( from posProduct: POSProduct ) {
6+ init ( from posProduct: POSProduct ) {
77 self . init (
88 id: posProduct. productID,
99 siteID: posProduct. siteID,
@@ -21,8 +21,8 @@ extension PersistedProduct {
2121 stockStatusKey: posProduct. stockStatusKey
2222 )
2323 }
24-
25- public func toPOSProduct( images: [ ProductImage ] = [ ] , attributes: [ ProductAttribute ] = [ ] ) -> POSProduct {
24+
25+ func toPOSProduct( images: [ ProductImage ] = [ ] , attributes: [ ProductAttribute ] = [ ] ) -> POSProduct {
2626 return POSProduct (
2727 siteID: siteID,
2828 productID: id,
@@ -43,7 +43,7 @@ extension PersistedProduct {
4343 )
4444 }
4545
46- public func toPOSProduct( db: GRDBDatabaseConnection ) throws -> POSProduct {
46+ func toPOSProduct( db: GRDBDatabaseConnection ) throws -> POSProduct {
4747 let images = try db. read { db in
4848 return try request ( for: PersistedProduct . images) . fetchAll ( db)
4949 }
@@ -55,11 +55,34 @@ extension PersistedProduct {
5555 attributes: attributes. map { $0. toProductAttribute ( siteID: siteID) }
5656 )
5757 }
58+
59+ }
60+
61+ // MARK: - POSProduct Storage Extensions
62+ extension POSProduct {
63+ public func save( to db: GRDBDatabaseConnection ) throws {
64+ try db. write { db in
65+ let product = PersistedProduct ( from: self )
66+ try product. insert ( db)
67+
68+ // Save related images
69+ for image in self . images {
70+ let persistedImage = PersistedProductImage ( from: image, productID: self . productID)
71+ try persistedImage. insert ( db)
72+ }
73+
74+ // Save related attributes
75+ for attribute in self . attributes {
76+ var persistedAttribute = PersistedProductAttribute ( from: attribute, productID: self . productID)
77+ try persistedAttribute. insert ( db)
78+ }
79+ }
80+ }
5881}
5982
6083// MARK: - PersistedProductAttribute Conversions
6184extension PersistedProductAttribute {
62- public init ( from productAttribute: ProductAttribute , productID: Int64 ) {
85+ init ( from productAttribute: ProductAttribute , productID: Int64 ) {
6386 self . init (
6487 productID: productID,
6588 name: productAttribute. name,
@@ -69,8 +92,8 @@ extension PersistedProductAttribute {
6992 options: productAttribute. options
7093 )
7194 }
72-
73- public func toProductAttribute( siteID: Int64 ) -> ProductAttribute {
95+
96+ func toProductAttribute( siteID: Int64 ) -> ProductAttribute {
7497 return ProductAttribute (
7598 siteID: siteID,
7699 attributeID: 0 ,
@@ -85,7 +108,7 @@ extension PersistedProductAttribute {
85108
86109// MARK: - PersistedProductImage Conversions
87110extension PersistedProductImage {
88- public init ( from productImage: ProductImage , productID: Int64 ) {
111+ init ( from productImage: ProductImage , productID: Int64 ) {
89112 self . init (
90113 id: productImage. imageID,
91114 productID: productID,
@@ -96,8 +119,8 @@ extension PersistedProductImage {
96119 alt: productImage. alt
97120 )
98121 }
99-
100- public func toProductImage( ) -> ProductImage {
122+
123+ func toProductImage( ) -> ProductImage {
101124 return ProductImage (
102125 imageID: id,
103126 dateCreated: dateCreated,
0 commit comments