Fix tryPutBeforeUpdating to graciously fail

sync_v2
Laurent 7 months ago
parent e4dac9ff43
commit 1ed6a9a295
  1. 12
      LeStorage/Services.swift
  2. 7
      LeStorage/StoredSingleton.swift
  3. 2
      LeStorage/SyncedStorable.swift

@ -496,22 +496,22 @@ public class Services {
return try await self._runRequest(getRequest) return try await self._runRequest(getRequest)
} }
/// Executes a POST request /// Executes a POST request on the generated DRF services corresponding to T
public func rawPost<T: Storable>(_ instance: T) async throws -> T {
public func post<T: Storable>(_ instance: T) async throws -> T {
var postRequest = try self._postRequest(type: T.self) var postRequest = try self._postRequest(type: T.self)
postRequest.httpBody = try JSON.encoder.encode(instance) postRequest.httpBody = try JSON.encoder.encode(instance)
return try await self._runRequest(postRequest) return try await self._runRequest(postRequest)
} }
/// Executes a PUT request /// Executes a PUT request on the generated DRF services corresponding to T
public func put<T: Storable>(_ instance: T) async throws -> T { public func rawPut<T: Storable>(_ instance: T) async throws -> T {
var postRequest = try self._putRequest(type: T.self, id: instance.stringId) var postRequest = try self._putRequest(type: T.self, id: instance.stringId)
postRequest.httpBody = try JSON.encoder.encode(instance) postRequest.httpBody = try JSON.encoder.encode(instance)
return try await self._runRequest(postRequest) return try await self._runRequest(postRequest)
} }
public func delete<T: Storable>(_ instance: T) async throws -> T { /// Executes a DELETE request on the generated DRF services corresponding to T
public func rawDelete<T: Storable>(_ instance: T) async throws -> T {
let deleteRequest = try self._deleteRequest(type: T.self, id: instance.stringId) let deleteRequest = try self._deleteRequest(type: T.self, id: instance.stringId)
return try await self._runRequest(deleteRequest) return try await self._runRequest(deleteRequest)
} }

@ -27,12 +27,11 @@ public class StoredSingleton<T: SyncedStorable>: SyncedCollection<T> {
return self.items.first return self.items.first
} }
public func tryPutBeforeUpdating(_ instance: T) async throws { @discardableResult public func tryPutBeforeUpdating(_ instance: T) async throws -> T? {
if let result = try await StoreCenter.main.service().put(instance) { let result = try await StoreCenter.main.service().rawPut(instance)
self.setItemNoSync(result) self.setItemNoSync(result)
return result
} }
}
// MARK: - Protects from use // MARK: - Protects from use

@ -30,7 +30,7 @@ public protocol SideStorable {
var storeId: String? { get set } var storeId: String? { get set }
} }
extension SyncedStorable { public extension SyncedStorable {
func copy() -> Self { func copy() -> Self {
let copy = Self() let copy = Self()

Loading…
Cancel
Save