0x00 Chapter 8: Controllers
上篇文章 把各種路由都寫在了
routes.swift
檔案内
如果路由事件太多,
routes.swift
檔案就會越來越大
慢慢地就會變得難以管理與維護
是以,如何減輕路由的負擔呢?
使用
Controllers
!
使用
Controllers
還可以對
新舊版本
的
API
進行區分管理
0x01 建立 Controllers 檔案
1.Create the file in
Sources/App/Controllers
and call it
AcronymsController.swift
add the following code:
import Fluent
import Vapor
struct AcronymsController: RouteCollection {
func boot(routes: RoutesBuilder) throws {
}
}
create an
AcronymsController
that conforms to
RouteCollection
2.Add a new
route handler
after
boot(routes:)
struct AcronymsController: RouteCollection {
func boot(routes: RoutesBuilder) throws {
}
func getAllHandler(_ req: Request) throws -> EventLoopFuture<[Acronym]> {
Acronym.query(on: req.db).all()
}
}
3.Register the route in
boot(router:)
struct AcronymsController: RouteCollection {
func boot(routes: RoutesBuilder) throws {
routes.get("api", "acronyms", use: getAllHandler)
}
func getAllHandler(_ req: Request) throws -> EventLoopFuture<[Acronym]> {
Acronym.query(on: req.db).all()
}
}
4.Open
routes.swift
and
delete
the following handler
app.get("api", "acronyms") { req -> EventLoopFuture<[Acronym]> in
Acronym.query(on: req.db).all()
}
5.add the following to the end of
routes(_:)
try app.register(collection: AcronymsController())
經過這
5
個步驟,就成功的把路由
app.get("api", "acronyms")
移動到了控制器
AcronymsController
内
0x02 其他路由
在
AcronymsController.swift
内添加其他路由處理方法
- create
func createHandler(_ req: Request) throws -> EventLoopFuture<Acronym> {
let acronym = try req.content.decode(Acronym.self)
return acronym.save(on: req.db).map { acronym }
}
- retrieve a single acronym
func getHandler(_ req: Request) throws -> EventLoopFuture<Acronym> {
Acronym.find(req.parameters.get("acronymID"), on: req.db)
.unwrap(or: Abort(.notFound))
}
- update
func updateHandler(_ req: Request) throws -> EventLoopFuture<Acronym> {
let updatedAcronym = try req.content.decode(Acronym.self)
return Acronym.find(req.parameters.get("acronymID"), on: req.db)
.unwrap(or: Abort(.notFound)).flatMap { acronym in
acronym.short = updatedAcronym.short
acronym.long = updatedAcronym.long
return acronym.save(on: req.db).map {
acronym
}
}
}
- delete
func deleteHandler(_ req: Request) throws -> EventLoopFuture<HTTPStatus> {
Acronym.find(req.parameters.get("acronymID"), on: req.db)
.unwrap(or: Abort(.notFound))
.flatMap { acronym in
acronym.delete(on: req.db)
.transform(to: .noContent)
}
}
- filter group
func searchHandler(_ req: Request) throws -> EventLoopFuture<[Acronym]> {
guard let searchTerm = req.query[String.self, at: "term"] else {
throw Abort(.badRequest)
}
return Acronym.query(on: req.db).group(.or) { or in
or.filter(\.$short == searchTerm)
or.filter(\.$long == searchTerm)
}.all()
}
- first result
func getFirstHandler(_ req: Request) throws -> EventLoopFuture<Acronym> {
Acronym.query(on: req.db).first().unwrap(or: Abort(.notFound))
}
- sorting results
func sortedHandler(_ req: Request) throws -> EventLoopFuture<[Acronym]> {
Acronym.query(on: req.db).sort(\.$short, .ascending).all()
}
最後進行路由與方法的注冊
func boot(routes: RoutesBuilder) throws {
routes.get("api", "acronyms", use: getAllHandler)
routes.post("api", "acronyms", use: createHandler)
routes.get("api", "acronyms", ":acronymID", use: getHandler)
routes.put("api", "acronyms", ":acronymID", use: updateHandler)
routes.delete("api", "acronyms", ":acronymID", use: deleteHandler)
routes.get("api", "acronyms", "search", use: searchHandler)
routes.get("api", "acronyms", "first", use: getFirstHandler)
routes.get("api", "acronyms", "sorted", use: sortedHandler)
}
0x03 路由組 - Route groups
func boot(routes: RoutesBuilder) throws {
// routes.get("api", "acronyms", use: getAllHandler)
// routes.post("api", "acronyms", use: createHandler)
// routes.get("api", "acronyms", ":acronymID", use: getHandler)
// routes.put("api", "acronyms", ":acronymID", use: updateHandler)
// routes.delete("api", "acronyms", ":acronymID", use: deleteHandler)
// routes.get("api", "acronyms", "search", use: searchHandler)
// routes.get("api", "acronyms", "first", use: getFirstHandler)
// routes.get("api", "acronyms", "sorted", use: sortedHandler)
let acronymsRoutes = routes.grouped("api", "acronyms")
acronymsRoutes.get(use: getAllHandler)
acronymsRoutes.post(use: createHandler)
acronymsRoutes.get(":acronymID", use: getHandler)
acronymsRoutes.put(":acronymID", use: updateHandler)
acronymsRoutes.delete(":acronymID", use: deleteHandler)
acronymsRoutes.get("search", use: searchHandler)
acronymsRoutes.get("first", use: getFirstHandler)
acronymsRoutes.get("sorted", use: sortedHandler)
}