File size: 3,425 Bytes
3533fb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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"}