30 lines
872 B
Swift
30 lines
872 B
Swift
import Fluent
|
|
import Vapor
|
|
|
|
struct TodoController: RouteCollection {
|
|
func boot(routes: RoutesBuilder) throws {
|
|
let todos = routes.grouped("todos")
|
|
todos.get(use: index)
|
|
todos.post(use: create)
|
|
todos.group(":todoID") { todo in
|
|
todo.delete(use: delete)
|
|
}
|
|
}
|
|
|
|
func index(req: Request) throws -> EventLoopFuture<[Todo]> {
|
|
return Todo.query(on: req.db).all()
|
|
}
|
|
|
|
func create(req: Request) throws -> EventLoopFuture<Todo> {
|
|
let todo = try req.content.decode(Todo.self)
|
|
return todo.save(on: req.db).map { todo }
|
|
}
|
|
|
|
func delete(req: Request) throws -> EventLoopFuture<HTTPStatus> {
|
|
return Todo.find(req.parameters.get("todoID"), on: req.db)
|
|
.unwrap(or: Abort(.notFound))
|
|
.flatMap { $0.delete(on: req.db) }
|
|
.transform(to: .ok)
|
|
}
|
|
}
|