website-practice/backend/app/crud.py

28 lines
612 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
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()