Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func edit(request:Request, id:String) throws -> ResponseRepresentable {
- // Check if the password came in POST request:
- let password = request.data["password"]?.string
- // Check if the request has a new set of roles
- let roles = request.data["roles"]?.array
- let user:ClinicUser = try ClinicUser.edit(id: id, password: password, roles: roles)
- return user
- }
- static func edit(id:String, password:String?, roles:Array<NodeRepresentable>?) throws -> ClinicUser {
- guard var user:ClinicUser = try ClinicUser.find(id) else {
- throw Abort.notFound
- }
- // Is it the best way of doing this? Because with "guard" I should "return" or "throw", right?
- if password != nil {
- user.password = try BCrypt.hash(password: password!)
- }
- // TODO: update user's roles relationships
- try user.save()
- return user
- }
- let user:ClinicUser = try ClinicUser.edit(id: id, password: password, roles: roles as! Array<NodeRepresentable>)
- static func edit(id:String, fieldA:String?, fieldN:String, etc..) throws -> ClinicUser {
- // If fieldA is available, update fieldA:
- if fieldA != nil {
- model.fieldA = fieldA
- }
- // If fieldN is available, update fieldN:
- if fieldN != nil {
- model.fieldN = fieldN
- }
- // After update all fields, save:
- try model.save()
- // Return the updated model:
- return model
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement