Add: add base fastapi backend
This commit is contained in:
commit
4d0fe108ff
13 changed files with 940 additions and 0 deletions
28
backend/app/crud.py
Normal file
28
backend/app/crud.py
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
"""
|
||||
CRUD
|
||||
Create
|
||||
Read
|
||||
Update
|
||||
Delete
|
||||
|
||||
Взаимодействие со схемами Pydantic и моделями в БД.
|
||||
"""
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from .models import Item
|
||||
from .schemas import ItemCreate
|
||||
|
||||
|
||||
async def create_item(db: AsyncSession, item: ItemCreate):
|
||||
obj = Item(title=item.title, description=item.description)
|
||||
db.add(obj)
|
||||
await db.commit()
|
||||
await db.refresh(obj)
|
||||
return obj
|
||||
|
||||
|
||||
async def list_items(db: AsyncSession):
|
||||
result = await db.execute(select(Item).order_by(Item.id.desc()))
|
||||
return result.scalars().all()
|
||||
Loading…
Add table
Add a link
Reference in a new issue