Skip to content

Commit

Permalink
improved sort method to handle a default direction (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
gperdomor committed Jul 30, 2018
1 parent d67f4e0 commit a618020
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -73,8 +73,8 @@ The extensions are grouped in 3 modules, `AsyncExt`, `FluentExt` and `ServiceExt
- `filter(\_ keyPath:, at parameter:, on req:)` to handle automatic filters based in query params
- New sort methods:

- `sort(\_ keyPath:, at queryParam:, as parameter:, on req:)` to handle automatic sorting based in query params
- `sort(\_ keyPath:, as parameter:, on req:)` to handle automatic sorting based in query params
- `sort(\_ keyPath:, at queryParam:, as parameter:, default direction:, on req:)` to handle automatic sorting based in query params
- `sort(\_ keyPath:, as parameter:, default direction:, on req:)` to handle automatic sorting based in query params

#### Query params sintax for filters:

Expand Down Expand Up @@ -113,7 +113,7 @@ You can set the sorts methods with this format `sort=field:direction,field:direc
```swift
return try User.query(on: req)
.sort(\User.username, as: "username", on: req)
.sort(\User.createdAt, as: "created_at", on: req)
.sort(\User.createdAt, as: "created_at", default: .ascending, on: req) // if created_at is not present in the url, then the sort is applied using the default direction
```

### ServiceExt
Expand Down
38 changes: 21 additions & 17 deletions Sources/FluentExt/QueryBuilder+Sort.swift
Expand Up @@ -16,32 +16,35 @@ public extension QueryBuilder where Result: Model, Result.Database == Database {
/// - keyPath: the model keypath.
/// - queryParam: the sorting parameter name in the query params url.
/// - parameter: the parameter name in sorting config.
/// - direction: Default direction to apply if no value is found in url query params.
/// - req: the request.
/// - Returns: Self
/// - Throws: FluentError
public func sort<T>(_ keyPath: KeyPath<Result, T>, at queryParam: String, as parameter: String, on req: Request) throws -> Self {
guard let sort = req.query[String.self, at: queryParam] else {
return self
}
public func sort<T>(_ keyPath: KeyPath<Result, T>, at queryParam: String, as parameter: String, default direction: Database.QuerySortDirection? = nil, on req: Request) throws -> Self {
if let sort = req.query[String.self, at: queryParam] {
let sortOpts = sort.components(separatedBy: ",")

let sortOpts = sort.components(separatedBy: ",")
for option in sortOpts {
let splited = option.components(separatedBy: ":")

for option in sortOpts {
let splited = option.components(separatedBy: ":")
let field = splited[0]

let field = splited[0]
if field != parameter {
continue
}

if field != parameter {
continue
}
let direction = splited.count == 1 ? "asc" : splited[1]

let direction = splited.count == 1 ? "asc" : splited[1]
guard ["asc", "desc"].contains(direction) else {
throw FluentError(identifier: "invalidSortConfiguration", reason: "Invalid sort config for '\(option)'")
}

guard ["asc", "desc"].contains(direction) else {
throw FluentError(identifier: "invalidSortConfiguration", reason: "Invalid sort config for '\(option)'")
return self.sort(keyPath, direction == "asc" ? Database.querySortDirectionAscending : Database.querySortDirectionDescending)
}
}

return self.sort(keyPath, direction == "asc" ? Database.querySortDirectionAscending : Database.querySortDirectionDescending)
if let direction = direction {
return self.sort(keyPath, direction)
}

return self
Expand All @@ -52,10 +55,11 @@ public extension QueryBuilder where Result: Model, Result.Database == Database {
/// - Parameters:
/// - keyPath: the model keypath.
/// - parameter: the parameter name in sorting config.
/// - direction: Default direction to apply if no value is found in url query params.
/// - req: the request.
/// - Returns: Self
/// - Throws: FluentError
public func sort<T>(_ keyPath: KeyPath<Result, T>, as parameter: String, on req: Request) throws -> Self {
return try self.sort(keyPath, at: "sort", as: parameter, on: req)
public func sort<T>(_ keyPath: KeyPath<Result, T>, as parameter: String, default direction: Database.QuerySortDirection? = nil, on req: Request) throws -> Self {
return try self.sort(keyPath, at: "sort", as: parameter, default: direction, on: req)
}
}

0 comments on commit a618020

Please sign in to comment.