Guest User

Untitled

a guest
Oct 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. import Routing
  2. import Vapor
  3.  
  4. public func routes(_ router: Router) throws {
  5.  
  6. router.group("todo") { (router) in
  7.  
  8. // Create Todo
  9. router.post(Todo.self) { req, todo -> Future<Todo> in
  10. return todo.save(on: req)
  11. }
  12.  
  13. // Get single Todo
  14. router.get(Int.parameter) { req -> Future<Todo> in
  15. let id = try req.parameters.next(Int.self)
  16. return Todo.find(id, on: req).map { (todo) -> Todo in
  17. guard let todo = todo else {
  18. throw Abort(.notFound)
  19. }
  20. return todo
  21. }
  22. }
  23.  
  24. // Get All Todos
  25. router.get() { req -> Future<[Todo]> in
  26. return Todo.query(on: req).all()
  27. }
  28.  
  29. // Update single todo
  30. router.put(Todo.self) { (req, updateTodo) -> Future<Todo> in
  31.  
  32. guard let id = updateTodo.id else {
  33. throw Abort(.notFound)
  34. }
  35.  
  36. return Todo.find(id, on: req).flatMap(to: Todo.self) { (todo) -> Future<Todo> in
  37. guard var todo = todo else {
  38. throw Abort(.notFound)
  39. }
  40. todo = updateTodo
  41. return todo.save(on: req)
  42. }
  43. }
  44. }
  45. }
Add Comment
Please, Sign In to add comment