This commit is contained in:
s.mostryukov 2024-07-12 14:44:02 +03:00
parent fab67f6ea9
commit 3b175a1f83
8 changed files with 80 additions and 7 deletions

View File

@ -1,4 +1,11 @@
from fastapi import APIRouter
from core.config import settings
from .api_v1 import router as router_api_v1
router = APIRouter()
router = APIRouter(
prefix=settings.api.prefix
)
router.include_router(
router_api_v1,
)

View File

@ -5,7 +5,7 @@ from core.config import settings
from .users import router as users_router
router = APIRouter(
prefix=settings.api.v1.users,
prefix=settings.api.v1.prefix,
)
router.include_router(users_router,

View File

View File

@ -0,0 +1,20 @@
from typing import Sequence
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from core.models import User
from core.schemas.user import UserCreate
async def get_all_users(session: AsyncSession) -> Sequence[User]:
stmt = select(User).order_by(User.id)
result = await session.scalars(stmt)
return result.all()
async def create_user(session: AsyncSession, user_create: UserCreate) -> User:
user = User(**user_create.model_dump())
session.add(user)
await session.commit()
return user

View File

@ -1,6 +1,27 @@
from fastapi import APIRouter
from typing import Annotated
from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession
from core.models import db_helper
from core.schemas.user import UserRead, UserCreate
from api.api_v1.crud import users as users_crud
router = APIRouter(
tags=['Users'],
)
)
@router.get('', response_model=list[UserRead])
async def get_users(session: Annotated[AsyncSession, Depends(db_helper.session_getter)]):
users = await users_crud.get_all_users(session=session)
return users
@router.post('', response_model=UserRead)
async def create_user(session: Annotated[AsyncSession, Depends(db_helper.session_getter)], user_create: UserCreate,):
user = await users_crud.create_user(session=session, user_create=user_create)
return user

View File

@ -39,9 +39,10 @@ class DatabaseConfig(BaseModel):
'pk': 'pk_%(table_name)s',
}
class Settings(BaseSettings):
model_config = SettingsConfigDict(
env_file=('.env-template', '.env'),
env_file=('D:\PythonScripts\sipi-web\sipi-app\.env', 'sipi-app/.env'),
case_sensitive=False,
env_nested_delimiter='__',
env_prefix='SIPI_CONFIG__',
@ -51,5 +52,4 @@ class Settings(BaseSettings):
db: DatabaseConfig
settings = Settings()
settings = Settings()

View File

View File

@ -0,0 +1,25 @@
from pydantic import BaseModel
class UserBase(BaseModel):
username: str
foo: int
bar: int
class UserCreate(UserBase):
pass
class UserRead(UserBase):
id: int
class UserRemove(UserBase):
pass
class UserChange(UserBase):
pass