Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update 'db-models/', add login route and dependents routes #5

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions gestao/db/models/dependent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion gestao/db/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,28 @@ 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)
state: str = ormar.String(max_length=100)
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)
Expand Down
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"])
41 changes: 26 additions & 15 deletions gestao/web/api/user/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
17 changes: 16 additions & 1 deletion gestao/web/api/user/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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")


Loading