diff --git a/app/routers/users.py b/app/routers/users.py index cdc4ca0..1efca9c 100644 --- a/app/routers/users.py +++ b/app/routers/users.py @@ -7,6 +7,7 @@ import uuid import fastapi +import fastapi.exceptions import pydantic from app import domain, services @@ -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()], diff --git a/app/services/store.py b/app/services/store.py index e69288f..32f6a32 100644 --- a/app/services/store.py +++ b/app/services/store.py @@ -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.