on c3c3690 help sign in
WebSocketTest/Sources/App/Controllers/TodoController.swift
23 lines 710 B history raw
1import Vapor
3/// Controls basic CRUD operations on `Todo`s.
4final class TodoController {
5 /// Returns a list of all `Todo`s.
6 func index(_ req: Request) throws -> Future<[Todo]> {
7 return Todo.query(on: req).all()
8 }
10 /// Saves a decoded `Todo` to the database.
11 func create(_ req: Request) throws -> Future<Todo> {
12 return try req.content.decode(Todo.self).flatMap { todo in
13 return todo.save(on: req)
14 }
15 }
17 /// Deletes a parameterized `Todo`.
18 func delete(_ req: Request) throws -> Future<HTTPStatus> {
19 return try req.parameters.next(Todo.self).flatMap { todo in
20 return todo.delete(on: req)
21 }.transform(to: .ok)
22 }