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) -> list[Post]: stmt = select(Post).where(Post.feed_date == day, Post.deleted.is_(False)).order_by(Post.position) return list((await s.scalars(stmt)).all()) async def has_day(self, s: AsyncSession, day: date) -> bool: return await s.scalar(select(Post.post_id).where(Post.feed_date == day, Post.deleted.is_(False)).limit(1)) is not None 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