
- 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
144 lines
3.9 KiB
Markdown
144 lines
3.9 KiB
Markdown
# 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
|
|
|
|
1. Clone the repository:
|
|
|
|
```bash
|
|
git clone <repository-url>
|
|
cd onlinebookstorebackendapi
|
|
```
|
|
|
|
2. Install dependencies:
|
|
|
|
```bash
|
|
pip install -r requirements.txt
|
|
```
|
|
|
|
3. Apply database migrations:
|
|
|
|
```bash
|
|
alembic upgrade head
|
|
```
|
|
|
|
## Usage
|
|
|
|
### Starting the Server
|
|
|
|
Run the following command to start the development server:
|
|
|
|
```bash
|
|
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](http://localhost:8000/docs)
|
|
- ReDoc: [http://localhost:8000/redoc](http://localhost:8000/redoc)
|
|
|
|
## API Endpoints
|
|
|
|
### Health Check
|
|
|
|
- `GET /health`: Check API and database health
|
|
|
|
### Authentication
|
|
|
|
- `POST /api/users/register`: Register a new user
|
|
- `POST /api/users/login`: Login to get access token
|
|
|
|
### Users
|
|
|
|
- `GET /api/users/me`: Get current user info
|
|
- `PUT /api/users/me`: Update current user info
|
|
- `GET /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 filters
|
|
- `POST /api/books`: Create a new book (admin only)
|
|
- `GET /api/books/{book_id}`: Get book details
|
|
- `PUT /api/books/{book_id}`: Update book (admin only)
|
|
- `DELETE /api/books/{book_id}`: Delete book (admin only)
|
|
|
|
### Authors
|
|
|
|
- `GET /api/books/authors`: List authors
|
|
- `POST /api/books/authors`: Create a new author (admin only)
|
|
- `GET /api/books/authors/{author_id}`: Get author details
|
|
- `PUT /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 categories
|
|
- `POST /api/books/categories`: Create a new category (admin only)
|
|
- `GET /api/books/categories/{category_id}`: Get category details
|
|
- `PUT /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 order
|
|
- `GET /api/orders`: List current user's orders
|
|
- `GET /api/orders/admin`: List all orders (admin only)
|
|
- `GET /api/orders/{order_id}`: Get order details
|
|
- `PUT /api/orders/{order_id}`: Update order
|
|
- `DELETE /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:
|
|
|
|
1. Register a user or login to get an access token
|
|
2. 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:
|
|
|
|
```bash
|
|
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. |