Non puoi selezionare più di 25 argomenti
Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
|
- 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) async throws -> [Todo] {
- try await Todo.query(on: req.db).all()
- }
-
- func create(req: Request) async throws -> Todo {
- let todo = try req.content.decode(Todo.self)
- try await todo.save(on: req.db)
- return todo
- }
-
- func delete(req: Request) async throws -> HTTPStatus {
- guard let todo = try await Todo.find(req.parameters.get("todoID"), on: req.db) else {
- throw Abort(.notFound)
- }
- try await todo.delete(on: req.db)
- return .noContent
- }
- }
|