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

Commit

Permalink
Merge pull request #101 from nodes-vapor/feature/fix-saveOrUpdate-beh…
Browse files Browse the repository at this point in the history
…avior

Use create instead of save
  • Loading branch information
siemensikkema authored Feb 7, 2020
2 parents 2090c3e + 085b0bd commit fbee74f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ jobs:
- run:
name: Install dependencies
command: |
export HOMEBREW_NO_AUTO_UPDATE=1
brew tap vapor/homebrew-tap
brew install cmysql
brew install ctls
Expand Down
12 changes: 3 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
*.swp
Packages
.build
.idea
xcuserdata
*.xcodeproj
Config/secrets/
.DS_Store
node_modules/
bower_components/
.swift-version
Package.pins
*.xcodeproj
DerivedData/
Package.resolved
.swiftpm
19 changes: 17 additions & 2 deletions Sources/Sugar/Helpers/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,22 @@ public extension Model {
.unwrap(or: Abort(.notFound, reason: "\(Self.self) with id \(id) not found"))
}

func saveOrUpdate (
@available(*, deprecated, message: "use `creatOrUpdate(given:withSoftDeleted:restore:on)`")
func saveOrUpdate(
given filters: [FilterOperator<Self.Database, Self>],
withSoftDeleted: Bool = false,
restore: Bool = false,
on db: DatabaseConnectable
) throws -> Future<Self> {
return try createOrUpdate(
given: filters,
withSoftDeleted: withSoftDeleted,
restore: restore,
on: db
)
}

func createOrUpdate(
given filters: [FilterOperator<Self.Database, Self>],
withSoftDeleted: Bool = false,
restore: Bool = false,
Expand All @@ -25,7 +40,7 @@ public extension Model {

return query.first().flatMap(to: Self.self) { result in
guard let result = result else {
return self.save(on: db)
return self.create(on: db)
}

var copy = self
Expand Down

0 comments on commit fbee74f

Please sign in to comment.