from datetime import date from typing import Optional from sqlalchemy import select from sqlalchemy.dialects.postgresql import insert from sqlalchemy.ext.asyncio import AsyncSession from common.database.model.models import Post class FeedCRUD: async def day(self, s: AsyncSession, day: date, scopes: list[str]) -> list[Post]: stmt = select(Post).where(Post.feed_date == day, Post.scope.in_(scopes), Post.deleted.is_(False)).order_by(Post.position, Post.scope) return list((await s.scalars(stmt)).all()) async def positions(self, s: AsyncSession, day: date) -> set[tuple[str, int]]: """그날 이미 만든 (scope, 카드 번호). 지운 카드도 넣어 다시 만들지 않게 한다.""" return {(scope, pos) for scope, pos in (await s.execute(select(Post.scope, Post.position).where(Post.feed_date == day))).all()} async def insert_day(self, s: AsyncSession, rows: list[dict]) -> None: if rows: await s.execute(insert(Post).values(rows).on_conflict_do_nothing()) async def get(self, s: AsyncSession, post_id: int) -> Optional[Post]: post = await s.get(Post, post_id) return post if post is not None and not post.deleted else None