diff --git a/gestao/web/api/router.py b/gestao/web/api/router.py index 68b5cb9..9c98314 100644 --- a/gestao/web/api/router.py +++ b/gestao/web/api/router.py @@ -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"]) diff --git a/gestao/web/api/user/__init__.py b/gestao/web/api/user/__init__.py new file mode 100644 index 0000000..8ee7aa8 --- /dev/null +++ b/gestao/web/api/user/__init__.py @@ -0,0 +1,3 @@ +from gestao.web.api.user.views import router + +__all__ = ["router"] diff --git a/gestao/web/api/user/schema.py b/gestao/web/api/user/schema.py new file mode 100644 index 0000000..f268388 --- /dev/null +++ b/gestao/web/api/user/schema.py @@ -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] diff --git a/gestao/web/api/user/views.py b/gestao/web/api/user/views.py new file mode 100644 index 0000000..c9db784 --- /dev/null +++ b/gestao/web/api/user/views.py @@ -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")