Spaces:
Sleeping
Sleeping
File size: 5,650 Bytes
224e40f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | from datetime import datetime
from uuid import uuid4
from fastapi import APIRouter, Body, HTTPException, Query, status
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import selectinload
from pydantic import UUID4
from fastapi_pagination import Page, paginate
from workout_api.atleta.schemas import AtletaIn, AtletaOut, AtletaUpdate, AtletaCustomOut
from workout_api.atleta.models import AtletaModel
from workout_api.categorias.models import CategoriaModel
from workout_api.centro_treinamento.models import CentroTreinamentoModel
from workout_api.contrib.dependencies import DatabaseDependency
from sqlalchemy.future import select
router = APIRouter()
@router.post(
'/',
summary='Criar um novo atleta',
status_code=status.HTTP_201_CREATED,
response_model=AtletaOut
)
async def post(
db_session: DatabaseDependency,
atleta_in: AtletaIn = Body(...)
):
categoria_nome = atleta_in.categoria.nome
centro_treinamento_nome = atleta_in.centro_treinamento.nome
categoria = (await db_session.execute(
select(CategoriaModel).filter_by(nome=categoria_nome))
).scalars().first()
if not categoria:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f'A categoria {categoria_nome} não foi encontrada.'
)
centro_treinamento = (await db_session.execute(
select(CentroTreinamentoModel).filter_by(nome=centro_treinamento_nome))
).scalars().first()
if not centro_treinamento:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f'O centro de treinamento {centro_treinamento_nome} não foi encontrado.'
)
try:
atleta_model = AtletaModel(
**atleta_in.model_dump(exclude={'categoria', 'centro_treinamento'}),
categoria_id=categoria.pk_id,
centro_treinamento_id=centro_treinamento.pk_id,
created_at=datetime.utcnow()
)
db_session.add(atleta_model)
await db_session.commit()
await db_session.refresh(atleta_model)
except IntegrityError:
await db_session.rollback()
raise HTTPException(
status_code=status.HTTP_303_SEE_OTHER,
detail=f"Já existe um atleta cadastrado com o cpf: {atleta_in.cpf}"
)
except Exception as e:
await db_session.rollback()
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f'Ocorreu um erro ao inserir os dados no banco: {e}'
)
return AtletaOut.model_validate(atleta_model)
@router.get(
'/',
summary='Consultar Atletas com filtros e paginação',
status_code=status.HTTP_200_OK,
response_model=Page[AtletaCustomOut],
)
async def query(
db_session: DatabaseDependency,
nome: str | None = Query(None, description="Filtrar atleta por nome"),
cpf: str | None = Query(None, description="Filtrar atleta por CPF")
) -> Page[AtletaCustomOut]:
stmt = select(AtletaModel).options(
selectinload(AtletaModel.categoria),
selectinload(AtletaModel.centro_treinamento)
)
if nome:
stmt = stmt.where(AtletaModel.nome.ilike(f"%{nome}%"))
if cpf:
stmt = stmt.where(AtletaModel.cpf == cpf)
atletas: list[AtletaModel] = (await db_session.execute(stmt)).scalars().all()
atletas_custom = [
AtletaCustomOut(
nome=atleta.nome,
categoria=atleta.categoria.nome,
centro_treinamento=atleta.centro_treinamento.nome
) for atleta in atletas
]
return paginate(atletas_custom)
@router.get(
'/{id}',
summary='Consulta um Atleta pelo id',
status_code=status.HTTP_200_OK,
response_model=AtletaOut,
)
async def get(id: UUID4, db_session: DatabaseDependency) -> AtletaOut:
atleta_model: AtletaModel | None = (
await db_session.execute(select(AtletaModel).filter_by(id=id))
).scalars().first()
if not atleta_model:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f'Atleta não encontrado no id: {id}'
)
return AtletaOut.model_validate(atleta_model)
@router.patch(
'/{id}',
summary='Editar um Atleta pelo id',
status_code=status.HTTP_200_OK,
response_model=AtletaOut,
)
async def patch(id: UUID4, db_session: DatabaseDependency, atleta_up: AtletaUpdate = Body(...)) -> AtletaOut:
atleta_model: AtletaModel | None = (
await db_session.execute(select(AtletaModel).filter_by(id=id))
).scalars().first()
if not atleta_model:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f'Atleta não encontrado no id: {id}'
)
atleta_update = atleta_up.model_dump(exclude_unset=True)
for key, value in atleta_update.items():
setattr(atleta_model, key, value)
await db_session.commit()
await db_session.refresh(atleta_model)
return AtletaOut.model_validate(atleta_model)
@router.delete(
'/{id}',
summary='Deletar um Atleta pelo id',
status_code=status.HTTP_204_NO_CONTENT
)
async def delete(id: UUID4, db_session: DatabaseDependency) -> None:
atleta_model: AtletaModel | None = (
await db_session.execute(select(AtletaModel).filter_by(id=id))
).scalars().first()
if not atleta_model:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f'Atleta não encontrado no id: {id}'
)
await db_session.delete(atleta_model)
await db_session.commit()
|