mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-08-26 16:40:40 +03:00
add multi stuff POST endpoint
This commit is contained in:
parent
d70ce04294
commit
d54c418830
@ -1,3 +1,4 @@
|
|||||||
|
from typing import List
|
||||||
from fastapi import APIRouter, Depends, status
|
from fastapi import APIRouter, Depends, status
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
@ -8,6 +9,19 @@ from the_app.schemas.stuff import StuffResponse, StuffSchema
|
|||||||
router = APIRouter(prefix="/v1/stuff")
|
router = APIRouter(prefix="/v1/stuff")
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/add_many", status_code=status.HTTP_201_CREATED)
|
||||||
|
async def create_multi_stuff(payload: List[StuffSchema], db_session: AsyncSession = Depends(get_db)):
|
||||||
|
stuff_instances = [
|
||||||
|
Stuff(
|
||||||
|
name=stuf.name, description=stuf.description
|
||||||
|
)
|
||||||
|
for stuf in payload
|
||||||
|
]
|
||||||
|
db_session.add_all(stuff_instances)
|
||||||
|
await db_session.commit()
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
@router.post("", status_code=status.HTTP_201_CREATED, response_model=StuffResponse)
|
@router.post("", status_code=status.HTTP_201_CREATED, response_model=StuffResponse)
|
||||||
async def create_stuff(payload: StuffSchema, db_session: AsyncSession = Depends(get_db)):
|
async def create_stuff(payload: StuffSchema, db_session: AsyncSession = Depends(get_db)):
|
||||||
stuff = Stuff(name=payload.name, description=payload.description)
|
stuff = Stuff(name=payload.name, description=payload.description)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user