Skip to content
This repository was archived by the owner on Apr 20, 2024. It is now read-only.

Commit

Permalink
Add support for soft deleted and restoring
Browse files Browse the repository at this point in the history
  • Loading branch information
steffendsommer committed Jul 9, 2018
1 parent 5192233 commit a18f581
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions Sources/Sugar/Helpers/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ public extension Model {

public func saveOrUpdate (
given filters: [Self.Database.QueryFilter],
withSoftDeleted: Bool = false,
restore: Bool = false,
on db: DatabaseConnectable
) throws -> Future<Self> {
var query = Self.query(on: db)
var query = Self.query(on: db, withSoftDeleted: withSoftDeleted)

for filter in filters {
query = query.filter(filter)
}
Expand All @@ -30,7 +33,13 @@ public extension Model {
copy.fluentCreatedAt = result.fluentCreatedAt
copy.fluentDeletedAt = result.fluentDeletedAt

return copy.save(on: db)
let future = copy.update(on: db)

guard restore else {
return future
}

return future.flatMap { $0.restore(on: db) }
}
}
}
Expand Down

0 comments on commit a18f581

Please sign in to comment.