Spaces:
Paused
Paused
File size: 1,363 Bytes
6d01d5b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
from fastapi import APIRouter, HTTPException
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.future import select
from sqlalchemy.orm import sessionmaker
from sqlalchemy import desc
from app.database import engine
from app.models import VideoUpload
router = APIRouter()
# Create async DB session
async_session = sessionmaker(engine, expire_on_commit=False, class_=AsyncSession)
@router.get("/dashboard/{user_id}")
async def get_user_dashboard(user_id: int):
try:
async with async_session() as session:
query = (
select(VideoUpload)
.where(VideoUpload.user_id == user_id)
.order_by(desc(VideoUpload.created_at))
)
result = await session.execute(query)
uploads = result.scalars().all()
# Convert SQLAlchemy objects to dicts for response
return [
{
"id": upload.id,
"video_url": upload.video_url,
"pdf_url": upload.pdf_url,
"status": upload.status,
"created_at": upload.created_at,
}
for upload in uploads
]
except Exception as e:
raise HTTPException(
status_code=500, detail=f"Error fetching dashboard data: {str(e)}"
)
|