Automated Action f1c2b73ade Implement online bookstore backend API
- 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
2025-05-20 12:04:27 +00:00
2025-05-20 11:49:59 +00:00
2025-05-20 12:04:27 +00:00

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:
git clone <repository-url>
cd onlinebookstorebackendapi
  1. Install dependencies:
pip install -r requirements.txt
  1. 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:

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:

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
Project: Online Bookstore Backend API
Readme 51 KiB
Languages
Python 99.1%
Mako 0.9%