Bookstore-FAST-APIs-Backend

Bookstore API Backend with FastAPI

This project demonstrates a simple but comprehensive backend API for a Bookstore application, built using FastAPI, a modern, fast (high-performance) web framework for building APIs with Python. The API provides features for managing books and authors, including full CRUD operations, JWT-based authentication, database integration using SQLAlchemy, and pagination. It also includes automated seeding of the database with fake data for testing purposes.

Table of Contents

  1. Introduction
  2. Features
  3. Installation
  4. Usage
  5. Project Structure
  6. API Endpoints
  7. Examples
  8. Contributing
  9. License

Introduction

The Bookstore API provides a secure and scalable backend service for managing books and authors in a bookstore. It demonstrates key backend development concepts using FastAPI, such as asynchronous endpoints, JWT authentication, and database integration with SQLAlchemy. The API is designed to be easily extended and deployed, making it a great starting point for more complex applications.

Features

Installation

Prerequisites

Install Required Packages

  1. Clone the Repository:

    Clone the repository to your local machine:

    git clone https://github.com/your-username/bookstore-api-fastapi.git
    
  2. Navigate to the Directory:

    Go to the project directory:

    cd bookstore-api-fastapi
    
  3. Install Dependencies:

    Install the required packages using pip:

    pip install -r requirements.txt
    

Usage

  1. Run the Application:

    Start the FastAPI application using Uvicorn:

    uvicorn main:app --reload
    

    The server will start at http://127.0.0.1:8000/.

  2. Access API Documentation:

    FastAPI provides interactive API documentation at:

Project Structure

bookstore/
│
├── main.py                # Entry point of the application
├── models.py              # SQLAlchemy models representing the database schema
├── schemas.py             # Pydantic models for request and response validation
├── crud.py                # CRUD operations for interacting with the database
├── auth.py                # Authentication logic (JWT token creation and verification)
├── database.py            # Database setup and session management
├── config.py              # Configuration settings (e.g., secrets, JWT settings)
├── seed_data.py           # Script for seeding the database with fake data
└── requirements.txt       # List of required Python packages

API Endpoints

1. User Registration

2. User Authentication

3. Create a New Book

4. Get All Books

5. Create a New Author

6. Get All Authors

Examples

Using cURL

  1. Register a New User:

    curl -X POST -H "Content-Type: application/json" -d '{"username": "user1", "password": "yourpassword"}' http://127.0.0.1:8000/users/
    
  2. Authenticate and Get Token:

    curl -X POST -d "username=user1&password=yourpassword" -H "Content-Type: application/x-www-form-urlencoded" http://127.0.0.1:8000/token
    
  3. Create a New Book:

    curl -X POST -H "Authorization: Bearer <your-token>" -H "Content-Type: application/json" -d '{"title": "The Great Gatsby", "description": "A novel by F. Scott Fitzgerald", "author_id": 1}' http://127.0.0.1:8000/books/
    
  4. Get All Books:

    curl -X GET http://127.0.0.1:8000/books/
    

Contributing

Contributions are welcome! If you have ideas for new features, improvements, or bug fixes, please feel free to open an issue or create a pull request.

Steps to Contribute

  1. Fork the Repository: Click the ‘Fork’ button at the top right of this page.
  2. Clone Your Fork: Clone your forked repository to your local machine.
    git clone https://github.com/your-username/bookstore-api-fastapi.git
    
  3. Create a Branch: Create a new branch for your feature or bug fix.
    git checkout -b feature/your-feature-name
    
  4. Make Changes: Make your changes and commit them with a descriptive message.
    git commit -m "Add: feature description"
    
  5. Push Changes: Push your changes to your forked repository.
    git push origin feature/your-feature-name
    
  6. Create a Pull Request: Go to the original repository on GitHub and create a pull request.

License

This project is licensed under the MIT License - see the LICENSE file for details.


Thank you for using the Bookstore API Backend with FastAPI! If you have any questions or feedback, feel free to reach out to me. Happy coding! 📚🚀