Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 

95 Zeilen
2.8 KiB

  1. # Creating a new user in the database
  2. from sqlalchemy.orm import Session
  3. from schemas.user import UserCreate, DriverCreate
  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 create_new_driver(driver: DriverCreate, db: Session):
  23. driver_object = User(
  24. Email=driver.Email,
  25. Name=driver.Name,
  26. MiddleName=driver.MiddleName,
  27. LastName=driver.LastName,
  28. GovernmentId=driver.GovernmentId,
  29. Address=driver.Address,
  30. ContactNumber=driver.ContactNumber,
  31. Role="Driver",
  32. HashedPassword=Hasher.get_password_hash(driver.Password),
  33. DrivingLicenseNumber=driver.DrivingLicenseNumber
  34. )
  35. db.add(driver_object)
  36. db.commit()
  37. db.refresh(driver_object)
  38. return driver_object
  39. def get_user_by_id(user_id: int, role: str, db: Session):
  40. user = db.query(User).filter(User.Id == user_id).first()
  41. if user.Role != role:
  42. return False
  43. return user
  44. def get_user_by_email(email: str, db: Session):
  45. user = db.query(User).filter(User.Email == email).first()
  46. return user
  47. def get_user_by_phone(phone: str, db: Session):
  48. user = db.query(User).filter(User.ContactNumber == phone).first()
  49. return user
  50. def verify_driver_exists(driver_id: int, db: Session):
  51. driver = db.query(User).filter(User.Id == driver_id).first()
  52. if not driver:
  53. return False
  54. if driver.Role != "Driver":
  55. return False
  56. return True
  57. def get_car_driver(vehicle_id: int, db: Session):
  58. driver = db.query(User).filter(User.AssignedVehicle == vehicle_id).first()
  59. if not driver:
  60. return False
  61. return driver
  62. def list_users(db: Session, role: str = "Any"):
  63. users = db.query(User).filter((User.Role == role) | (role == "Any")).all()
  64. return users
  65. def replace_user_data(user_id: int, user_data: UserCreate, db: Session):
  66. user = db.query(User).filter(User.Id == user_id).first()
  67. if not user:
  68. return "userNotFound"
  69. user.Email = user_data.Email
  70. user.Name = user_data.Name
  71. user.MiddleName = user_data.MiddleName
  72. user.LastName = user_data.LastName
  73. user.GovernmentId = user_data.GovernmentId
  74. user.Address = user_data.Address
  75. user.ContactNumber = user_data.ContactNumber
  76. user.Role = user_data.Role
  77. user.HashedPassword = Hasher.get_password_hash(user_data.Password)
  78. db.commit()
  79. db.refresh(user)
  80. return user