Skip to content

Commit

Permalink
feat: add status field
Browse files Browse the repository at this point in the history
  • Loading branch information
Matheusafonsouza committed Oct 29, 2023
1 parent 231a13b commit b04b163
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ def upgrade():
batch_op.add_column(
sa.Column("nickname", sa.String(200), nullable=True),
)
batch_op.add_column(
sa.Column("status", sa.String(200), default="active"),
)


def downgrade():
with op.batch_alter_table("user") as batch_op:
batch_op.drop_column("workstation")
batch_op.drop_column("nickname")
batch_op.drop_column("status")
1 change: 1 addition & 0 deletions gestao/db/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Meta(BaseMeta):
role: str = ormar.String(max_length=200)
category: str = ormar.String(max_length=200)
pattern: str = ormar.String(max_length=200)
status: str = ormar.String(max_length=200, default="active")
workstation: Optional[str] = ormar.String(max_length=200, nullable=True)
nickname: Optional[str] = ormar.String(max_length=200, unique=True, nullable=True)
dispatcher: str = ormar.String(max_length=200)
Expand Down
6 changes: 6 additions & 0 deletions gestao/web/api/user/enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from enum import Enum


class UserStatus(str, Enum):
active = "active"
inactive = "inactive"
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,4 @@ class UpdateUserDTO(BaseModel):
dispatched_date: Optional[date]
workstation: Optional[str]
nickname: Optional[str]
status: Optional[str]
51 changes: 41 additions & 10 deletions gestao/web/api/user/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
from typing import List
from uuid import uuid4

from asyncpg.exceptions import UniqueViolationError
from fastapi import APIRouter, HTTPException

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.enums import UserStatus
from gestao.web.api.user.schemas import CreateUserDTO, UpdateUserDTO

router = APIRouter()

Expand All @@ -16,13 +18,22 @@ async def get_users(
limit: int = 10,
offset: int = 0,
) -> List[User]:
return await User.objects.limit(limit).offset(offset).all()
return (
await User.objects.limit(limit)
.offset(
offset,
)
.filter(status=UserStatus.active)
.all()
)


@router.get("/{user_id}", response_model_exclude={"dependents__user_id"})
async def get_user(user_id: str) -> User:
try:
return await User.objects.select_related(User.dependents).get(id=user_id)
return await User.objects.select_related(
User.dependents,
).get(id=user_id)
except Exception:
logging.error("Error occurred while get user", exc_info=True)
raise HTTPException(status_code=404, detail="User not found")
Expand All @@ -34,7 +45,9 @@ async def create_user(create_user: CreateUserDTO) -> User:
create_user_dict = create_user.dict()
dependents = create_user_dict.pop("dependents", [])
user_id = str(uuid4())
await User.objects.create(id=user_id, **create_user_dict)
await User.objects.create(
id=user_id, **create_user_dict, status=UserStatus.active
)
if dependents:
await Dependent.objects.bulk_create(
[
Expand All @@ -43,6 +56,12 @@ async def create_user(create_user: CreateUserDTO) -> User:
],
)
return await User.objects.select_related(User.dependents).get(id=user_id)
except UniqueViolationError:
logging.error("User already exists", exc_info=True)
raise HTTPException(
status_code=400,
detail="User already exists",
)
except Exception:
logging.error("Error occurred while creating user", exc_info=True)
raise HTTPException(
Expand All @@ -54,16 +73,14 @@ async def create_user(create_user: CreateUserDTO) -> User:
@router.put("/{user_id}", response_model_exclude={"dependents__user_id"})
async def update_user(user_id: str, update_user: UpdateUserDTO) -> User:
try:
await User.objects.filter(id=user_id).update(
each=True,
**update_user.dict(exclude_none=True),
)
user = await User.objects.get(id=user_id)
await user.update(**update_user.dict(exclude_none=True))
return await User.objects.select_related(User.dependents).get(id=user_id)
except Exception:
logging.error("Error occurred while updating user", exc_info=True)
logging.error("User not found", exc_info=True)
raise HTTPException(
status_code=404,
detail="Error occurred while updating user",
detail="User not found",
)


Expand All @@ -77,3 +94,17 @@ async def delete_user(user_id: str) -> None:
status_code=404,
detail="Error occurred while deleting user",
)


@router.patch("/{user_id}/disable")
async def disable_user(user_id: str) -> None:
try:
user = await User.objects.get(id=user_id)
await user.update(status=UserStatus.inactive)
return {"detail": "User disabled successfully"}
except Exception:
logging.error("User not found", exc_info=True)
raise HTTPException(
status_code=404,
detail="User not found",
)

0 comments on commit b04b163

Please sign in to comment.