Created
November 18, 2016 23:15
-
-
Save siavashalipour/0b35cdfaf89aab142aeea61adf842108 to your computer and use it in GitHub Desktop.
End-to-end Swift Solution with IBM Bluemix Part four - 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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