From 49151b4caf5f27f8eee8055fbb4bf9d2ac96b0b3 Mon Sep 17 00:00:00 2001 From: Eneko Alonso Date: Sun, 11 Sep 2016 09:42:02 -0700 Subject: [PATCH 1/3] Create query-parameters.md --- routing/query-parameters.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 routing/query-parameters.md diff --git a/routing/query-parameters.md b/routing/query-parameters.md new file mode 100644 index 00000000..c7526c03 --- /dev/null +++ b/routing/query-parameters.md @@ -0,0 +1,35 @@ +--- +currentMenu: routing-query-parameters +--- + +# Query Parameters + +Request query parameters can be accessed either as a dictionary or using the `extract` syntax which throws instead of returning an optional. + +## Optional Syntax + +Optional syntax is the easiest way to handle optional query parameters. + +```swift +drop.get("comments") { request in + if let rating = request.query?["rating"]?.int { + return "You requested comments with rating greater than #\(rating)" + } + return "You requested all comments" +} +``` + +## Extract Syntax + +Extract syntax might be useful to *enforce* the presence of query parameters and throw an exception if they are not present. +To use this syntax first we need to ensure the query object is present with a `guard`. + +```swift +drop.get("comments") { request in + guard let query = request.query else { + throw Abort.badRequest + } + let rating = try query.extract("rating") as Int + return "You requested comments with rating greater than #\(rating)" +} +``` From faaacfc36846973105f0da45d589ad13dff4b6e7 Mon Sep 17 00:00:00 2001 From: Eneko Alonso Date: Sun, 11 Sep 2016 09:43:15 -0700 Subject: [PATCH 2/3] Add Query Parameters page --- couscous.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/couscous.yml b/couscous.yml index 9caf2213..6df02d3d 100644 --- a/couscous.yml +++ b/couscous.yml @@ -69,8 +69,11 @@ menu: text: Basic relativeUrl: routing/basic.html routing-parameters: - text: Parameters + text: Route Parameters relativeUrl: routing/parameters.html + routing-query-parameters: + text: Query Parameters + relativeUrl: routing/query-parameters.html routing-group: text: Group relativeUrl: routing/group.html From cd3a0dcb1a3bd3a9751f6f5fded67a615ccac9c7 Mon Sep 17 00:00:00 2001 From: Casper Rasmussen Date: Fri, 13 Jan 2017 20:03:17 +0100 Subject: [PATCH 3/3] Update query-parameters.md --- routing/query-parameters.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/routing/query-parameters.md b/routing/query-parameters.md index c7526c03..f918fca4 100644 --- a/routing/query-parameters.md +++ b/routing/query-parameters.md @@ -26,10 +26,9 @@ To use this syntax first we need to ensure the query object is present with a `g ```swift drop.get("comments") { request in - guard let query = request.query else { - throw Abort.badRequest - } - let rating = try query.extract("rating") as Int + guard let rating = request.query?["rating"]?.int else { + throw Abort.custom(status: .preconditionFailed, message: "Please include a rating") + } return "You requested comments with rating greater than #\(rating)" } ```