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.
 
 

53 lines
1.4 KiB

  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. BirthDate=user.BirthDate,
  13. ContactNumber=user.ContactNumber,
  14. Role=user.Role,
  15. HashedPassword=Hasher.get_password_hash(user.Password),
  16. )
  17. db.add(user_object)
  18. db.commit()
  19. db.refresh(user_object)
  20. return user_object
  21. def get_user_by_id(user_id: int, db: Session):
  22. user = db.query(User).filter(User.Id == user_id).first()
  23. return user
  24. def get_user_by_email(email: str, db: Session):
  25. user = db.query(User).filter(User.Email == email).first()
  26. return user
  27. def get_user_by_phone(phone: str, db: Session):
  28. user = db.query(User).filter(User.ContactNumber == phone).first()
  29. return user
  30. def verify_driver_exists(driver_id: int, db: Session):
  31. driver = db.query(User).filter(User.Id == driver_id).first()
  32. if not driver:
  33. return False
  34. if driver.Role != "Driver":
  35. return False
  36. return True
  37. def list_users(db: Session, role: str = "Any"):
  38. users = db.query(User).filter((User.Role == role) | (role == "Any")).all()
  39. return users