-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a max-page-size config option and paging for search results (#1485)
Adds a `max-page-size` option to configure the maximum size when returning lists from the api. For the search endpoint, enable paging parameters `page` and `pageSize`. The `pageSize` will be capped to the `max-page-size` config value. Refs: #1294
- Loading branch information
Showing
13 changed files
with
115 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package sharry.common | ||
|
||
final case class Page(limit: Int, offset: Int): | ||
def next: Page = Page(limit, offset + limit) | ||
def prev: Page = Page(limit, math.max(0, offset - limit)) | ||
def capped(max: Int): Page = copy(limit = math.min(max, limit)) | ||
|
||
object Page: | ||
|
||
def one(size: Int): Page = Page(size, 0) | ||
|
||
def page(pageNum: Int, pageSize: Int): Page = | ||
if (pageNum <= 1) one(pageSize) | ||
else Page(pageSize, (pageNum - 1) * pageSize) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
modules/restserver/src/main/scala/sharry/restserver/routes/Params.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package sharry.restserver.routes | ||
|
||
import sharry.common.Page as CPage | ||
|
||
import org.http4s.dsl.impl.OptionalQueryParamDecoderMatcher | ||
|
||
object Params: | ||
|
||
object Query extends OptionalQueryParamDecoderMatcher[String]("q") | ||
|
||
object Page { | ||
def unapply(params: Map[String, collection.Seq[String]]): Option[CPage] = | ||
val pageNum = params.get("page").flatMap(_.headOption).flatMap(_.toIntOption) match | ||
case Some(n) if n >= 1 => n | ||
case _ => 1 | ||
|
||
val pageSize = | ||
params.get("pageSize").flatMap(_.headOption).flatMap(_.toIntOption) match | ||
case Some(n) if n >= 1 => n | ||
case _ => 100 | ||
|
||
Some(CPage.page(pageNum, pageSize)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters