Skip to content

Commit ef9304a

Browse files
authored
Create atleta_repo.py
1 parent f50d6f2 commit ef9304a

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

app/repositories/atleta_repo.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from sqlalchemy import select, func
2+
from sqlalchemy.orm import selectinload
3+
from sqlalchemy.ext.asyncio import AsyncSession
4+
from app.models.atleta import Atleta
5+
6+
class AtletaRepository:
7+
def __init__(self, session: AsyncSession):
8+
self.session = session
9+
10+
async def get_by_cpf(self, cpf: str) -> Atleta | None:
11+
q = select(Atleta).where(Atleta.cpf == cpf).options(selectinload(Atleta.categoria), selectinload(Atleta.centro))
12+
result = await self.session.execute(q)
13+
return result.scalars().first()
14+
15+
async def list(self, nome: str | None = None, cpf: str | None = None, limit: int = 10, offset: int = 0) -> list[Atleta]:
16+
q = select(Atleta).options(selectinload(Atleta.categoria), selectinload(Atleta.centro))
17+
if nome:
18+
q = q.where(Atleta.nome.ilike(f"%{nome}%"))
19+
if cpf:
20+
q = q.where(Atleta.cpf == cpf)
21+
q = q.limit(limit).offset(offset)
22+
result = await self.session.execute(q)
23+
return result.scalars().all()
24+
25+
async def count(self, nome: str | None = None, cpf: str | None = None) -> int:
26+
q = select(func.count()).select_from(Atleta)
27+
if nome:
28+
q = q.where(Atleta.nome.ilike(f"%{nome}%"))
29+
if cpf:
30+
q = q.where(Atleta.cpf == cpf)
31+
result = await self.session.execute(q)
32+
return result.scalar_one()
33+
34+
async def create(self, atleta: Atleta) -> Atleta:
35+
self.session.add(atleta)
36+
await self.session.commit()
37+
await self.session.refresh(atleta)
38+
return atleta

0 commit comments

Comments
 (0)