ts-lyceum-back/db/users_manip.py
2023-11-30 21:01:57 +03:00

17 lines
609 B
Python

from fastapi import HTTPException
from tortoise.exceptions import IntegrityError
from db.models.user import User
from passlib.context import CryptContext
from schemas.user_schemas import UserCreateInfo, UserInfo
pass_context = CryptContext(schemes=["bcrypt"])
async def register_user(user: UserCreateInfo) -> UserInfo:
user.password = pass_context.hash(user.password)
try:
db_user = await User.create(**user.dict(exclude_unset=True))
except IntegrityError:
raise HTTPException(status_code=401, detail="User already exists")
return await UserInfo.from_tortoise_orm(db_user)