From e0b77d7ff231e3320c7eca7a3f2d4a59d6e0c89f Mon Sep 17 00:00:00 2001 From: Peter Vacho Date: Wed, 25 Dec 2024 20:22:00 +0100 Subject: [PATCH] Add a create_user method for register data --- src/api/users.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/api/users.py b/src/api/users.py index 4f06acd..42c714e 100644 --- a/src/api/users.py +++ b/src/api/users.py @@ -30,6 +30,12 @@ class RegisterData(BaseModel): password: Annotated[str, StringConstraints(min_length=1, max_length=50)] email: EmailStr + async def create_user(self) -> User: + """Create a new user in the database.""" + password_hash = create_password_hash(self.password) + user = User(username=self.username, email=self.email, password_hash=password_hash) + return await user.create() + @final class UserData(BaseModel): @@ -93,9 +99,7 @@ async def register(reg_data: Annotated[RegisterData, Body()]) -> UserData: if user is not None: raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="Username already taken") - password_hash = create_password_hash(reg_data.password) - user = User(username=reg_data.username, email=reg_data.email, password_hash=password_hash) - user = await user.insert() + user = await reg_data.create_user() return UserData.from_user(user)