Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import math


app = FastAPI(title="FastAPI Worksohp", version="0.1.0")
app = FastAPI(title="FastAPI Workshop", version="0.1.0")

app.add_middleware(
CORSMiddleware,
Expand All @@ -18,20 +18,22 @@
)


@app.get("/health", status_code=201)
@app.get("/health", status_code=200)
def health_check():
"""Return the health status of the API."""
return {"status": "ok"}


@app.get("/sum")
def compute_sum(a: int = Query(...), b: int = Query(...)):
return {"result": a * b}

return {"result": a + b}


def format_profile(data):
return {
"username": data["username"],
"name": data["username"],
"username": data["username"],
"bio": data["bio"],
"age": data.get("age"),
}
Expand Down Expand Up @@ -59,8 +61,9 @@ def get_profile(username: str):
@app.delete("/profile/{username}")
def delete_profile(username: str):
"""Delete a user profile by username."""

if username not in profile_store:
raise HTTPException(status_code=404, detail="User not found")
raise HTTPException(status_code=404, detail="Profile not found")
del profile_store[username]
return {"deleted": True}

Expand All @@ -72,12 +75,11 @@ def search_profiles(
limit: int = Query(default=10, ge=1),
):
"""Search profiles by username or bio."""
if not q:
return {"results": [], "total": 0}

results = [
p
for p in profile_store.values()
if q.lower() in p["username"].lower() or q.lower() in p["bio"].lower()
]
return {"results": results[offset : offset + limit - 1], "total": len(results)}

return {"results": results[offset : offset + limit], "total": len(results)}