FastAPI Bookstore Application
This is a FastAPI application for a bookstore management system, featuring user authentication, order management, and various endpoints for managing books, users, and carts.
- Installation
- Environment Variables
- Database Setup
- Running the Application
- API Documentation
- API Endpoints
- Contributing
- User Authentication: Supports login and logout using HTTP Basic Authentication and token-based authentication (JWT).
- User Management: Admins can view all user profiles and manage their orders.
- Book Management: Admins can add, update, and delete books in the inventory.
- Order Management: Users can place orders, view their orders, and cancel orders. Admins can manage order statuses.
- Cart Management: Users can add books to their cart and view their cart items.
Book store/
├─ app/
│ ├─ database/
│ │ ├─ __init__.py
│ │ ├─ db_connect.py
│ ├─ orders/
│ │ ├─ __init__.py
│ │ ├─ ordermanagement.py
│ ├─ schemas/
│ │ ├─ __init__.py
│ │ ├─ schemas.py
│ ├─ search/
│ │ ├─ __init__.py
│ │ ├─ searchcontroller.py
│ ├─ users/
│ │ ├─ __init__.py
│ │ ├─ user_routes.py
│ ├─ utlis/
│ │ ├─ __init__.py
│ │ ├─ password_utils.py
│ ├─ auth/
│ │ ├─ __init__.py
│ │ ├─ auth_routes.py
│ │ ├─ jwt_handler.py
│ ├─ books/
│ │ ├─ __init__.py
│ │ ├─ bookscontroller.py
│ ├─ cart/
│ │ ├─ __init__.py
│ │ ├─ cartcontroller.py
├─ main.py
├─ requirements.txt
├─ .gitignore
├─ __init__.py
- FastAPI: A modern web framework for building APIs with Python.
- MySQL: Database for storing user, book, and order information.
- Pydantic: Data validation and settings management using Python type annotations.
- Uvicorn: ASGI server for running the FastAPI application.
- Python 3.7 or later
- MySQL Server
- pip (Python package installer)
Create a .env file in the root directory and set the following environment variables:
DB_HOST=localhost
DB_USER=your_db_user
DB_PASSWORD=your_db_password
DB_NAME=your_db_name
SECRET_KEY=your_secert_key
This document provides instructions for setting up the database for the Online Bookstore application.
To create the database, run the following SQL command:
CREATE DATABASE IF NOT EXISTS onlinebookstore;After creating the database, you can create the necessary tables by executing the following SQL commands:
CREATE TABLE IF NOT EXISTS books (
barcode VARCHAR(100) PRIMARY KEY,
name VARCHAR(100),
author VARCHAR(100),
price INT,
quantity INT,
added_by VARCHAR(100)
);CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(100) UNIQUE,
password VARCHAR(255),
firstname VARCHAR(100),
lastname VARCHAR(100),
address VARCHAR(255),
phone VARCHAR(20),
mailid VARCHAR(100) UNIQUE,
usertype VARCHAR(50)
);curl --location 'localhost:8000/users/register' --header 'Content-Type: application/json' --data '{
"username": "Admin",
"password": "Admin!",
"firstname": "Admin",
"lastname": "Admin",
"address": "Admin",
"phone": "Admin",
"mailid": "Admin",
"usertype": "admin"
}
CREATE TABLE IF NOT EXISTS orders (
order_id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT,
barcode VARCHAR(100),
order_date DATETIME DEFAULT CURRENT_TIMESTAMP,
transaction_id VARCHAR(100) UNIQUE,
total_amount DECIMAL(10, 2),
status VARCHAR(50),
quantity INT DEFAULT 1,
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (barcode) REFERENCES books(barcode)
);CREATE TABLE IF NOT EXISTS cart (
cart_id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT,
barcode VARCHAR(100),
quantity INT DEFAULT 1,
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (barcode) REFERENCES books(barcode)
);Start the application using Uvicorn:
uvicorn main:app --reloadAccess the application at http://127.0.0.1:8000.
The API documentation is automatically generated by FastAPI and can be accessed at:
- Swagger UI: http://127.0.0.1:8000/docs
- ReDoc: http://127.0.0.1:8000/redoc
-
Login
- URL:
/auth/login - Method: POST
- Auth: Basic Auth
- Request Body: (in Authorization header)
- username
- password
- Response:
{ "message": "Login successful!", "Token": "Bearer {your_jwt_token}" } - URL:
-
Logout
- URL:
/auth/logout - Method: POST
- Auth: Bearer Token
- Response:
{ "message": "Successfully logged out" } - URL:
-
Get User Profile
- URL:
/users/profile - Method: GET
- Auth: Bearer Token
- Response:
{ "id": 1, "username": "user1", "usertype": "admin" } - URL:
- Add Book
- Update Book
- Delete Book
-
Place Order
- URL:
/order/order_book - Method: POST
- Request Body:
{ "barcode": "123456", "quantity": 1 }- Response:
{ "message": "Order placed successfully", "transaction_id": "uuid" } - URL:
-
View Orders
- URL:
/order/view_orders - Method: GET
- Auth: Bearer Token (Admin can view all orders)
- Response: List of orders with their details.
- URL:
-
View Cart
- URL:
/cart/view - Method: GET
- Auth: Bearer Token
- Response: List of cart items for the user.
- URL:
If you would like to contribute to this project, please fork the repository and create a pull request.