Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

user.py 1.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 verify_driver_exists(driver_id: int, db: Session):
  25. driver = db.query(User).filter(User.Id == driver_id).first()
  26. if not driver:
  27. return False
  28. if driver.Role != "Driver":
  29. return False
  30. return True
  31. def list_users(db: Session, role: str = "Any"):
  32. users = db.query(User).filter((User.Role == role) | (role == "Any")).all()
  33. return users