def update_user_by_id(user_id: int, user: Dict, users_repository: UsersRepository) -> User: try: return users_repository.update_by_id(user_id, user) except ResourceNotFoundError: raise_user_not_found() except ResourceAlreadyExistsError: raise_conflict("User with data already exists")
def send_access_code( body: AccessCodeCreate, access_code_service: AccessCodeService = Depends(access_code_service), producer: SendCodeProducer = Depends(send_code_producer), users_repository: UsersRepository = Depends(users_repository), ): try: user = find_user_by_email(body.email, users_repository) except HTTPException as error: if not body.create_user: raise error user = users_repository.create(UserCreate(email=body.email).dict()) code = access_code_service.generate_code(user.id) producer.send_code(code, user.email)
def delete_user_by_id(user_id: int, users_repository: UsersRepository): try: return users_repository.delete_by_id(user_id) except ResourceNotFoundError: raise_user_not_found()
def find_user_by_email(email: str, users_repository: UsersRepository) -> User: try: return users_repository.find_by_email(email) except ResourceNotFoundError: raise_user_not_found()
def find_user_by_id(user_id: int, users_repository: UsersRepository) -> User: try: return users_repository.find_by_id(user_id) except ResourceNotFoundError: raise_user_not_found()
def users_repository(db: Session = Depends(db_session)) -> UsersRepository: return UsersRepository(db)
def users_repository(db_session): return UsersRepository(db_session)
def list_users(users_repository: UsersRepository = Depends(users_repository)): return users_repository.find_all()
def create_user(user: UserCreate, users_repository: UsersRepository = Depends(users_repository)): try: return users_repository.create(user.dict()) except ResourceAlreadyExistsError: raise_conflict("User already exists")