-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-Authored-by: Vinicius Castelo <[email protected]>
- Loading branch information
Showing
8 changed files
with
137 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from gestao.web.api.dependent.views import router | ||
|
||
__all__ = ["router"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters