Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

2 лет назад
12345678910111213141516171819202122232425262728293031323334
  1. import Fluent
  2. import Vapor
  3. struct TodoController: RouteCollection {
  4. func boot(routes: RoutesBuilder) throws {
  5. let todos = routes.grouped("todos")
  6. todos.get(use: { try await self.index(req: $0) })
  7. todos.post(use: { try await self.create(req: $0) })
  8. todos.group(":todoID") { todo in
  9. todo.delete(use: { try await self.delete(req: $0) })
  10. }
  11. }
  12. func index(req: Request) async throws -> [Todo] {
  13. try await Todo.query(on: req.db).all()
  14. }
  15. func create(req: Request) async throws -> Todo {
  16. let todo = try req.content.decode(Todo.self)
  17. try await todo.save(on: req.db)
  18. return todo
  19. }
  20. func delete(req: Request) async throws -> HTTPStatus {
  21. guard let todo = try await Todo.find(req.parameters.get("todoID"), on: req.db) else {
  22. throw Abort(.notFound)
  23. }
  24. try await todo.delete(on: req.db)
  25. return .noContent
  26. }
  27. }