25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 

32 satır
871 B

  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: index)
  7. todos.post(use: create)
  8. todos.group(":todoID") { todo in
  9. todo.delete(use: delete)
  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. }