Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. func edit(request:Request, id:String) throws -> ResponseRepresentable {
  2. // Check if the password came in POST request:
  3. let password = request.data["password"]?.string
  4.  
  5. // Check if the request has a new set of roles
  6. let roles = request.data["roles"]?.array
  7.  
  8. let user:ClinicUser = try ClinicUser.edit(id: id, password: password, roles: roles)
  9. return user
  10. }
  11.  
  12. static func edit(id:String, password:String?, roles:Array<NodeRepresentable>?) throws -> ClinicUser {
  13. guard var user:ClinicUser = try ClinicUser.find(id) else {
  14. throw Abort.notFound
  15. }
  16. // Is it the best way of doing this? Because with "guard" I should "return" or "throw", right?
  17. if password != nil {
  18. user.password = try BCrypt.hash(password: password!)
  19. }
  20.  
  21. // TODO: update user's roles relationships
  22.  
  23. try user.save()
  24.  
  25. return user
  26. }
  27.  
  28. let user:ClinicUser = try ClinicUser.edit(id: id, password: password, roles: roles as! Array<NodeRepresentable>)
  29.  
  30. static func edit(id:String, fieldA:String?, fieldN:String, etc..) throws -> ClinicUser {
  31. // If fieldA is available, update fieldA:
  32. if fieldA != nil {
  33. model.fieldA = fieldA
  34. }
  35.  
  36. // If fieldN is available, update fieldN:
  37. if fieldN != nil {
  38. model.fieldN = fieldN
  39. }
  40.  
  41. // After update all fields, save:
  42. try model.save()
  43.  
  44. // Return the updated model:
  45. return model
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement