business-card-site/API/app/application/contest_carousel_photos_repository.py

33 lines
1.1 KiB
Python

from typing import Optional, Sequence
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.domain.models import ContestCarouselPhoto
class ContestCarouselPhotosRepository:
def __init__(self, db: AsyncSession):
self.db = db
async def get_by_id(self, photo_id: int) -> Optional[ContestCarouselPhoto]:
stmt = select(ContestCarouselPhoto).filter_by(id=photo_id)
result = await self.db.execute(stmt)
return result.scalars().first()
async def get_by_contest_id(self, contest_id: int) -> Sequence[ContestCarouselPhoto]:
stmt = select(ContestCarouselPhoto).filter_by(contest_id=contest_id)
result = await self.db.execute(stmt)
return result.scalars().all()
async def create(self, photo: ContestCarouselPhoto) -> ContestCarouselPhoto:
self.db.add(photo)
await self.db.commit()
await self.db.refresh(photo)
return photo
async def delete(self, photo: ContestCarouselPhoto) -> ContestCarouselPhoto:
await self.db.delete(photo)
await self.db.commit()
return photo