The TodoController class introduced previously is declared in TodoController.swift:
// File: /Sources/App/Controllers/TodoController.swift
import Vapor
/// Controlers basic CRUD operations on `Todo`s.
final class TodoController { // [1]
/// Returns a list of all `Todo`s.
func index(_ req: Request) throws -> Future<[Todo]> { // [2]
return Todo.query(on: req).all()
}
/// Saves a decoded `Todo` to the database.
func create(_ req: Request) throws -> Future<Todo> {
return try req.content.decode(Todo.self).flatMap { todo in // [3]
return todo.save(on: req)
}
}
/// Deletes a parameterized `Todo`.
func delete(_ req: Request) throws -> Future<HTTPStatus> {
return try req.parameters.next(Todo.self).flatMap { todo in // [4]
return todo.delete(on: req)
}.transform(to: .ok) // [5]
}
}
Some interesting techniques are noted in the preceding...