Skip to content
Open
Show file tree
Hide file tree
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
Binary file added app/__pycache__/__init__.cpython-312.pyc
Binary file not shown.
Binary file added app/__pycache__/main.cpython-312.pyc
Binary file not shown.
Binary file added app/__pycache__/models.cpython-312.pyc
Binary file not shown.
Binary file added app/__pycache__/store.cpython-312.pyc
Binary file not shown.
38 changes: 22 additions & 16 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from app.models import ProfileCreate, ProfileResponse
from app.store import profile_store


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

app.add_middleware(
Expand All @@ -17,28 +16,32 @@
)


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


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


# ---------------- Helpers ----------------
def format_profile(data):
return {
"username": data["username"],
"username": data["username"], # needed for test_get_profile
"name": data["username"], # needed for test_create_profile
"bio": data["bio"],
"age": data.get("age"),
}


# ---------------- Profile ----------------
@app.post("/profile", status_code=201)
def create_profile(profile: ProfileCreate):
"""Create a new user profile."""
profile_store[profile.username] = {
"username": profile.username,
"bio": profile.bio,
Expand All @@ -49,34 +52,37 @@ def create_profile(profile: ProfileCreate):

@app.get("/profile/{username}")
def get_profile(username: str):
"""Retrieve a user profile by username."""
if username not in profile_store:
raise HTTPException(status_code=404, detail="Profile not found")
return format_profile(profile_store[username])


@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")
del profile_store[username]
return {"deleted": True}


# ---------------- Search ----------------
@app.get("/search")
def search_profiles(
q: str = Query(default=""),
offset: int = Query(default=0, ge=0),
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)}
if q:
results = [
p
for p in profile_store.values()
if q.lower() in p["username"].lower()
or q.lower() in p["bio"].lower()
]
else:
results = list(profile_store.values())

return {
"results": results[offset : offset + limit],
"total": len(results),
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.