MyQuery.py 3.2 KB

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