|
- # Creating a new user in the database
- from sqlalchemy.orm import Session
-
- from schemas.user import UserCreate
- from db.models.user import User
- from core.hashing import Hasher
-
-
- def create_new_user(user: UserCreate, db: Session):
- user_object = User(
- Email=user.Email,
- Name=user.Name,
- MiddleName=user.MiddleName,
- LastName=user.LastName,
- BirthDate=user.BirthDate,
- ContactNumber=user.ContactNumber,
- Role=user.Role,
- HashedPassword=Hasher.get_password_hash(user.Password),
- )
- db.add(user_object)
- db.commit()
- db.refresh(user_object)
- return user_object
-
-
- def get_user_by_id(user_id: int, db: Session):
- user = db.query(User).filter(User.Id == user_id).first()
- return user
-
-
- def get_user_by_email(email: str, db: Session):
- user = db.query(User).filter(User.Email == email).first()
- return user
-
-
- def get_user_by_phone(phone: str, db: Session):
- user = db.query(User).filter(User.ContactNumber == phone).first()
- return user
-
-
- def verify_driver_exists(driver_id: int, db: Session):
- driver = db.query(User).filter(User.Id == driver_id).first()
- if not driver:
- return False
- if driver.Role != "Driver":
- return False
- return True
-
-
- def list_users(db: Session, role: str = "Any"):
- users = db.query(User).filter((User.Role == role) | (role == "Any")).all()
- return users
|