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 #14 from nodes-vapor/remove-filter
Browse files Browse the repository at this point in the history
Removed auto filter
  • Loading branch information
BrettRToomey authored Feb 15, 2017
2 parents fc3b1e4 + 5563a2b commit 8cd9a49
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 94 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.swp
Packages
.build
.idea
Expand All @@ -7,4 +8,4 @@ Config/secrets/
.DS_Store
node_modules/
bower_components/
.swift-version
.swift-version
Binary file removed Sources/.NodesModel.swift.swp
Binary file not shown.
92 changes: 0 additions & 92 deletions Sources/NodesModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,95 +53,3 @@ extension NodesModel {
}
}
}

extension NodesModel {
public static func find(_ id: NodeRepresentable) throws -> Self? {
guard let idKey = database?.driver.idKey else {
return nil
}

var query = try Self.query().filter(idKey, .equals, id)
if Self.softDeletable {
query = try query.filter("deletedAt", .equals, Node.null)
}

return try query.first()
}

public static func all() throws -> [Self] {
var query = try Self.query()
if Self.softDeletable {
query = try query.filter("deletedAt", .equals, Node.null)
}

return try query.all()
}
}

extension Query where T: NodesModel {
public func first() throws -> T? {
let query = self
query.action = .fetch
query.limit = Limit(count: 1)

if T.softDeletable {
query.filters = [
Filter(T.self, .compare("deletedAt", .equals, Node.null))
]
}

var model = try query.run().first
model?.exists = true

return model
}

public func all() throws -> [T] {
let query = self
query.action = .fetch

if T.softDeletable {
query.filters = [
Filter(T.self, .compare("deletedAt", .equals, Node.null))
]
}

let models = try query.run()
models.forEach { model in
var model = model
model.exists = true
}

return models
}


@discardableResult
public func run() throws -> [T] {
var models: [T] = []

// filter out soft-deleted models on fetch and count queries
if action == .fetch || action == .count, T.softDeletable {
let filter = Filter(T.self, .compare("deletedAt", .equals, Node.null))
filters.append(filter)
}

if case .array(let array) = try raw() {
for result in array {
do {
var model = try T(node: result)
if case .object(let dict) = result {
model.id = dict["id"]
}
models.append(model)
} catch {
print("Could not initialize \(T.self), skipping: \(error)")
}
}
} else {
print("Unsupported Node type, only array is supported.")
}

return models
}
}
1 change: 0 additions & 1 deletion Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ import XCTest
XCTMain([
testCase(DatabaseMySQLTests.allTests),
testCase(DateSugarTests.allTests),
testCase(NodesModelTests.allTests),
])
2 changes: 2 additions & 0 deletions Tests/SugarTests/NodesModelTests.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/*
import Vapor
import Fluent
import XCTest
Expand Down Expand Up @@ -107,3 +108,4 @@ struct TestUser: NodesModel {
static func prepare(_ database: Database) throws {}
static func revert(_ database: Database) throws {}
}
*/

0 comments on commit 8cd9a49

Please sign in to comment.