diff --git a/README.md b/README.md index 64ca216..ffd3909 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/app/main.py b/app/main.py index bf69788..498ec80 100644 --- a/app/main.py +++ b/app/main.py @@ -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, @@ -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] = { @@ -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: @@ -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)} diff --git a/app/models.py b/app/models.py index 9043fb4..69c0afa 100644 --- a/app/models.py +++ b/app/models.py @@ -1,6 +1,6 @@ """Data models for the Workshop API.""" -from pydantic import BaseModel +from pydantic import BaseModel, Field from typing import Optional @@ -8,13 +8,14 @@ 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