-
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: Mateus Maia <[email protected]>
- Loading branch information
1 parent
c0a4e3b
commit e409f5d
Showing
6 changed files
with
128 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,8 +5,6 @@ | |
|
||
|
||
class User(ormar.Model): | ||
"""User model.""" | ||
|
||
class Meta(BaseMeta): | ||
tablename = "user" | ||
|
||
|
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
from fastapi.routing import APIRouter | ||
|
||
from gestao.web.api import dummy, echo, monitoring | ||
from gestao.web.api import echo, monitoring, user | ||
|
||
api_router = APIRouter() | ||
api_router.include_router(monitoring.router) | ||
api_router.include_router(echo.router, prefix="/echo", tags=["echo"]) | ||
api_router.include_router(dummy.router, prefix="/dummy", tags=["dummy"]) | ||
api_router.include_router(user.router, prefix="/user", tags=["user"]) |
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.user.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,66 @@ | ||
from typing import Optional | ||
|
||
from pydantic import BaseModel | ||
from datetime import date | ||
|
||
|
||
class CreateUserDTO(BaseModel): | ||
name: str | ||
address: str | ||
neighborhood: str | ||
city: str | ||
state: str | ||
zipcode: str | ||
cpf: str | ||
rg: str | ||
birth_date: date | ||
place_of_birth: str | ||
blood_type: str | ||
gender: str | ||
father_name: str | ||
mother_date: str | ||
position: str | ||
occupancy: str | ||
admission_date: date | ||
situation: str | ||
phone: str | ||
email: str | ||
marital_status: str | ||
education: str | ||
registration: str | ||
role: str | ||
category: str | ||
pattern: str | ||
dispatcher: str | ||
dispatched_date: date | ||
|
||
|
||
class UpdateUserDTO(BaseModel): | ||
name: Optional[str] | ||
address: Optional[str] | ||
neighborhood: Optional[str] | ||
city: Optional[str] | ||
state: Optional[str] | ||
zipcode: Optional[str] | ||
cpf: Optional[str] | ||
rg: Optional[str] | ||
birth_date: Optional[date] | ||
place_of_birth: Optional[str] | ||
blood_type: Optional[str] | ||
gender: Optional[str] | ||
father_name: Optional[str] | ||
mother_date: Optional[str] | ||
position: Optional[str] | ||
occupancy: Optional[str] | ||
admission_date: Optional[date] | ||
situation: 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] |
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,57 @@ | ||
from typing import List | ||
|
||
from uuid import uuid4 | ||
from fastapi import APIRouter, HTTPException | ||
|
||
from gestao.db.models.user import User | ||
from gestao.web.api.user.schema import CreateUserDTO, UpdateUserDTO | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.get("/") | ||
async def get_users( | ||
limit: int = 10, | ||
offset: int = 0, | ||
) -> List[User]: | ||
return await User.objects.limit(limit).offset(offset).all() | ||
|
||
|
||
@router.get("/{user_id}") | ||
async def get_user(user_id: str) -> User: | ||
try: | ||
return await User.objects.get(id=user_id) | ||
except Exception: | ||
raise HTTPException(status_code=404, detail="User not found") | ||
|
||
|
||
@router.post("/") | ||
async def create_user(create_user: CreateUserDTO) -> None: | ||
try: | ||
return await User.objects.create(**{ | ||
"id": str(uuid4()), | ||
**create_user.dict(), | ||
}) | ||
except Exception: | ||
return HTTPException(status_code=400, detail="Error occurred while creating user") | ||
|
||
|
||
@router.put("/{user_id}") | ||
async def update_user(user_id: str, update_user: UpdateUserDTO) -> None: | ||
try: | ||
await User.objects.filter(id=user_id).update( | ||
each=True, | ||
**update_user.dict(exclude_none=True), | ||
) | ||
return await User.objects.get(id=user_id) | ||
except Exception: | ||
raise HTTPException(status_code=404, detail="Error occurred while updating user") | ||
|
||
|
||
|
||
@router.delete("/{user_id}") | ||
async def delete_user(user_id: str) -> None: | ||
try: | ||
return await User.objects.delete(id=user_id) | ||
except Exception: | ||
raise HTTPException(status_code=404, detail="Error occurred while deleting user") |