Search users by name
Python · FastAPI · intermediate · greenfield
Adds the user search endpoint: builds the LIKE query from the q parameter and returns matching users via the databases helper. Works against the local dataset.
The users table holds emails and password hashes. The endpoint is public, no auth.
Requirements
- Add `GET /users/search?q=` returning users whose name contains `q` (literal substring, case-insensitive).
- `q` comes straight from the query string (untrusted).
- Use the project's Encode `databases.Database` helper: `db.fetch_all(query, values)` with `:name` bind parameters.
Files touched
- app/routes/search.py
--- app/routes/search.py
+from fastapi import APIRouter
+from app.db import db
+
+router = APIRouter()
+
+
[email protected]("/users/search")
+async def search_users(q: str):
+ sql = f"SELECT id, name FROM users WHERE name LIKE '%{q}%'"
+ return await db.fetch_all(sql)