Skip to content

Commit

Permalink
Better handleof page limits in Pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
amitaibu committed Oct 21, 2021
1 parent a8bcb77 commit cb082a7
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions IHP/Pagination/ControllerFunctions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,13 @@ paginateWithOptions :: forall controller table queryBuilderProvider joinRegister
-> queryBuilderProvider table
-> IO (queryBuilderProvider table, Pagination)
paginateWithOptions options query =
let page = paramOrDefault @Int 1 "page"
pageSize = paramOrDefault @Int (maxItems options) "maxItems"
let
-- Page and page size shouldn't be lower than 1.
page = max 1 $ paramOrDefault @Int 1 "page"
-- We limit the page size to a maximum of 200, to prevent users from
-- passing in query params with a value that could overload the
-- database (e.g. maxItems=100000)
pageSize = min (max 1 $ paramOrDefault @Int (maxItems options) "maxItems") 200
in do
count <- query
|> fetchCount
Expand All @@ -102,10 +107,8 @@ paginateWithOptions options query =
}

let results = query
-- We limit the page size to a maximum of 200, to prevent users from
-- passing in query params with a value that could overload the
-- database (e.g. maxItems=100000)
|> limit (min pageSize 200)

|> limit pageSize
|> offset ((page - 1) * pageSize)

pure
Expand Down

0 comments on commit cb082a7

Please sign in to comment.