Skip to content

Commit

Permalink
Merge pull request #16 from staffler-xyz/feature/remove-workaround
Browse files Browse the repository at this point in the history
Remove workaround
  • Loading branch information
anthonycastelli authored Oct 10, 2018
2 parents d018e2a + a4fa8f9 commit 538dcab
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
4 changes: 0 additions & 4 deletions Sources/Pagination/QueryBuilder+Paginatable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@ extension QueryBuilder where Result: Paginatable, Result.Database == Database {
// Require page 1 or greater
let page = page > 0 ? page : 1

// For now until https://github.com/vapor/fluent/issues/518 is fixed, this is the work around.
let copy = self.query

// Return a full count
return self.count().flatMap(to: Page<Result>.self) { total in
self.query = copy
// Limit the query to the desired page
let lowerBound = (page - 1) * per
Database.queryRangeApply(lower: lowerBound, upper: lowerBound + per, to: &self.query)
Expand Down
5 changes: 3 additions & 2 deletions Tests/PaginationTests/PaginationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ final class PaginationTests: XCTestCase {
}

func testQuery() throws {
let models = try [
let models: [TestModel] = try [
TestModel.create(name: "Test 1", on: self.sqlConnection),
TestModel.create(name: "Test 2", on: self.sqlConnection),
TestModel.create(name: "Test 3", on: self.sqlConnection),
TestModel.create(name: "Test 4", on: self.sqlConnection),
TestModel.create(name: "Test 5", on: self.sqlConnection)
]

let pageInfo = try TestModel.query(on: self.sqlConnection).paginate(page: 1).wait()
let pageInfo = try TestModel.query(on: self.sqlConnection).sort(\.id).paginate(page: 1).wait()
XCTAssertEqual(pageInfo.total, models.count)
XCTAssertEqual(pageInfo.data, models)
}

func testFilterQuery() throws {
Expand Down
6 changes: 6 additions & 0 deletions Tests/PaginationTests/TestModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,9 @@ extension TestModel {
return try TestModel(name: name).save(on: connection).wait()
}
}

extension TestModel: Equatable {
static func == (lhs: TestModel, rhs: TestModel) -> Bool {
return lhs.id == rhs.id
}
}

0 comments on commit 538dcab

Please sign in to comment.