mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-11-30 13:20:40 +03:00
nonsense part added
This commit is contained in:
40
the_app/api/nonsense.py
Normal file
40
the_app/api/nonsense.py
Normal file
@@ -0,0 +1,40 @@
|
||||
from fastapi import APIRouter, Depends, status
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from the_app.database import get_db
|
||||
from the_app.models.nonsense import Nonsense
|
||||
from the_app.schemas.nnonsense import NonsenseResponse, NonsenseSchema
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.post("/", status_code=status.HTTP_201_CREATED, response_model=NonsenseResponse)
|
||||
async def create_nonsense(payload: NonsenseSchema, db_session: AsyncSession = Depends(get_db)):
|
||||
nonsense = Nonsense(**payload.dict())
|
||||
await nonsense.save(db_session)
|
||||
return nonsense
|
||||
|
||||
|
||||
@router.get("/", response_model=NonsenseResponse)
|
||||
async def find_nonsense(
|
||||
name: str,
|
||||
db_session: AsyncSession = Depends(get_db),
|
||||
):
|
||||
return await Nonsense.find(db_session, name)
|
||||
|
||||
|
||||
@router.delete("/")
|
||||
async def delete_nonsense(name: str, db_session: AsyncSession = Depends(get_db)):
|
||||
nonsense = await Nonsense.find(db_session, name)
|
||||
return await nonsense.delete(nonsense, db_session)
|
||||
|
||||
|
||||
@router.patch("/", response_model=NonsenseResponse)
|
||||
async def update_nonsense(
|
||||
payload: NonsenseSchema,
|
||||
name: str,
|
||||
db_session: AsyncSession = Depends(get_db),
|
||||
):
|
||||
nonsense = await Nonsense.find(db_session, name)
|
||||
await nonsense.update(db_session, **payload.dict())
|
||||
return nonsense
|
||||
Reference in New Issue
Block a user