Skip to content

Commit

Permalink
feat: add users route
Browse files Browse the repository at this point in the history
Co-authored-by: Mateus Maia <[email protected]>
  • Loading branch information
Matheusafonsouza and mateusmaiamaia committed Oct 22, 2023
1 parent c0a4e3b commit e409f5d
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 10 deletions.
2 changes: 0 additions & 2 deletions gestao/db/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@


class User(ormar.Model):
"""User model."""

class Meta(BaseMeta):
tablename = "user"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@

@pytest.mark.anyio
async def test_health(client: AsyncClient, fastapi_app: FastAPI) -> None:
"""
Checks the health endpoint.
:param client: client for the app.
:param fastapi_app: current FastAPI application.
"""
url = fastapi_app.url_path_for("health_check")
response = await client.get(url)
assert response.status_code == status.HTTP_200_OK
4 changes: 2 additions & 2 deletions gestao/web/api/router.py
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"])
3 changes: 3 additions & 0 deletions gestao/web/api/user/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from gestao.web.api.user.views import router

__all__ = ["router"]
66 changes: 66 additions & 0 deletions gestao/web/api/user/schema.py
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]
57 changes: 57 additions & 0 deletions gestao/web/api/user/views.py
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")

0 comments on commit e409f5d

Please sign in to comment.