Skip to content

Commit

Permalink
Raise 404 when the todo key is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
Donnype committed Jan 9, 2024
1 parent b1af8b8 commit ff18880
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
9 changes: 7 additions & 2 deletions api/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import List, Optional

from fastapi import Depends, FastAPI
from fastapi import Depends, FastAPI, HTTPException
from starlette.responses import RedirectResponse
from starlette.status import HTTP_201_CREATED

Expand All @@ -23,7 +23,12 @@ def create(key: str, value: str, todo_repository: TodoRepository = Depends(creat
@app.get("/get/{key}", response_model=Optional[Todo])
def get(key: str, todo_repository: TodoRepository = Depends(create_todo_repository)):
with todo_repository as repo:
return repo.get_by_key(key)
todo = repo.get_by_key(key)

if not todo:
raise HTTPException(status_code=404, detail="Todo not found")

return todo


@app.get("/find", response_model=List[Todo])
Expand Down
3 changes: 3 additions & 0 deletions api/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,6 @@ def test_api():

assert response.status_code == 200
assert response.json() == {"key": "testkey", "value": "testvalue", "done": False}

response = client.get("/get/wrong")
assert response.status_code == 404

0 comments on commit ff18880

Please sign in to comment.