Skip to content
This repository has been archived by the owner on Dec 24, 2024. It is now read-only.

Commit

Permalink
feat: add new endpoints to retrieve and delete users
Browse files Browse the repository at this point in the history
  • Loading branch information
1995parham committed Mar 19, 2024
1 parent 6902e2e commit 42fd1f5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
31 changes: 31 additions & 0 deletions app/routers/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import uuid

import fastapi
import fastapi.exceptions
import pydantic

from app import domain, services
Expand Down Expand Up @@ -49,6 +50,36 @@ async def users_list(
return storage.all()


@router.get("/{user_id}", tags=["users"])
async def users_get(
storage: typing.Annotated[services.Storage, fastapi.Depends()], user_id: str
):
"""
Retrieve a user specified by its ID.
"""
user = storage.retrieve(user_id)

if user is not None:
return user

raise fastapi.exceptions.HTTPException(status_code=404, detail="user not found")


@router.delete("/{user_id}", tags=["users"])
async def users_delete(
storage: typing.Annotated[services.Storage, fastapi.Depends()], user_id: str
):
"""
Retrieve a user specified by its ID.
"""
user = storage.delete(user_id)

if user is not None:
return user

raise fastapi.exceptions.HTTPException(status_code=404, detail="user not found")


@router.post("/", tags=["users"])
async def users_new(
storage: typing.Annotated[services.Storage, fastapi.Depends()],
Expand Down
16 changes: 16 additions & 0 deletions app/services/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ def append(self, user: domain.User):
if user.id not in self.storage:
self.storage[user.id] = user

def retrieve(self, user_id: str) -> domain.User | None:
"""
Retrieve the user by ID.
"""
return self.storage.get(user_id, None)

def delete(self, user_id: str) -> domain.User | None:
"""
Delete the user by ID.
"""
user = self.storage.get(user_id, None)
if user is None:
return None
del self.storage[user_id]
return user

def all(self) -> list[domain.User]:
"""
Return all the registered users.
Expand Down

0 comments on commit 42fd1f5

Please sign in to comment.