-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from fastapi import APIRouter, Depends, status | ||
from sqlalchemy.ext.asyncio.session import AsyncSession | ||
|
||
from app.repositories.sessions import get_async_session | ||
from app.services.users import ( | ||
User, | ||
UserAdd, | ||
process_add_user, | ||
) | ||
|
||
router = APIRouter(prefix="/users") | ||
|
||
|
||
@router.post( | ||
"/", | ||
status_code=status.HTTP_201_CREATED, | ||
response_model=User, | ||
name="user", | ||
) | ||
async def add_user( | ||
body: UserAdd, | ||
session: AsyncSession = Depends(get_async_session), | ||
) -> User: | ||
"""Create User. | ||
The user add endpoint is an API function allowing the creation of new user accounts. | ||
It receives user details via HTTP requests, validates the information, | ||
and stores it in the system's database. | ||
This endpoint is essential for user registration and onboarding. | ||
Please note that currently endpoint is not protected. | ||
However, if there are a lot of spam requests, the endpoint will be blocked or limited. | ||
""" | ||
return await process_add_user(body, session) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from datetime import datetime | ||
|
||
from fastapi import HTTPException, status | ||
from pydantic import BaseModel, ConfigDict, EmailStr, Field | ||
from sqlalchemy.ext.asyncio.session import AsyncSession | ||
|
||
from app.repositories.models import User as UserModel, UserAlreadyExists | ||
from app.services.hashers import hasher | ||
|
||
|
||
class UserBase(BaseModel): | ||
name: str = Field( | ||
min_length=1, | ||
max_length=64, | ||
) | ||
surname: str = Field( | ||
min_length=1, | ||
max_length=64, | ||
) | ||
middle_name: str | None = Field( | ||
default=None, | ||
alias="middleName", | ||
min_length=1, | ||
max_length=64, | ||
) | ||
email: EmailStr | ||
username: str = Field( | ||
min_length=1, | ||
max_length=64, | ||
) | ||
password: str = Field( | ||
min_length=8, | ||
max_length=128, | ||
) | ||
is_subscribed: bool = Field( | ||
default=True, | ||
alias="isSubscribed", | ||
) | ||
|
||
model_config = ConfigDict( | ||
from_attributes=True, | ||
populate_by_name=True, | ||
) | ||
|
||
|
||
class UserAdd(UserBase): | ||
... | ||
|
||
|
||
class User(UserBase): | ||
id: int | ||
is_confirmed: bool = Field(alias="isConfirmed") | ||
created_at: datetime = Field(alias="createdAt") | ||
|
||
|
||
async def process_add_user(body: UserAdd, session: AsyncSession, /): | ||
body.password = hasher.encode(body.password) | ||
try: | ||
user: UserModel = await UserModel.add(session, body) | ||
except UserAlreadyExists: | ||
raise HTTPException( | ||
detail="User with username or email already exists", | ||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, | ||
) | ||
return User.model_validate(user) |