Skip to content

Instantly share code, notes, and snippets.

@siavashalipour
Created November 18, 2016 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save siavashalipour/0b35cdfaf89aab142aeea61adf842108 to your computer and use it in GitHub Desktop.
Save siavashalipour/0b35cdfaf89aab142aeea61adf842108 to your computer and use it in GitHub Desktop.
End-to-end Swift Solution with IBM Bluemix Part four - 3
public func postToDB(request: RouterRequest, response: RouterResponse, next: @escaping () -> Void) throws {
 Log.debug(“POST — /addtodb route handler…”)
 response.headers[“Content-Type”] = “application/json; charset=utf-8”
 var jsonResponse = JSON([:])
 if let body = request.body {
 switch body {
 case .json(let json):
 let fname = json[“fname”].stringValue
 let lname = json[“lname”].stringValue
 do {
 if try ProfileDatabase().addToDB(fname: fname, lname: lname) {
 jsonResponse[“message”].stringValue = “Success”
 try response.status(.OK).send(json: jsonResponse).end()
 } else {
 jsonResponse[“message”].stringValue = “Fail”
 try response.status(.methodFailure).send(json: jsonResponse).end()
 }
} catch DBError.fileError(let s) {
 jsonResponse[“message”].stringValue = s
 try response.status(.methodFailure).send(json: jsonResponse).end()
 }
 default:
 break
 }
 }
 jsonResponse[“message”].stringValue = “Empty Body”
 try response.status(.badRequest).send(json: jsonResponse).end()
 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment