final
This commit is contained in:
parent
fab67f6ea9
commit
3b175a1f83
|
@ -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,
|
||||
)
|
|
@ -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,
|
||||
|
|
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -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__',
|
||||
|
@ -52,4 +53,3 @@ class Settings(BaseSettings):
|
|||
|
||||
|
||||
settings = Settings()
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue