MyQuery.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from flask import jsonify
  2. from function import RestFunc
  3. restfunc = RestFunc()
  4. class MyQuery:
  5. def auth_query(self, request_data):
  6. try:
  7. result = restfunc.query_select('''
  8. SELECT * FROM rest_user
  9. WHERE (rest_user.name=%s OR rest_user.email=%s) AND rest_user.password=%s;
  10. ''', (request_data['login_email'], request_data['login_email'], request_data['password'],), True)
  11. return restfunc.gen_token(result['id'], 5), 200
  12. except Exception as ex:
  13. return jsonify({"msg":str(ex)}), 500
  14. def add_query(self, request_data):
  15. try:
  16. proverka = restfunc.query_proverka('''
  17. SELECT * FROM rest_user
  18. WHERE rest_user.name=%s OR rest_user.email=%s;
  19. ''', (request_data['name'], request_data['email']))
  20. if(proverka):
  21. return jsonify({"msg":"User exist!"}), 400
  22. else:
  23. result = restfunc.query_insert('''
  24. INSERT INTO rest_user (name, birthday, lastlogintime, insys, idrole, email, password)
  25. VALUES (%s, %s, %s, %s, %s, %s, %s)
  26. RETURNING id;
  27. ''', \
  28. (request_data['name'], request_data['reg_date'],\
  29. request_data['log_time'], request_data['in_sys'],\
  30. request_data['role_id'], request_data['email'], request_data['password']))
  31. return result, 201
  32. except Exception as ex:
  33. return jsonify({"msg":str(ex)}), 500
  34. def get_user_query(self, id):
  35. try:
  36. result = restfunc.query_select('''
  37. SELECT * FROM rest_user WHERE id = %s;
  38. ''', (id,), True)
  39. return jsonify(result), 200
  40. except Exception as ex:
  41. return jsonify({"msg":str(ex)}), 500
  42. def get_all_user_query(self):
  43. try:
  44. result = restfunc.query_select('''
  45. SELECT rest_user.id, rest_user.name, rest_user.birthday, rest_user.insys, rest_role.name AS role_name, rest_user.lastlogintime, rest_user.email, rest_user.password
  46. FROM rest_user, rest_role
  47. WHERE rest_user.idrole = rest_role.id
  48. ORDER BY id ASC;
  49. ''', (), False)
  50. return jsonify(result), 200
  51. except Exception as ex:
  52. return jsonify({"msg":str(ex)}), 500
  53. def delete_user_query(self, request_data):
  54. try:
  55. result = restfunc.query_delete('''
  56. DELETE FROM rest_user
  57. WHERE id = %s;
  58. ''', (request_data['id'],))
  59. if(result):
  60. return jsonify({"msg":"Successful removal!"}), 200
  61. else:
  62. return jsonify({"msg":"User does not exist!"}), 400
  63. except Exception as ex:
  64. return jsonify({"msg":str(ex)}), 500
  65. def update_user_query(self, request_data, id):
  66. try:
  67. result = restfunc.query_update(f'''
  68. UPDATE rest_user
  69. SET name = %s
  70. WHERE id = %s;
  71. ''', (request_data['name'], id))
  72. if(result):
  73. return jsonify({"msg":"Successful update!"}), 200
  74. else:
  75. return jsonify({"msg":"User does not exist!"}), 400
  76. except Exception as ex:
  77. return jsonify({"msg":str(ex)}), 500