Created
January 11, 2019 21:04
-
-
Save tanner0101/d17c02bfff784ab33f73f121a5279890 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
r.get("posts", ":postID") { req in | |
let postID = try req.parameters.get(":postID") | |
print(postID) // String | |
... | |
} | |
enum PathComponent { | |
case part(String) | |
case dynamic(name: String) | |
case anything | |
case catchall | |
} | |
extension PathComponent: ExpressibleByStringLiteral { | |
init(stringLiteral value: String) { | |
if value.hasPrefix(":") { | |
self = .dynamic(name: value[1...]) | |
} else if value == ":" { | |
self = .anything | |
} else if value == "*" { | |
self = .catchall | |
} else { | |
self = .part(value) | |
} | |
} | |
} | |
extension PathComponent { | |
static var postID: PathComponent { | |
return ":postID" | |
} | |
} | |
extension PathComponent { | |
static var postID: PathComponent { | |
return .dynamic(name: "postID") | |
} | |
} | |
r.get("posts", .postID) { req in | |
let postID = try req.parameters.get(.postID) | |
print(postID) // String | |
... | |
} | |
try req.parameters.get(.postID, as: Int.self) | |
return try Post.find(req.parameters.get(.postID), on: self.db).first() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment