Spaces:
Paused
Paused
| 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) | |
| 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)}" | |
| ) | |