27 lines
660 B
Python
27 lines
660 B
Python
"""
|
|
Heaps API router
|
|
"""
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.database import get_db
|
|
from app.models import Heap
|
|
|
|
router = APIRouter()
|
|
|
|
@router.get("")
|
|
async def list_heaps(db: AsyncSession = Depends(get_db)):
|
|
"""List all heaps"""
|
|
result = await db.execute(select(Heap))
|
|
heaps = result.scalars().all()
|
|
return heaps
|
|
|
|
@router.post("")
|
|
async def create_heap(name: str, db: AsyncSession = Depends(get_db)):
|
|
"""Create a new heap"""
|
|
heap = Heap(name=name)
|
|
db.add(heap)
|
|
await db.commit()
|
|
await db.refresh(heap)
|
|
return heap |