Skip to content

Commit

Permalink
feat: add dependents routes
Browse files Browse the repository at this point in the history
Co-Authored-by: Vinicius Castelo <[email protected]>
  • Loading branch information
Eduard0803 and Vini47 committed Oct 28, 2023
1 parent 594a3f6 commit a1b970b
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 18 deletions.
3 changes: 3 additions & 0 deletions gestao/web/api/dependent/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from gestao.web.api.dependent.views import router

__all__ = ["router"]
23 changes: 23 additions & 0 deletions gestao/web/api/dependent/schema.py
Original file line number Diff line number Diff line change
@@ -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]

58 changes: 58 additions & 0 deletions gestao/web/api/dependent/views.py
Original file line number Diff line number Diff line change
@@ -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")


3 changes: 2 additions & 1 deletion gestao/web/api/router.py
Original file line number Diff line number Diff line change
@@ -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"])
18 changes: 1 addition & 17 deletions gestao/web/api/user/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit a1b970b

Please sign in to comment.