|
|
|
|
@ -14,15 +14,20 @@ enum StoredCollectionError: Error { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protocol SomeCollection: Identifiable { |
|
|
|
|
var resourceName: String { get } |
|
|
|
|
var synchronized: Bool { get } |
|
|
|
|
|
|
|
|
|
func allItems() -> [any Storable] |
|
|
|
|
func deleteById(_ id: String) throws |
|
|
|
|
func deleteApiCallById(_ id: String) throws |
|
|
|
|
func reset() |
|
|
|
|
|
|
|
|
|
func loadDataFromServerIfAllowed() async throws |
|
|
|
|
var synchronized: Bool { get } |
|
|
|
|
|
|
|
|
|
func hasPendingAPICalls() -> Bool |
|
|
|
|
var resourceName: String { get } |
|
|
|
|
func contentOfApiCallFile() -> String? |
|
|
|
|
|
|
|
|
|
func reset() |
|
|
|
|
func resetApiCalls() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
extension Notification.Name { |
|
|
|
|
@ -403,17 +408,18 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if let existingCall = apiCallCollection.first(where: { $0.dataId == instance.id }) { |
|
|
|
|
switch existingCall.method { |
|
|
|
|
case HTTPMethod.post.rawValue, HTTPMethod.put.rawValue: |
|
|
|
|
switch method { |
|
|
|
|
case .delete: |
|
|
|
|
try self.deleteApiCallById(existingCall.id) // delete the existing call as we don't need it |
|
|
|
|
if existingCall.method == HTTPMethod.post.rawValue { |
|
|
|
|
return nil // if the post has not been done, we can just stop here |
|
|
|
|
} else { |
|
|
|
|
return try self._createCall(instance, method: method) // otherwise it's a put and we want to send the delete |
|
|
|
|
} |
|
|
|
|
default: // here we should only trying to PUT, so we update the existing POST/PUT with the instance new values |
|
|
|
|
existingCall.body = try instance.jsonString() |
|
|
|
|
return existingCall |
|
|
|
|
case HTTPMethod.delete.rawValue: |
|
|
|
|
try self.deleteApiCallById(existingCall.id) |
|
|
|
|
return nil |
|
|
|
|
default: |
|
|
|
|
throw StoredCollectionError.unmanagedHTTPMethod(method: existingCall.method) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
return try self._createCall(instance, method: method) |
|
|
|
|
} |
|
|
|
|
|