From af5c677dedea285f31a0d13af9374f9f1421aa26 Mon Sep 17 00:00:00 2001 From: Laurent Date: Thu, 3 Apr 2025 17:46:14 +0200 Subject: [PATCH] Adds direct post/call calls before adding to the collection --- LeStorage/Codables/ApiCall.swift | 5 +++++ LeStorage/Codables/DataAccess.swift | 12 ++++++++---- LeStorage/Codables/FailedAPICall.swift | 8 ++++++++ LeStorage/Codables/GetSyncData.swift | 8 ++++++++ LeStorage/Codables/Log.swift | 6 +++++- LeStorage/SyncedCollection.swift | 16 +++++++++++++++- LeStorage/SyncedStorable.swift | 10 +++++++++- 7 files changed, 58 insertions(+), 7 deletions(-) diff --git a/LeStorage/Codables/ApiCall.swift b/LeStorage/Codables/ApiCall.swift index 3aa3696..8f362d7 100644 --- a/LeStorage/Codables/ApiCall.swift +++ b/LeStorage/Codables/ApiCall.swift @@ -23,6 +23,11 @@ public enum CallOption: String, Codable { public class ApiCall: ModelObject, Storable, SomeCall { + public required override init() { + self.method = .get + super.init() + } + public static func resourceName() -> String { return "apicalls_" + T.resourceName() } static func tokenExemptedMethods() -> [HTTPMethod] { return [] } diff --git a/LeStorage/Codables/DataAccess.swift b/LeStorage/Codables/DataAccess.swift index 767464b..ab428ce 100644 --- a/LeStorage/Codables/DataAccess.swift +++ b/LeStorage/Codables/DataAccess.swift @@ -12,11 +12,15 @@ class DataAccess: SyncedModelObject, SyncedStorable { static func tokenExemptedMethods() -> [HTTPMethod] { return [] } static func resourceName() -> String { return "data-access" } static func relationships() -> [Relationship] { return [] } - + + override required init() { + super.init() + } + var id: String = Store.randomId() - var sharedWith: [String] - var modelName: String - var modelId: String + var sharedWith: [String] = [] + var modelName: String = "" + var modelId: String = "" var grantedAt: Date = Date() init(owner: String, sharedWith: [String], modelName: String, modelId: String) { diff --git a/LeStorage/Codables/FailedAPICall.swift b/LeStorage/Codables/FailedAPICall.swift index 58c56f7..53786dd 100644 --- a/LeStorage/Codables/FailedAPICall.swift +++ b/LeStorage/Codables/FailedAPICall.swift @@ -13,6 +13,14 @@ class FailedAPICall: SyncedModelObject, SyncedStorable { static func tokenExemptedMethods() -> [HTTPMethod] { return [] } static func relationships() -> [Relationship] { return [] } + override required init() { + self.callId = "" + self.type = "" + self.apiCall = "" + self.error = "" + super.init() + } + var id: String = Store.randomId() /// The creation date of the call diff --git a/LeStorage/Codables/GetSyncData.swift b/LeStorage/Codables/GetSyncData.swift index e705c53..7d246b8 100644 --- a/LeStorage/Codables/GetSyncData.swift +++ b/LeStorage/Codables/GetSyncData.swift @@ -11,6 +11,14 @@ class GetSyncData: SyncedModelObject, SyncedStorable, URLParameterConvertible { var date: String = "" + override required init() { + super.init() + } + + required public init(from decoder: Decoder) throws { + fatalError("init(from:) has not been implemented") + } + static func tokenExemptedMethods() -> [HTTPMethod] { return [] } static func resourceName() -> String { diff --git a/LeStorage/Codables/Log.swift b/LeStorage/Codables/Log.swift index 95f0be2..5a223ac 100644 --- a/LeStorage/Codables/Log.swift +++ b/LeStorage/Codables/Log.swift @@ -13,11 +13,15 @@ class Log: SyncedModelObject, SyncedStorable { static func tokenExemptedMethods() -> [HTTPMethod] { return [] } static func relationships() -> [Relationship] { return [] } + override required init() { + super.init() + } + var id: String = Store.randomId() var date: Date = Date() - var message: String + var message: String = "" init(message: String) { self.message = message diff --git a/LeStorage/SyncedCollection.swift b/LeStorage/SyncedCollection.swift index 5c11eb6..3a6f4b0 100644 --- a/LeStorage/SyncedCollection.swift +++ b/LeStorage/SyncedCollection.swift @@ -183,7 +183,7 @@ public class SyncedCollection: BaseCollection, SomeSynced // MARK: - Basic operations without sync /// Adds or update an instance without synchronizing it - func addOrUpdateNoSync(_ instance: T) throws { + func addOrUpdateNoSync(_ instance: T) { self.addOrUpdateItem(instance: instance) } @@ -235,6 +235,20 @@ public class SyncedCollection: BaseCollection, SomeSynced } } + // MARK: Single calls + + fileprivate func _addsIfPostSucceeds(_ instance: T) async throws { + if let result = try await StoreCenter.main.service().post(instance) { + self.addOrUpdateNoSync(result) + } + } + + fileprivate func _updateIfPutSucceeds(_ instance: T) async throws { + if let result = try await StoreCenter.main.service().put(instance) { + self.addOrUpdateNoSync(result) + } + } + /// Sends an insert api call for the provided /// Calls copyFromServerInstance on the instance with the result of the HTTP call /// - Parameters: diff --git a/LeStorage/SyncedStorable.swift b/LeStorage/SyncedStorable.swift index c4ee339..f997542 100644 --- a/LeStorage/SyncedStorable.swift +++ b/LeStorage/SyncedStorable.swift @@ -12,6 +12,8 @@ public protocol SyncedStorable: Storable { var lastUpdate: Date { get set } var shared: Bool? { get set } + init() + /// Returns HTTP methods that do not need to pass the token to the request static func tokenExemptedMethods() -> [HTTPMethod] @@ -31,7 +33,13 @@ public protocol SideStorable { extension SyncedStorable { public static var copyServerResponse: Bool { return false } - + + func copy() -> Self { + let copy = Self() + copy.copy(from: self) + return copy + } + func getStoreId() -> String? { if let alt = self as? SideStorable { return alt.storeId