Guest User

Untitled

a guest
Jan 19th, 2018
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. //This route will be used to fetch data from the mysql database
  2. routes.add(method: .get, uri: "/use", handler: useMysql)
  3.  
  4. public func useMysql(_ request: HTTPRequest, response: HTTPResponse)
  5. {
  6. //function body
  7. }
  8.  
  9. routes.add(method: .get, uri: "/use", handler: useMysql(request: object of HTTPRequest, response: object of HTTPResponse))
  10.  
  11. // Register your own routes and handlers
  12. var routes = Routes()
  13. routes.add(method: .get, uri: "/", handler: {
  14. request, response in
  15. response.setHeader(.contentType, value: "text/html")
  16. response.appendBody(string: "<html><title>Hello, world!</title><body>Hello, world!</body></html>")
  17. //This route will be used to fetch data from the mysql database
  18. useMysql(request, response: response)
  19. response.completed()
  20. }
  21. )
  22.  
  23. routes.add(method: .post, uri: "/admin/api/v1/login") { (request, response) in
  24.  
  25. let email = request.param(name: "email")
  26. let password = request.param(name: "passwrod")
  27. let JSON = Login.checkLogin(email: email, password: password)
  28. do {
  29. try response.setBody(json: JSON)
  30. .completed()
  31. } catch {
  32. }
  33. }
  34.  
  35. import Foundation
  36. import PerfectMySQL
  37. import PerfectLib
  38.  
  39. class Login: NSObject {
  40.  
  41. class func checkLogin(email: String?, password: String?)-> JSONConvertible {
  42.  
  43. var JSON: JSONConvertible?
  44.  
  45.  
  46.  
  47. if !ServerHelper.isValidEmail(candidate: email) {
  48.  
  49. JSON = ServerHelper.getErrorJson(message: KMESSAGE_INVALID_EMAIL_ADDRESS)
  50. return JSON
  51.  
  52.  
  53. } else if !ServerHelper.isValidPassword(password: password!) {
  54.  
  55. JSON = ServerHelper.getErrorJson(message: KMESSAGE_INVALID_PASSWORD)
  56. return JSON
  57.  
  58. } else {
  59.  
  60.  
  61. let mySql = MySQL()
  62. let connect = mySql.connect(host: "127.0.0.1", user: "root", password: "mypassword", db:"SuperAdmin" , port: 3306, socket: "", flag: 0)
  63. if !connect {
  64.  
  65. JSON = ServerHelper.getErrorJson(message: KMESSAGE_SERVER_ERROR)
  66. return JSON
  67. }
  68.  
  69. let query = "select email, phone_number from SuperAdmin.Login where email='(String(describing: email!))' AND password='(String(describing: password!))'"
  70.  
  71. if mySql.query(statement: query) {
  72.  
  73. let result = mySql.storeResults
  74.  
  75. var jsonResult = [Dictionary<String, Any>]()
  76.  
  77. result()?.forEachRow(callback: { (element) in
  78.  
  79. var dict = Dictionary<String, Any>()
  80.  
  81. if let arrRow = element as? [String] {
  82.  
  83. let email = arrRow[0]
  84. let phone_number = arrRow[1]
  85. dict["email"] = email
  86. dict["phone_number"] = phone_number
  87. jsonResult.append(dict)
  88. }
  89. })
  90.  
  91. if jsonResult.count == 0 {
  92.  
  93. JSON = ServerHelper.getErrorJson(message: KMESSAGE_NOT_AUTHORIZE)
  94.  
  95. } else {
  96.  
  97. JSON = ServerHelper.getJson(result: jsonResult)
  98.  
  99. }
  100.  
  101. } else {
  102.  
  103. JSON = ServerHelper.getErrorJson(message: KMESSAGE_NOT_AUTHORIZE)
  104. return JSON
  105. }
  106. mySql.close()
  107. }
  108. return JSON
  109. }
  110.  
  111.  
  112. }
  113.  
  114. {
  115. "status": 200,
  116. "succcess": true,
  117. "result": [
  118. {
  119. "phone_number": "84747474574",
  120. "email": "myemail@swift.com"
  121. }
  122. ]
  123.  
  124.  
  125. }
  126.  
  127. let connect = mySql.connect(host: "127.0.0.1", user: "root", password: "mypassword", db:"SuperAdmin" , port: 3306, socket: "", flag: 0)
Add Comment
Please, Sign In to add comment