from typing import Optional from sqlalchemy import select from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.orm import defer from common.database.model.models import Reel class ReelCRUD: async def create(self, s: AsyncSession, reel: Reel) -> Reel: s.add(reel) await s.flush() return reel async def get(self, s: AsyncSession, reel_id: int) -> Optional[Reel]: return await s.get(Reel, reel_id) async def recent(self, s: AsyncSession, limit: int) -> list[Reel]: """만든 순서 역순. 영상·표지 바이트는 안 읽는다.""" return list((await s.scalars(select(Reel).options(defer(Reel.video), defer(Reel.cover)).order_by(Reel.reel_id.desc()).limit(limit))).all()) async def video(self, s: AsyncSession, reel_id: int) -> Optional[bytes]: return await s.scalar(select(Reel.video).where(Reel.reel_id == reel_id)) async def cover(self, s: AsyncSession, reel_id: int) -> Optional[bytes]: return await s.scalar(select(Reel.cover).where(Reel.reel_id == reel_id))