An open API service indexing awesome lists of open source software.

https://github.com/bharathbk07/book-store

This is a FastAPI application for a bookstore management system, featuring user authentication, order management, and various endpoints for managing books, users, and carts.
https://github.com/bharathbk07/book-store

fast-api mysql python

Last synced: 9 months ago
JSON representation

This is a FastAPI application for a bookstore management system, featuring user authentication, order management, and various endpoints for managing books, users, and carts.

Awesome Lists containing this project

README

          

# Book-Store

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.

## Table of Contents

- [Installation](#installation)
- [Environment Variables](#environment-variables)
- [Database Setup](#database-setup)
- [Running the Application](#running-the-application)
- [API Documentation](#api-documentation)
- [API Endpoints](#api-endpoints)
- [Authentication](#authentication)
- [User Management](#user-management)
- [Book Management](#book-management)
- [Order Management](#order-management)
- [Cart Management](#cart-management)
- [Contributing](#contributing)

## Features

- **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.

## Folder Structure

```
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

```

## Technologies Used

- **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.

## Setup and Installation

### Prerequisites

- Python 3.7 or later
- MySQL Server
- pip (Python package installer)

## Environment Variables

Create a `.env` file in the root directory and set the following environment variables:

```plaintext
DB_HOST=localhost
DB_USER=your_db_user
DB_PASSWORD=your_db_password
DB_NAME=your_db_name
SECRET_KEY=your_secert_key
```

# Online Bookstore Database Setup

This document provides instructions for setting up the database for the Online Bookstore application.

## Creating the Database

To create the database, run the following SQL command:

```sql
CREATE DATABASE IF NOT EXISTS onlinebookstore;
```

# Creating Tables

After creating the database, you can create the necessary tables by executing the following SQL commands:

## Create Books Table
```sql
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 Users Table
```sql
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)
);
```

Use Below api to create admin user

```curl
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 Orders table

```sql
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 Cart table

```sql
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)
);
```

## Running the Application

Start the application using Uvicorn:

```bash
uvicorn main:app --reload
```

Access the application at [http://127.0.0.1:8000](http://127.0.0.1:8000).

## API Documentation

The API documentation is automatically generated by FastAPI and can be accessed at:

- Swagger UI: [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs)
- ReDoc: [http://127.0.0.1:8000/redoc](http://127.0.0.1:8000/redoc)

## API Endpoints

### Authentication

- **Login**

- **URL:** `/auth/login`
- **Method:** POST
- **Auth:** Basic Auth
- **Request Body:** (in Authorization header)
- username
- password
- **Response:**

```json
{
"message": "Login successful!",
"Token": "Bearer {your_jwt_token}"
}
```

- **Logout**

- **URL:** `/auth/logout`
- **Method:** POST
- **Auth:** Bearer Token
- **Response:**

```json
{
"message": "Successfully logged out"
}
```

### User Management

- **Get User Profile**

- **URL:** `/users/profile`
- **Method:** GET
- **Auth:** Bearer Token
- **Response:**

```json
{
"id": 1,
"username": "user1",
"usertype": "admin"
}
```

### Book Management

- **Add Book**
- **Update Book**
- **Delete Book**

### Order Management

- **Place Order**

- **URL:** `/order/order_book`
- **Method:** POST
- **Request Body:**

```json
{
"barcode": "123456",
"quantity": 1
}
```

- **Response:**

```json
{
"message": "Order placed successfully",
"transaction_id": "uuid"
}
```

- **View Orders**

- **URL:** `/order/view_orders`
- **Method:** GET
- **Auth:** Bearer Token (Admin can view all orders)
- **Response:** List of orders with their details.

### Cart Management

- **View Cart**

- **URL:** `/cart/view`
- **Method:** GET
- **Auth:** Bearer Token
- **Response:** List of cart items for the user.

## Contributing

If you would like to contribute to this project, please fork the repository and create a pull request.