SharixQuery.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from FlaskApp.BackendApi.Models import RoleModel, UserModel
  2. from FlaskApp.BackendApi.connect import connect
  3. from sqlalchemy import create_engine, or_
  4. from sqlalchemy.orm import sessionmaker, scoped_session
  5. engine = create_engine(connect())
  6. session = scoped_session(sessionmaker(
  7. autocommit=False,
  8. autoflush=False,
  9. bind=engine
  10. ))
  11. print(str(session.is_active))
  12. class ReturnData:
  13. def __init__(self, data, code):
  14. self.data = data
  15. self.code = code
  16. class SharixQuery:
  17. def auth_query(self, request_data) -> ReturnData:
  18. try:
  19. result = session.query(UserModel).\
  20. filter(or_(UserModel.email == request_data['login_email'], UserModel.name == request_data['login_email'])).\
  21. filter(UserModel.password == request_data['password']).first()
  22. return ReturnData(result, 200)
  23. except Exception as ex:
  24. return ReturnData({"msg":str(ex)}, 500)
  25. def add_user(self, request_data) -> ReturnData:
  26. try:
  27. user = UserModel(**request_data)
  28. chek = session.query(UserModel).filter(or_(UserModel.email==user.email, UserModel.name==user.name)).first()
  29. if chek is None:
  30. session.add(user)
  31. session.flush()
  32. session.commit()
  33. return ReturnData({"id":user.id}, 200)
  34. else:
  35. return ReturnData({"msg":"User exist!"}, 400)
  36. except Exception as ex:
  37. return ReturnData({"msg":str(ex)}, 500)
  38. def get_user(self, id) -> UserModel:
  39. try:
  40. user = session.query(UserModel).filter(UserModel.id == id).first()
  41. return user
  42. except Exception as ex:
  43. return ReturnData({"msg":str(ex)}, 500)
  44. def get_all_user(self) -> list[UserModel]:
  45. try:
  46. alluser = session.query(UserModel).order_by(UserModel.email).all()
  47. return alluser
  48. except Exception as ex:
  49. return ReturnData({"msg":str(ex)}, 500)
  50. def delete_user_query(self, filed, filed_value) -> ReturnData:
  51. try:
  52. session.query(UserModel).filter(filed == filed_value).delete()
  53. session.commit()
  54. return ReturnData({"msg":"User is deleted!"}, 200)
  55. except Exception as ex:
  56. return ReturnData({"msg":str(ex)}, 500)
  57. def update_user(self, request_data) -> ReturnData:
  58. try:
  59. user = UserModel(**request_data)
  60. session.query(UserModel).filter(UserModel.id == user.id).update({
  61. UserModel.name:UserModel.name if user.name is None else user.name,
  62. UserModel.password:UserModel.password if user.password is None else user.password,
  63. })
  64. session.flush()
  65. session.commit()
  66. return ReturnData(user.as_dict(), 200)
  67. except Exception as ex:
  68. return ReturnData({"msg":str(ex)}, 500)
  69. def get_roles(self) -> list[RoleModel]:
  70. try:
  71. all_role = session.query(RoleModel).all()
  72. return all_role
  73. except Exception as ex:
  74. return ReturnData({"msg":str(ex)}, 500)
  75. def delete_role(self, filed, filed_value) -> ReturnData:
  76. try:
  77. self.delete_user_query(UserModel.idrole, filed_value)
  78. session.query(RoleModel).filter(filed == filed_value).delete()
  79. session.commit()
  80. return ReturnData({"msg":"Role is deleted!"}, 200)
  81. except Exception as ex:
  82. return ReturnData({"msg":str(ex)}, 500)
  83. def add_role(self, request_data) -> ReturnData:
  84. try:
  85. role = RoleModel(**request_data)
  86. session.add(role)
  87. session.flush()
  88. session.commit()
  89. return ReturnData({"id":str(role.id)}, 500)
  90. except Exception as ex:
  91. return ReturnData({"msg":str(ex)}, 500)