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

38 lines
1.1 KiB
Python

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