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 6e310aa..81b34ed 100644 --- a/gestao/web/api/user/schema.py +++ b/gestao/web/api/user/schema.py @@ -3,23 +3,7 @@ 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] +from gestao.web.api.dependent.schema import CreateUserDependentDTO class CreateUserDTO(BaseModel):