From d9d4157a191fb5835a6167cdd5313e53e5731e6f Mon Sep 17 00:00:00 2001 From: LamequeFernandes Date: Tue, 30 Aug 2022 13:33:30 -0300 Subject: [PATCH 1/5] criacao do pattern repositories para professor. #1 metodo de criacao de professor utilizando o banco de dados assincrono --- src/repositories/professor.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/repositories/professor.py diff --git a/src/repositories/professor.py b/src/repositories/professor.py new file mode 100644 index 0000000..eb86d70 --- /dev/null +++ b/src/repositories/professor.py @@ -0,0 +1,25 @@ +from typing import List + +from src.models.professor import ProfessorModel + +from fastapi import HTTPException, status + +from src.schemas.professor import ProfessorSchema + +from sqlalchemy.ext.asyncio import AsyncSession +from sqlalchemy.future import select + + +class ProfessorRepository: + + async def create(self, professor: ProfessorSchema, db: AsyncSession): + novo_professor: ProfessorModel = ProfessorModel(**professor.dict()) + + try: + db.add(novo_professor) + await db.commit() + except Exception as error: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, detail=error) + + return novo_professor \ No newline at end of file From 6290aa52e4af7dc7dcbf1082b3cc2755f4072320 Mon Sep 17 00:00:00 2001 From: LamequeFernandes Date: Tue, 30 Aug 2022 13:34:34 -0300 Subject: [PATCH 2/5] criacao do endpoint de post para professor. #1 --- src/api/endpoints/professor.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/api/endpoints/professor.py diff --git a/src/api/endpoints/professor.py b/src/api/endpoints/professor.py new file mode 100644 index 0000000..51bf888 --- /dev/null +++ b/src/api/endpoints/professor.py @@ -0,0 +1,22 @@ +from fastapi import APIRouter, status, Depends + +from typing import List + +from src.schemas.professor import ProfessorSchema +from sqlalchemy.ext.asyncio import AsyncSession + +from src.repositories.professor import ProfessorRepository + +from db.database import get_session + + +router = APIRouter() + +professor_repository = ProfessorRepository() + + +@router.post('/', status_code=status.HTTP_201_CREATED, response_model=ProfessorSchema) +async def create_professor(professor: ProfessorSchema, db: AsyncSession = Depends(get_session)): + professor = await professor_repository.create(professor=professor, db=db) + return professor + From 80beb9998fa009148b2094fe8d696cd3784e1deb Mon Sep 17 00:00:00 2001 From: LamequeFernandes Date: Tue, 30 Aug 2022 13:41:03 -0300 Subject: [PATCH 3/5] adicao do endpoint de professor nas rotas e na main. #1 --- main.py | 4 +++- src/api/routers/routers.py | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 src/api/routers/routers.py diff --git a/main.py b/main.py index 5de2b50..c8f416d 100644 --- a/main.py +++ b/main.py @@ -4,6 +4,7 @@ from src.schemas.aluno import AlunoSchema from db.database import get_session +from src.api.routers.routers import api_router from sqlalchemy.ext.asyncio import AsyncSession @@ -12,8 +13,9 @@ # Exemplos apenas para teste app = FastAPI() +app.include_router(api_router) -@app.get('/') +@app.get('/', status_code=status.HTTP_200_OK) def teste(): return {"teste": "teste"} diff --git a/src/api/routers/routers.py b/src/api/routers/routers.py new file mode 100644 index 0000000..937db86 --- /dev/null +++ b/src/api/routers/routers.py @@ -0,0 +1,9 @@ +from fastapi import APIRouter + +from src.api.endpoints import professor + + +api_router = APIRouter() +api_router.include_router(professor.router, + prefix='/professor', + tags=['professor']) \ No newline at end of file From 89a768742655e0738dcfa8dc96e48a1f7f74f197 Mon Sep 17 00:00:00 2001 From: LamequeFernandes Date: Tue, 30 Aug 2022 16:16:41 -0300 Subject: [PATCH 4/5] feat: repositories e endpoint delete para professor. #2 --- .gitignore | 2 ++ requirements.txt | 12 ++++++++++++ src/api/endpoints/professor.py | 9 ++++++++- src/repositories/professor.py | 25 ++++++++++++++++++++++++- 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 6769e21..78c62d1 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/requirements.txt b/requirements.txt index edf4dac..55ef284 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/src/api/endpoints/professor.py b/src/api/endpoints/professor.py index 51bf888..0069004 100644 --- a/src/api/endpoints/professor.py +++ b/src/api/endpoints/professor.py @@ -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 \ No newline at end of file diff --git a/src/repositories/professor.py b/src/repositories/professor.py index eb86d70..68c4f91 100644 --- a/src/repositories/professor.py +++ b/src/repositories/professor.py @@ -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()) @@ -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 \ No newline at end of file + 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") \ No newline at end of file From 6ab779a493ed101eec9fb23b2f964442f909363e Mon Sep 17 00:00:00 2001 From: LamequeFernandes Date: Tue, 30 Aug 2022 18:37:20 -0300 Subject: [PATCH 5/5] teste basicos. #1 --- test/__init__.py | 0 test/tests_professor.py | 58 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 test/__init__.py create mode 100644 test/tests_professor.py diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/tests_professor.py b/test/tests_professor.py new file mode 100644 index 0000000..3567729 --- /dev/null +++ b/test/tests_professor.py @@ -0,0 +1,58 @@ +from fastapi.testclient import TestClient + +from main import app + + +client = TestClient(app, base_url="http://localhost") + + +def test_post_professor_status(): + response = client.post( + url="/professor/99" + ) + assert response.status_code == 405 + + +def test_post_professor_json(): + response = client.post( + url="/professor/", + json={ "id": 1000, + "nome": "Joãozinho", + "email": "test_3@gmail.com", + "senha": "bekoajnsd", + "numero_telefone": "81556698899", + "dt_nascumento": "1985-08-02 17:41:02.621282", + "created_at": "2022-08-02 17:41:02.621282", + "matricula": 150123654, + "is_coordenador": False} + ) + assert response.status_code == 201 + assert response.json() == { + "id": 1000, + "nome": "Joãozinho", + "email": "test_3@gmail.com", + "senha": "bekoajnsd", + "numero_telefone": "81556698899", + "dt_nascimento": None, + "created_at": "2022-08-02T17:41:02.621282", + "matricula": 150123654, + "is_coordenador": False + } + + +def test_post_professor_json_url(): + response = client.post( + url="/professor", + json={ "nome": "Elaine", + "email": "test_34@gmail.com", + "senha": "bekoajnasdasd21312sd", + "numero_telefone": "815566988499", + "dt_nascumento": "1985-08-02 17:41:02.621282", + "created_at": "2022-08-02 17:41:02.621282", + "matricula": 43120123654, + "is_coordenador": False} + ) + assert response.status_code == 307 + + +