From 96f3fef4a219eb5347c0e2a04d6a3903ec31d46f Mon Sep 17 00:00:00 2001 From: Eduard0803 Date: Fri, 27 Oct 2023 22:54:40 -0300 Subject: [PATCH] feat: add dependents routes Co-Authored-by: Vinicius Castelo --- gestao/db/models/dependent.py | 3 ++ gestao/db/models/user.py | 7 +++- gestao/web/api/dependent/__init__.py | 3 ++ gestao/web/api/dependent/schema.py | 23 +++++++++++ gestao/web/api/dependent/views.py | 58 ++++++++++++++++++++++++++++ gestao/web/api/router.py | 3 +- gestao/web/api/user/schema.py | 41 +++++++++++++------- gestao/web/api/user/views.py | 17 +++++++- 8 files changed, 137 insertions(+), 18 deletions(-) create mode 100644 gestao/web/api/dependent/__init__.py create mode 100644 gestao/web/api/dependent/schema.py create mode 100644 gestao/web/api/dependent/views.py diff --git a/gestao/db/models/dependent.py b/gestao/db/models/dependent.py index 75d4722..b9fc7bf 100644 --- a/gestao/db/models/dependent.py +++ b/gestao/db/models/dependent.py @@ -13,8 +13,11 @@ class Meta(BaseMeta): id: str = ormar.String(max_length=200, primary_key=True) user_id: User = ormar.ForeignKey(User, ondelete=ormar.ReferentialAction.CASCADE) name: str = ormar.String(max_length=200) + cpf: str = ormar.String(max_length=200, unique=True) birth_date: date = ormar.Date() + gender: str = ormar.String(max_length=200) relationship: str = ormar.String(max_length=200) + deceased: bool = ormar.Boolean(default=False) created_at: datetime = ormar.DateTime(timezone=True, default=datetime.now) updated_at: datetime = ormar.DateTime( timezone=True, diff --git a/gestao/db/models/user.py b/gestao/db/models/user.py index 85ec5ac..52e937a 100644 --- a/gestao/db/models/user.py +++ b/gestao/db/models/user.py @@ -11,6 +11,8 @@ class Meta(BaseMeta): id: str = ormar.String(max_length=200, primary_key=True) name: str = ormar.String(max_length=200) + password: str = ormar.String(max_length=200) + war_name: str = ormar.String(max_length=200) address: str = ormar.String(max_length=200) neighborhood: str = ormar.String(max_length=200) city: str = ormar.String(max_length=100) @@ -18,16 +20,19 @@ class Meta(BaseMeta): zipcode: str = ormar.String(max_length=100) cpf: str = ormar.String(max_length=200, unique=True) rg: str = ormar.String(max_length=200, unique=True) + rg_consignor: str = ormar.String(max_length=100) + rg_date: date = ormar.Date() birth_date: date = ormar.Date() place_of_birth: str = ormar.String(max_length=200) blood_type: str = ormar.String(max_length=200) gender: str = ormar.String(max_length=200) father_name: str = ormar.String(max_length=200) - mother_date: str = ormar.String(max_length=200) + mother_name: str = ormar.String(max_length=200) position: str = ormar.String(max_length=200) occupancy: str = ormar.String(max_length=200) admission_date: date = ormar.Date() situation: str = ormar.String(max_length=200) + situation_obs: str = ormar.String(max_length=300) phone: str = ormar.String(max_length=200) email: str = ormar.String(max_length=200) marital_status: str = ormar.String(max_length=200) diff --git a/gestao/web/api/dependent/__init__.py b/gestao/web/api/dependent/__init__.py new file mode 100644 index 0000000..38d5e79 --- /dev/null +++ b/gestao/web/api/dependent/__init__.py @@ -0,0 +1,3 @@ +from gestao.web.api.dependent.views import router + +__all__ = ["router"] diff --git a/gestao/web/api/dependent/schema.py b/gestao/web/api/dependent/schema.py new file mode 100644 index 0000000..c3f148b --- /dev/null +++ b/gestao/web/api/dependent/schema.py @@ -0,0 +1,23 @@ +from datetime import date +from typing import Optional + +from pydantic import BaseModel + + +class CreateUserDependentDTO(BaseModel): + name: str + cpf: str + birth_date: date + gender: str + relationship: str + deceased: bool + + +class UpdateUserDependentDTO(BaseModel): + name: Optional[str] + cpf: Optional[str] + birth_date: Optional[date] + gender: Optional[str] + relationship: Optional[str] + deceased: Optional[bool] + diff --git a/gestao/web/api/dependent/views.py b/gestao/web/api/dependent/views.py new file mode 100644 index 0000000..f74f930 --- /dev/null +++ b/gestao/web/api/dependent/views.py @@ -0,0 +1,58 @@ +import logging +from typing import List +from uuid import uuid4 + +from fastapi import APIRouter, HTTPException + +from gestao.db.models.dependent import Dependent +from gestao.db.models.user import User +from gestao.web.api.dependent.schema import CreateUserDependentDTO, UpdateUserDependentDTO + + +router = APIRouter() + + +@router.get("/{user_id}") +async def get_dependents(user_id: str) -> List[Dependent]: + try: + user = await User.objects.select_related(User.dependents).get(id=user_id) + return user.dependents + except Exception: + logging.error("Error occurred while get dependents", exc_info=True) + raise HTTPException(status_code=404, detail="Dependents not found") + + +@router.post("/{user_id}") +async def create_dependent(user_id: str, create_dependent: CreateUserDependentDTO) -> Dependent: + try: + dependent_dict = create_dependent.dict() + dependent_id = str(uuid4()) + await Dependent.objects.create(id=dependent_id, user_id=user_id, **dependent_dict) + return await Dependent.objects.get(id=dependent_id) + except Exception: + logging.error("Error occurred while creating dependent", exc_info=True) + raise HTTPException(status_code=400, detail="Error occurred while creating dependent") + + +@router.put("/{dependent_id}") +async def update_dependent(dependent_id: str, update_dependent: UpdateUserDependentDTO) -> Dependent: + try: + await Dependent.objects.filter(id=dependent_id).update( + each=True, + **update_dependent.dict(exclude_none=True) + ) + return await Dependent.objects.get(id=dependent_id) + except Exception: + logging.error("Error occurred while updating dependent", exc_info=True) + raise HTTPException(status_code=404, detail="Error occurred while updating dependent") + + +@router.delete("/{dependent_id}") +async def delete_dependent(dependent_id: str) -> None: + try: + await Dependent.objects.delete(id=dependent_id) + except Exception: + logging.error("Error occurred while deleting dependent", exc_info=True) + raise HTTPException(status_code=404, detail="Error occurred while deleting dependent") + + diff --git a/gestao/web/api/router.py b/gestao/web/api/router.py index 3788ffd..094c81f 100644 --- a/gestao/web/api/router.py +++ b/gestao/web/api/router.py @@ -1,8 +1,9 @@ from fastapi.routing import APIRouter -from gestao.web.api import echo, monitoring, user +from gestao.web.api import echo, monitoring, user, dependent api_router = APIRouter() api_router.include_router(monitoring.router) api_router.include_router(echo.router, prefix="/echo", tags=["echo"]) api_router.include_router(user.router, prefix="/users", tags=["users"]) +api_router.include_router(dependent.router, prefix="/dependents", tags=["dependents"]) diff --git a/gestao/web/api/user/schema.py b/gestao/web/api/user/schema.py index 4086acb..c2f5a39 100644 --- a/gestao/web/api/user/schema.py +++ b/gestao/web/api/user/schema.py @@ -3,37 +3,38 @@ from pydantic import BaseModel - -class CreateUserDependentDTO(BaseModel): - name: str - birth_date: date - relationship: str +from gestao.web.api.dependent.schema import CreateUserDependentDTO class CreateUserDTO(BaseModel): name: str + registration: str + password: str + war_name: str + zipcode: str address: str - neighborhood: str - city: str state: str - zipcode: str + city: str + neighborhood: str cpf: str rg: str + rg_consignor: str + rg_date: date birth_date: date place_of_birth: str blood_type: str gender: str father_name: str - mother_date: str + mother_name: str position: str occupancy: str admission_date: date situation: str + situation_obs: Optional[str] phone: str email: str marital_status: str education: str - registration: str role: str category: str pattern: str @@ -44,30 +45,40 @@ class CreateUserDTO(BaseModel): class UpdateUserDTO(BaseModel): name: Optional[str] + registration: Optional[str] + password: Optional[str] + war_name: Optional[str] + zipcode: Optional[str] address: Optional[str] - neighborhood: Optional[str] - city: Optional[str] state: Optional[str] - zipcode: Optional[str] + city: Optional[str] + neighborhood: Optional[str] cpf: Optional[str] rg: Optional[str] + rg_consignor: Optional[str] + rg_date: Optional[date] birth_date: Optional[date] place_of_birth: Optional[str] blood_type: Optional[str] gender: Optional[str] father_name: Optional[str] - mother_date: Optional[str] + mother_name: Optional[str] position: Optional[str] occupancy: Optional[str] admission_date: Optional[date] situation: Optional[str] + situation_obs: Optional[str] phone: Optional[str] email: Optional[str] marital_status: Optional[str] education: Optional[str] - registration: Optional[str] role: Optional[str] category: Optional[str] pattern: Optional[str] dispatcher: Optional[str] dispatched_date: Optional[date] + + +class AuthUserDTO(BaseModel): + registration: str + password: str diff --git a/gestao/web/api/user/views.py b/gestao/web/api/user/views.py index 98ed083..68fdd5e 100644 --- a/gestao/web/api/user/views.py +++ b/gestao/web/api/user/views.py @@ -6,7 +6,7 @@ from gestao.db.models.dependent import Dependent from gestao.db.models.user import User -from gestao.web.api.user.schema import CreateUserDTO, UpdateUserDTO +from gestao.web.api.user.schema import CreateUserDTO, UpdateUserDTO, AuthUserDTO router = APIRouter() @@ -77,3 +77,18 @@ async def delete_user(user_id: str) -> None: status_code=404, detail="Error occurred while deleting user", ) + + +@router.get("_auth/login") +async def user_auth(user_data: AuthUserDTO) -> User: + try: + user_data_dict = user_data.dict() + return await User.objects.get( + registration=user_data_dict['registration'], + password=user_data_dict['password'] + ) + except Exception: + logging.error("Error occurred in user_login", exc_info=True) + raise HTTPException(status_code=404, detail="Error occurred in login user") + +