Adds direct post/call calls before adding to the collection

sync_v2
Laurent 7 months ago
parent 51163af3a3
commit af5c677ded
  1. 5
      LeStorage/Codables/ApiCall.swift
  2. 12
      LeStorage/Codables/DataAccess.swift
  3. 8
      LeStorage/Codables/FailedAPICall.swift
  4. 8
      LeStorage/Codables/GetSyncData.swift
  5. 6
      LeStorage/Codables/Log.swift
  6. 16
      LeStorage/SyncedCollection.swift
  7. 10
      LeStorage/SyncedStorable.swift

@ -23,6 +23,11 @@ public enum CallOption: String, Codable {
public class ApiCall<T: Storable>: 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 [] }

@ -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) {

@ -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

@ -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 {

@ -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

@ -183,7 +183,7 @@ public class SyncedCollection<T : SyncedStorable>: BaseCollection<T>, 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<T : SyncedStorable>: BaseCollection<T>, 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:

@ -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

Loading…
Cancel
Save