Skip to content

Commit

Permalink
Add endpoint to add users
Browse files Browse the repository at this point in the history
  • Loading branch information
koldakov committed Jan 22, 2024
1 parent 2803770 commit dedf892
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from app.routers.notifications import router as notifications_router
from app.routers.root import router as root_router
from app.routers.seasons import router as seasons_router
from app.routers.users import router as users_router

mimetypes.add_type("image/webp", ".webp")

Expand Down Expand Up @@ -61,6 +62,11 @@
prefix="/api",
include_in_schema=False,
)
app.include_router(
users_router,
tags=["users"],
prefix="/api",
)

app.mount("/static", StaticFiles(directory="static"), name="static")

Expand Down
34 changes: 34 additions & 0 deletions app/routers/users.py
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)
65 changes: 65 additions & 0 deletions app/services/users.py
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)

0 comments on commit dedf892

Please sign in to comment.