from fastapi import APIRouter, Depends, HTTPException, Query from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.future import select from sqlalchemy import func, delete from sqlalchemy.orm import selectinload from typing import Optional from app.db.async_session import get_async_db from app.schemas import library as schemas from app.models.library import LikedVideo from app.models.models import Video from app.core.async_dependencies import get_async_current_user router = APIRouter() @router.get("/", response_model=schemas.LikedResponse) async def get_liked_videos( page: int = 1, limit: int = 20, category: Optional[str] = Query("all"), db: AsyncSession = Depends(get_async_db), current_user = Depends(get_async_current_user) ): offset = (page - 1) * limit query = select(LikedVideo).options( selectinload(LikedVideo.video).selectinload(Video.owner) ).filter(LikedVideo.user_id == current_user.id) # Category filter (if Video model supports it) if category != "all": # This assumes existing Video model has a 'video_type' or similar field that matches category # Or we could filter by tags. For now we follow the 'category' requirement. query = query.join(Video).filter(Video.video_type == category) # Total count_query = select(func.count()).select_from(query.subquery()) total = await db.scalar(count_query) # Items query = query.order_by(LikedVideo.liked_at.desc()).offset(offset).limit(limit) result = await db.execute(query) items = result.scalars().all() return { "items": items, "page": page, "limit": limit, "total": total, "has_more": offset + limit < total } async def get_video_db_async(db: AsyncSession, video_id: str) -> Video: if isinstance(video_id, str) and not video_id.isdigit(): result = await db.execute(select(Video).filter(Video.public_id == video_id)) else: result = await db.execute(select(Video).filter(Video.id == int(video_id))) video = result.scalar_one_or_none() if not video: raise HTTPException(status_code=404, detail="Video not found") return video @router.post("/{video_id}") async def like_video( video_id: str, db: AsyncSession = Depends(get_async_db), current_user = Depends(get_async_current_user) ): video = await get_video_db_async(db, video_id) result = await db.execute(select(LikedVideo).filter( LikedVideo.user_id == current_user.id, LikedVideo.video_id == video.id )) if result.scalar_one_or_none(): return {"status": "already_liked"} new_like = LikedVideo(user_id=current_user.id, video_id=video.id) db.add(new_like) # Increment like count on video (requires existing field) # await db.execute(update(Video).where(Video.id == video.id).values(likes_count=Video.likes_count + 1)) await db.commit() return {"status": "success"} @router.delete("/{video_id}") async def unlike_video( video_id: str, db: AsyncSession = Depends(get_async_db), current_user = Depends(get_async_current_user) ): video = await get_video_db_async(db, video_id) await db.execute(delete(LikedVideo).filter( LikedVideo.user_id == current_user.id, LikedVideo.video_id == video.id )) await db.commit() return {"status": "success"}