Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hasNext and hasPrevious properties #482

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions Sources/FluentKit/Query/Builder/QueryBuilder+Paginate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ extension Page: Decodable where T: Decodable {}

/// Metadata for a given `Page`.
public struct PageMetadata: Codable {
/// Indicates if there is a page after this one.
public let hasNext: Bool

/// Indicates if there is a page before this one.
public let hasPrevious: Bool

/// Current page number. Starts at `1`.
public let page: Int

Expand All @@ -99,9 +105,23 @@ public struct PageMetadata: Codable {
///. - per: Max items per page.
///. - total: Total number of items available.
public init(page: Int, per: Int, total: Int) {
self.page = page
self.per = per
self.total = total
self.page = page < 1 ? 1 : page
self.per = per < 1 ? 1 : per
self.total = total < 0 ? 0 : total

// Find out how many items are left in the pagination object
let remainingItems = total - (per * page)
if remainingItems <= 0 {
self.hasNext = false
} else {
self.hasNext = true
}
Comment on lines +113 to +118

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do we feel about one-liners like below?

Suggested change
let remainingItems = total - (per * page)
if remainingItems <= 0 {
self.hasNext = false
} else {
self.hasNext = true
}
self.hasNext = (total - (per * page)) <= 0
self.hasPrevious = page > 1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I'd choose the existing code, it's a bit easier to read at a glance. But I don't have a strong opinion on it


if (page > 1) {
self.hasPrevious = true
} else {
self.hasPrevious = false
}
}
}

Expand Down
20 changes: 20 additions & 0 deletions Tests/FluentKitTests/FluentKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,26 @@ final class FluentKitTests: XCTestCase {
}
}
}

func testPageMetadataFields() throws {
let met = PageMetadata(page: 2, per: 5, total: 17)
XCTAssertEqual(met.page, 2)
XCTAssertEqual(met.per, 5)
XCTAssertEqual(met.total, 17)
XCTAssertEqual(met.pageCount, 4)
XCTAssertEqual(met.hasNext, true)
XCTAssertEqual(met.hasPrevious, true)
}

func testPageMetadataFieldsWithLowerBounds() throws {
let met = PageMetadata(page: 0, per: 0, total: -1)
XCTAssertEqual(met.page, 1)
XCTAssertEqual(met.per, 1)
XCTAssertEqual(met.total, 0)
XCTAssertEqual(met.pageCount, 1)
XCTAssertEqual(met.hasNext, false)
XCTAssertEqual(met.hasPrevious, false)
}
}

final class User: Model, @unchecked Sendable {
Expand Down
Loading