No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

user.py 1.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Creating a new user in the database
  2. from sqlalchemy.orm import Session
  3. from schemas.user import UserCreate
  4. from db.models.user import User
  5. from core.hashing import Hasher
  6. def create_new_user(user: UserCreate, db: Session):
  7. user_object = User(
  8. Email=user.Email,
  9. Name=user.Name,
  10. MiddleName=user.MiddleName,
  11. LastName=user.LastName,
  12. GovernmentId=user.GovernmentId,
  13. Address=user.Address,
  14. ContactNumber=user.ContactNumber,
  15. Role=user.Role,
  16. HashedPassword=Hasher.get_password_hash(user.Password),
  17. )
  18. db.add(user_object)
  19. db.commit()
  20. db.refresh(user_object)
  21. return user_object
  22. def get_user_by_id(user_id: int, db: Session):
  23. user = db.query(User).filter(User.Id == user_id).first()
  24. return user
  25. def get_user_by_email(email: str, db: Session):
  26. user = db.query(User).filter(User.Email == email).first()
  27. return user
  28. def get_user_by_phone(phone: str, db: Session):
  29. user = db.query(User).filter(User.ContactNumber == phone).first()
  30. return user
  31. def verify_driver_exists(driver_id: int, db: Session):
  32. driver = db.query(User).filter(User.Id == driver_id).first()
  33. if not driver:
  34. return False
  35. if driver.Role != "Driver":
  36. return False
  37. return True
  38. def get_car_driver(vehicle_id: int, db: Session):
  39. driver = db.query(User).filter(User.AssignedVehicle == vehicle_id).first()
  40. if not driver:
  41. return False
  42. return driver
  43. def list_users(db: Session, role: str = "Any"):
  44. users = db.query(User).filter((User.Role == role) | (role == "Any")).all()
  45. return users