You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

user.py 972 B

12345678910111213141516171819202122232425262728293031323334353637
  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 = User(
  8. Email=user.email,
  9. Name=user.name,
  10. MiddleName=user.middlename,
  11. LastName=user.lastname,
  12. BirthDate=user.birthdate,
  13. ContactNumber=user.phone,
  14. Role=user.role,
  15. HashedPassword=Hasher.get_password_hash(user.password),
  16. )
  17. db.add(user)
  18. db.commit()
  19. db.refresh(user)
  20. return user
  21. def verify_driver_exists(driver_id: int, db: Session):
  22. driver = db.query(User).filter(User.Id == driver_id).first()
  23. if not driver:
  24. return False
  25. if driver.Role != "Driver":
  26. return False
  27. return True
  28. def list_users(db: Session, role: str = "Any"):
  29. users = db.query(User).filter((User.Role == role) | (role == "Any")).all()
  30. return users