SharixQuery.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. from BackendApi.function import QueryFunc
  2. restfunc = QueryFunc()
  3. def generate_return_data(data, code):
  4. return {"data":data, "httpcode":code}
  5. class SharixQuery:
  6. def auth_query(self, request_data):
  7. try:
  8. result = restfunc.query_select('''
  9. SELECT * FROM rest_user
  10. WHERE (rest_user.name=%s OR rest_user.email=%s) AND rest_user.password=%s;
  11. ''', (request_data['login_email'], request_data['login_email'], request_data['password'],), True)
  12. return generate_return_data(result, 200)
  13. except Exception as ex:
  14. return generate_return_data({"msg":str(ex)}, 500)
  15. def add_query(self, request_data):
  16. try:
  17. check_email_login = restfunc.query_check_count('''
  18. SELECT * FROM rest_user
  19. WHERE rest_user.name=%s OR rest_user.email=%s;
  20. ''', (request_data['name'], request_data['email']))
  21. if(check_email_login):
  22. return generate_return_data({"msg":"User exist!"}, 400)
  23. else:
  24. result = restfunc.query_insert('''
  25. INSERT INTO rest_user (name, birthday, lastlogintime, insys, idrole, email, password)
  26. VALUES (%s, %s, %s, %s, %s, %s, %s)
  27. RETURNING id;
  28. ''', \
  29. (request_data['name'], request_data['reg_date'],\
  30. request_data['log_time'], request_data['in_sys'],\
  31. request_data['role_id'], request_data['email'], request_data['password']))
  32. return generate_return_data(result, 201)
  33. except Exception as ex:
  34. return generate_return_data({"msg":str(ex)}, 500)
  35. def get_user_query(self, id):
  36. try:
  37. result = restfunc.query_select('''
  38. SELECT * FROM rest_user WHERE id = %s;
  39. ''', (id,), True)
  40. return generate_return_data(result, 200)
  41. except Exception as ex:
  42. return generate_return_data({"msg":str(ex)}, 500)
  43. def get_all_user_query(self):
  44. try:
  45. result = restfunc.query_select('''
  46. 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
  47. FROM rest_user, rest_role
  48. WHERE rest_user.idrole = rest_role.id
  49. ORDER BY id ASC;
  50. ''', (), False)
  51. return generate_return_data(result, 200)
  52. except Exception as ex:
  53. return generate_return_data({"msg":str(ex)}, 500)
  54. def delete_user_query(self, request_data):
  55. try:
  56. result = restfunc.query_delete_update('''
  57. DELETE FROM rest_user
  58. WHERE id = %s;
  59. ''', (request_data['id'],))
  60. if(result):
  61. return generate_return_data({"msg":"Successful delete!"}, 200)
  62. else:
  63. return generate_return_data({"msg":"User does not exist!"}, 400)
  64. except Exception as ex:
  65. return generate_return_data({"msg":str(ex)}, 500)
  66. def update_user_query(self, request_data, id):
  67. try:
  68. result = restfunc.query_delete_update('''
  69. UPDATE rest_user
  70. SET name = %s
  71. WHERE id = %s;
  72. ''', (request_data['name'], id))
  73. if(result):
  74. return {"data": {"msg":"Successful update!"}, "httpcode": 200}
  75. else:
  76. return generate_return_data({"msg":"User does not exist!"}, 400)
  77. except Exception as ex:
  78. return generate_return_data({"msg":str(ex)}, 500)
  79. def add_car_log(self, request_data):
  80. try:
  81. result = restfunc.query_insert('''
  82. INSERT INTO cars_log (id, resource_id, operation, time_log_created, user_id)
  83. VALUES(%s, %s, %s, %s, %s)
  84. RETURNING id;
  85. ''', (8, 3, request_data['operation'], request_data['date_cur'], 1))
  86. return generate_return_data(result, 201)
  87. except Exception as ex:
  88. return generate_return_data({"msg":str(ex)}, 500)