Skip to content

Commit

Permalink
feat: repositories e endpoint delete para professor. #2
Browse files Browse the repository at this point in the history
  • Loading branch information
LamequeFernandes committed Aug 30, 2022
1 parent 80beb99 commit 89a7687
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ dmypy.json
# Cython debug symbols
cython_debug/


.vscode
# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
Expand Down
12 changes: 12 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
alembic==1.8.1
anyio==3.6.1
asyncpg==0.26.0
attrs==22.1.0
certifi==2022.6.15
charset-normalizer==2.1.1
click==8.1.3
fastapi==0.79.1
greenlet==1.1.2
h11==0.13.0
idna==3.3
iniconfig==1.1.1
Mako==1.2.1
MarkupSafe==2.1.1
packaging==21.3
pluggy==1.0.0
psycopg2-binary==2.9.3
py==1.11.0
pydantic==1.9.2
pyparsing==3.0.9
pytest==7.1.2
requests==2.28.1
sniffio==1.2.0
SQLAlchemy==1.4.40
starlette==0.19.1
tomli==2.0.1
typing_extensions==4.3.0
urllib3==1.26.12
uvicorn==0.18.2
9 changes: 8 additions & 1 deletion src/api/endpoints/professor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@


@router.post('/', status_code=status.HTTP_201_CREATED, response_model=ProfessorSchema)
async def create_professor(professor: ProfessorSchema, db: AsyncSession = Depends(get_session)):
async def create_professor(
professor: ProfessorSchema,
db: AsyncSession=Depends(get_session)):
professor = await professor_repository.create(professor=professor, db=db)
return professor


@router.delete('/{professor_id}', status_code=status.HTTP_202_ACCEPTED)
async def delete_professor(professor_id: int, db: AsyncSession=Depends(get_session)):
result = await professor_repository.delete(id=professor_id, db=db)
return result
25 changes: 24 additions & 1 deletion src/repositories/professor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@

class ProfessorRepository:

async def professor_existe(self, id: int, db: AsyncSession):
async with db as session:
query_professor = select(ProfessorModel).filter(ProfessorModel.id == id)
result = await session.execute(query_professor)
professor = result.scalar()

if not professor:
return None
return professor

async def create(self, professor: ProfessorSchema, db: AsyncSession):
novo_professor: ProfessorModel = ProfessorModel(**professor.dict())

Expand All @@ -22,4 +32,17 @@ async def create(self, professor: ProfessorSchema, db: AsyncSession):
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail=error)

return novo_professor
return novo_professor

async def delete(self, id: int, db: AsyncSession):
professor = await self.professor_existe(id=id, db=db)

if not professor:
raise HTTPException(detail='Usuario não encontrado',
status_code=status.HTTP_404_NOT_FOUND)

async with db as session:
await session.delete(professor)
await session.commit()

return dict(message = "Usuario deletado com sucesso")

0 comments on commit 89a7687

Please sign in to comment.