Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Desafio FastApi #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
run:
@uvicorn workout_api.main:app --reload

create-migrations:
@PYTHONPATH=$PYTHONPATH:$(pwd) alembic revision --autogenerate -m $(d)

run-migrations:
@PYTHONPATH=$PYTHONPATH:$(pwd) alembic upgrade head
90 changes: 11 additions & 79 deletions workout_api/atleta/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ async def post(
db_session: DatabaseDependency,
atleta_in: AtletaIn = Body(...)
):
# Check if an athlete with the same CPF already exists
existing_atleta = (await db_session.execute(
select(AtletaModel).filter_by(cpf=atleta_in.cpf))
).scalars().first()

if existing_atleta:
raise HTTPException(
status_code=status.HTTP_303_SEE_OTHER,
detail=f'Já existe um atleta cadastrado com o cpf: {atleta_in.cpf}'
)

categoria_nome = atleta_in.categoria.nome
centro_treinamento_nome = atleta_in.centro_treinamento.nome

Expand Down Expand Up @@ -61,82 +72,3 @@ async def post(
)

return atleta_out


@router.get(
'/',
summary='Consultar todos os Atletas',
status_code=status.HTTP_200_OK,
response_model=list[AtletaOut],
)
async def query(db_session: DatabaseDependency) -> list[AtletaOut]:
atletas: list[AtletaOut] = (await db_session.execute(select(AtletaModel))).scalars().all()

return [AtletaOut.model_validate(atleta) for atleta in atletas]


@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: AtletaOut = (
await db_session.execute(select(AtletaModel).filter_by(id=id))
).scalars().first()

if not atleta:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f'Atleta não encontrado no id: {id}'
)

return atleta


@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: AtletaOut = (
await db_session.execute(select(AtletaModel).filter_by(id=id))
).scalars().first()

if not atleta:
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, key, value)

await db_session.commit()
await db_session.refresh(atleta)

return atleta


@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: AtletaOut = (
await db_session.execute(select(AtletaModel).filter_by(id=id))
).scalars().first()

if not atleta:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f'Atleta não encontrado no id: {id}'
)

await db_session.delete(atleta)
await db_session.commit()
8 changes: 5 additions & 3 deletions workout_api/atleta/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


class Atleta(BaseSchema):
nome: Annotated[str, Field(description='Nome do atleta', example='Joao', max_length=50)]
nome: Annotated[str, Field(description='Nome do atleta', example='Paulo', max_length=50)]
cpf: Annotated[str, Field(description='CPF do atleta', example='12345678900', max_length=11)]
idade: Annotated[int, Field(description='Idade do atleta', example=25)]
peso: Annotated[PositiveFloat, Field(description='Peso do atleta', example=75.5)]
Expand All @@ -25,5 +25,7 @@ class AtletaOut(Atleta, OutMixin):
pass

class AtletaUpdate(BaseSchema):
nome: Annotated[Optional[str], Field(None, description='Nome do atleta', example='Joao', max_length=50)]
idade: Annotated[Optional[int], Field(None, description='Idade do atleta', example=25)]
nome: Annotated[Optional[str], Field(None, description='Nome do atleta', example='Paulo', max_length=50)]
idade: Annotated[Optional[int], Field(None, description='Idade do atleta', example=25)]


2 changes: 2 additions & 0 deletions workout_api/centro_treinamento/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ async def get(id: UUID4, db_session: DatabaseDependency) -> CentroTreinamentoOut
await db_session.execute(select(CentroTreinamentoModel).filter_by(id=id))
).scalars().first()

breakpoint()

if not centro_treinamento_out:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
Expand Down
4 changes: 2 additions & 2 deletions workout_api/centro_treinamento/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
class CentroTreinamentoIn(BaseSchema):
nome: Annotated[str, Field(description='Nome do centro de treinamento', example='CT King', max_length=20)]
endereco: Annotated[str, Field(description='Endereço do centro de treinamento', example='Rua X, Q02', max_length=60)]
proprietario: Annotated[str, Field(description='Proprietario do centro de treinamento', example='Marcos', max_length=30)]
proprietario: Annotated[str, Field(description='Proprietario do centro de treinamento', example='Altair', max_length=30)]


class CentroTreinamentoAtleta(BaseSchema):
nome: Annotated[str, Field(description='Nome do centro de treinamento', example='CT King', max_length=20)]
nome: Annotated[str, Field(description='Nome do centro de treinamento', example='King of Kings JESUS', max_length=20)]


class CentroTreinamentoOut(CentroTreinamentoIn):
Expand Down