
- Set up FastAPI project structure with SQLite and SQLAlchemy - Create models for users, books, authors, categories, and orders - Implement JWT authentication and authorization - Add CRUD endpoints for all resources - Set up Alembic for database migrations - Add health check endpoint - Add proper error handling and validation - Create comprehensive documentation
Online Bookstore API
This is a FastAPI backend for an online bookstore application. It provides endpoints for managing books, authors, categories, users, and orders.
Features
- User registration and authentication with JWT tokens
- Book management with support for authors and categories
- Order processing with inventory management
- Role-based access control (admins and regular users)
- Comprehensive API documentation with Swagger UI and ReDoc
- SQLite database with SQLAlchemy ORM
- Database migrations with Alembic
Installation
Prerequisites
- Python 3.8+
- pip
Setup
- Clone the repository:
git clone <repository-url>
cd onlinebookstorebackendapi
- Install dependencies:
pip install -r requirements.txt
- Apply database migrations:
alembic upgrade head
Usage
Starting the Server
Run the following command to start the development server:
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
API Documentation
Once the server is running, you can access the API documentation at:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
API Endpoints
Health Check
GET /health
: Check API and database health
Authentication
POST /api/users/register
: Register a new userPOST /api/users/login
: Login to get access token
Users
GET /api/users/me
: Get current user infoPUT /api/users/me
: Update current user infoGET /api/users/{user_id}
: Get user by ID (admin only)PUT /api/users/{user_id}
: Update user (admin only)DELETE /api/users/{user_id}
: Delete user (admin only)
Books
GET /api/books
: List books with optional filtersPOST /api/books
: Create a new book (admin only)GET /api/books/{book_id}
: Get book detailsPUT /api/books/{book_id}
: Update book (admin only)DELETE /api/books/{book_id}
: Delete book (admin only)
Authors
GET /api/books/authors
: List authorsPOST /api/books/authors
: Create a new author (admin only)GET /api/books/authors/{author_id}
: Get author detailsPUT /api/books/authors/{author_id}
: Update author (admin only)DELETE /api/books/authors/{author_id}
: Delete author (admin only)
Categories
GET /api/books/categories
: List categoriesPOST /api/books/categories
: Create a new category (admin only)GET /api/books/categories/{category_id}
: Get category detailsPUT /api/books/categories/{category_id}
: Update category (admin only)DELETE /api/books/categories/{category_id}
: Delete category (admin only)
Orders
POST /api/orders
: Create a new orderGET /api/orders
: List current user's ordersGET /api/orders/admin
: List all orders (admin only)GET /api/orders/{order_id}
: Get order detailsPUT /api/orders/{order_id}
: Update orderDELETE /api/orders/{order_id}
: Cancel order
Database Schema
The application uses the following database models:
- User: User account information
- Book: Book details including stock quantity
- Author: Author information
- Category: Book categories
- Order: Order information including status and shipping address
- OrderItem: Individual items in an order with quantity and price
Authentication and Authorization
The API uses JWT tokens for authentication. To access protected endpoints:
- Register a user or login to get an access token
- Include the token in the Authorization header of subsequent requests:
Authorization: Bearer {your_token}
Development
Database Migrations
To create a new migration after modifying models:
alembic revision --autogenerate -m "Description of changes"
alembic upgrade head
Adding Admin Users
To add an admin user, you can use the API to create a user and then update the is_admin
field in the database manually, or create a script to do this.
Description
Languages
Python
99.1%
Mako
0.9%