A simple asynchronous REST API for managing books and reviews, built using FastAPI, PostgreSQL, SQLAlchemy, and Redis. Ideal for learning how to implement clean backend architecture, API design, caching, and testing.
CRUD operations for Books
Add and retrieve Book Reviews
Redis-based caching for improved performance
Pytest-based testing
SQLAlchemy ORM with Alembic migrations
Backend: FastAPI, Python 3.13+
Database: PostgreSQL / SQLite (dev)
ORM: SQLAlchemy
Caching: Redis
Testing: Pytest, HTTPX
Migrations: Alembic
book-review-service/ ├── alembic/ # Alembic migrations │ ├── versions/ # Auto-generated migration files │ ├── env.py # Alembic environment configuration │ └── script.py.mako # Alembic script template │ ├── app/ # Main application package │ ├── routes/ # FastAPI route handlers │ │ ├── books.py │ │ ├── reviews.py │ │ ├── clear_cache.py │ │ │ ├── cache.py # Redis caching logic │ ├── database.py # SQLAlchemy or MySQL DB engine and session │ ├── main.py # FastAPI app entry point │ ├── models.py # SQLAlchemy models │ └── schemas.py # Pydantic schemas │ ├── tests/ # Unit and integration tests │ ├── test_books.py │ ├── test_reviews.py │ ├── test_cache.py │ ├── conftest.py # Fixtures and test setup │ ├── alembic.ini # Alembic configuration file ├── requirements.txt # Python dependencies └── README.md # Project documentation
1.Create Virtual Environment python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate or Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned .\venv\Scripts\Activate.ps1
2.Install Dependencies
pip install mysql-connector-python
pip install -r requirements.txt
3.Configure Environment (Optional)
Create .env if you're using environment variables:
DATABASE_URL=sqlite:///./books.db
DATABASE_URL = "mysql+pymysql://username:password@localhost:3306/databse"
4.Run Alembic Migrations alembic revision --autogenerate -m "Add rating column to books" alembic upgrade head
5.Start the Server
uvicorn app.main:app --reload
Visit: http://127.0.0.1:8000/docs
pytest or $env:PYTHONPATH="." ; pytest
Make sure Redis is running locally before running cache tests.
| Method | Endpoint | Description |
|---|---|---|
| GET | /books |
List all books |
| POST | /books |
Add a new book |
| GET | /books/{id}/reviews |
Get reviews for a book |
| POST | /books/{id}/reviews |
Add a review for a book |
| GET | /books/all |
List all reviews book |
| POST | /admin/clear-cache |
Clear the cache |
open the website and past the for interactive with API Docs:http://localhost:8000/docs
curl -X POST http://127.0.0.1:8000/books/
application/json:
Add a new book
{
"title":"Atomic Habits",
"author": "James Clear,
"rating": 4.1
}
Add a review for a book
{
"content": "A brutally honest and humorous take on living a meaningful life by embracing limitations and choosing what truly matters Refreshing and thought-provoking."
}