diff --git a/couscous.yml b/couscous.yml index 2f5d84fc..f38c2c66 100644 --- a/couscous.yml +++ b/couscous.yml @@ -78,8 +78,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 diff --git a/routing/query-parameters.md b/routing/query-parameters.md new file mode 100644 index 00000000..f918fca4 --- /dev/null +++ b/routing/query-parameters.md @@ -0,0 +1,34 @@ +--- +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 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)" +} +```