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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# FastAPI Worksohp
# FastAPI Workshop

A tiny FastAPI application built for practicing open-source contributions.
This project is designed for third-year CS students participating in
Expand Down
33 changes: 17 additions & 16 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@

from app.models import ProfileCreate, ProfileResponse
from app.store import profile_store
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,26 +17,28 @@
)


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


@app.get("/sum")
def compute_sum(a: int = Query(...), b: int = Query(...)):
return {"result": a * b}
"""Compute the sum of two integers."""
return {"result": a + b}


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


@app.post("/profile", status_code=201)
@app.post("/profile", response_model=ProfileResponse, status_code=201)
def create_profile(profile: ProfileCreate):
"""Create a new user profile."""
profile_store[profile.username] = {
Expand All @@ -48,7 +49,7 @@ def create_profile(profile: ProfileCreate):
return format_profile(profile_store[profile.username])


@app.get("/profile/{username}")
@app.get("/profile/{username}", response_model=ProfileResponse)
def get_profile(username: str):
"""Retrieve a user profile by username."""
if username not in profile_store:
Expand All @@ -73,11 +74,11 @@ def search_profiles(
):
"""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)}
results = list(profile_store.values())
else:
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], "total": len(results)}
5 changes: 3 additions & 2 deletions app/models.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
"""Data models for the Workshop API."""

from pydantic import BaseModel
from pydantic import BaseModel, Field
from typing import Optional


class ProfileCreate(BaseModel):
"""Schema for creating a new profile."""

username: str
bio: str
bio: str = Field(..., max_length=500)
age: Optional[int] = None


class ProfileResponse(BaseModel):
"""Schema for profile responses."""

name: str
username: str
bio: str
age: Optional[int] = None