FastAPI backend for a campus buy/sell marketplace with JWT auth, item listings, reservation flow, moderation, reports, ratings, and image upload support.
This repository currently contains backend code under backend/.
- Python 3.12+
- FastAPI
- SQLAlchemy (async)
- Alembic
- PostgreSQL (via asyncpg)
- JWT (
python-jose) - Password hashing (
passlib[bcrypt]) - Pytest + HTTPX
- Cloudinary (image uploads)
backend/
app/
api/v1/ # Route handlers
core/ # Config, security, permissions, constants
db/ # Engine/session/base/init
models/ # SQLAlchemy models
schemas/ # Pydantic request/response models
services/ # Business logic
tests/ # Async API tests
utils/ # Helpers (pagination, validators, image upload)
alembic/ # DB migrations
requirements.txt
alembic.ini
Create backend/.env with:
SECRET_KEY=your-secret-key
DATABASE_URL=postgresql+asyncpg://user:password@host:5432/dbname
DATABASE_URL_POOLER=postgresql+asyncpg://user:password@host:5432/dbname
DEBUG=True
CLOUDINARY_CLOUD_NAME=your-cloud-name
CLOUDINARY_API_KEY=your-api-key
CLOUDINARY_API_SECRET=your-api-secretNotes:
DATABASE_URLis used by Alembic migration config.DATABASE_URL_POOLERis used by the async app engine inapp/db/session.py.
From repository root:
cd backend
python -m venv .venv
# Windows
.venv\Scripts\activate
# macOS/Linux
source .venv/bin/activate
pip install -r requirements.txtcd backend
uvicorn app.main:app --reload --port 8000- Base URL:
http://127.0.0.1:8000 - OpenAPI docs:
http://127.0.0.1:8000/docs - Health endpoint:
GET /
Run migrations:
cd backend
alembic upgrade headCreate a migration:
cd backend
alembic revision --autogenerate -m "your_message"All routes are prefixed with /api/v1.
-
Auth
-
POST /auth/signup -
POST /auth/login -
Users
-
GET /users/me -
Items
-
POST /items/ -
GET /items/ -
POST /items/{item_id}/image -
GET /items/me- Get my listings -
GET /items/purchases- Get my purchases -
DELETE /items/{item_id}- Delete own item (seller or admin) -
Categories
-
GET /categories/ -
Reservations
-
POST /reservations/- Create reservation request -
GET /reservations/- List user's reservations (as buyer/seller) -
GET /reservations/{reservation_id}- Get specific reservation -
POST /reservations/{reservation_id}/accept- Seller accepts -
POST /reservations/{reservation_id}/reject- Seller rejects -
POST /reservations/{reservation_id}/cancel- Buyer/seller cancels -
POST /reservations/{reservation_id}/sold- Seller confirms sale -
Reports
-
POST /reports/ -
Ratings
-
POST /ratings/ -
Admin
-
POST /admin/block-user/{user_id} -
DELETE /admin/soft-delete-item/{item_id} -
DELETE /admin/hard-delete-item/{item_id}
Important: After making any changes to the codebase, please update the
ARCHITECTURE.mdfile to reflect the new architecture, endpoints, or logic. This ensures the documentation stays in sync with the implementation.
cd backend
pytest -qCurrent tests cover:
- signup/login
- create/list items
- reservation request/accept flow
Based on current implementation:
- Startup runs
init_db()which creates tables and seeds default categories when empty. - Reservation flow uses row locking (
FOR UPDATE) and item timeout logic (Item.is_actually_available). - Item image upload route exists and persists
image_url.
- Keep
.envinbackend/.env. - Do not commit local DB files, caches, or virtual environments.
- Keep model/schema/service changes aligned before generating migrations.