Skip to content

drizzy1772/LibraryAPI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BookStore API

A modern REST API for managing a bookstore with authors, books, orders, and user authentication built with FastAPI and SQLAlchemy.

Features

Tech Stack

  • FastAPI - Modern web framework
  • SQLAlchemy 2.0 - Async ORM
  • PostgreSQL - Primary database
  • Alembic - Database migrations
  • Pydantic - Data validation
  • JWT - Token-based authentication
  • pytest - Testing framework

Prerequisites

  • Python 3.11+
  • PostgreSQL
  • pip or poetry

Installation

  1. Clone the repository
git clone https://github.com/drizzy1772/libraryAPI-api.git
cd bookstore-api
  1. Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install dependencies
pip install -r requirements.txt
  1. Setup environment variables
cp .env.example .env

Edit .env with your configuration:

DATABASE_URL=postgresql+asyncpg://postgres:postgres@localhost:5432/bookstore
SECRET_KEY=your-secret-key-here
DEBUG=False
ACCESS_TOKEN_EXPIRE_MINUTES=40
  1. Initialize database
# Run migrations
alembic upgrade head
  1. Start the server
uvicorn app.main:app --reload

API Documentation

Once the server is running, visit:

Authentication

Register a new user

POST /auth/register
{
  "username": "john_doe",
  "email": "john@example.com",
  "password": "secure_password"
}

Login

POST /auth/login
{
  "username": "john_doe",
  "password": "secure_password"
}

Response:

{
  "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "token_type": "bearer"
}

Use the token in subsequent requests:

Authorization: Bearer <your_token>

API Endpoints

Authors

Method Endpoint Description Auth Required
GET /authors List all authors No
GET /authors/{id} Get author details with books No
POST /authors Create new author Admin only

Books

Method Endpoint Description Auth Required
GET /books List all books (paginated) No
GET /books/{id} Get book details No
POST /books Create new book Admin only
PATCH /books/{id} Update book Admin only
DELETE /books/{id} Delete book Admin only

Orders

Method Endpoint Description Auth Required
POST /orders Place an order User

Testing

Run the test suite:

pytest

Run with coverage:

pytest --cov=app tests/

Run specific test file:

pytest tests/test_books.py

Project Structure

bookstore-api/
├── alembic/                 # Database migrations
│   ├── versions/           # Migration files
│   └── env.py
├── app/
│   ├── routers/            # API routes
│   │   ├── auth.py
│   │   ├── authors.py
│   │   ├── books.py
│   │   └── orders.py
│   ├── config.py           # Configuration settings
│   ├── crud.py             # Database operations
│   ├── database.py         # Database connection
│   ├── dependencies.py     # FastAPI dependencies
│   ├── main.py             # Application entry point
│   ├── models.py           # SQLAlchemy models
│   ├── schemas.py          # Pydantic schemas
│   └── security.py         # Authentication utilities
├── tests/                  # Test suite
│   ├── conftest.py
│   ├── test_auth.py
│   ├── test_authors.py
│   ├── test_books.py
│   └── test_orders.py
├── .env.example
├── alembic.ini
├── requirements.txt
└── README.md

Configuration

Key settings in app/config.py:

  • DATABASE_URL - PostgreSQL connection string
  • SECRET_KEY - JWT signing key
  • ACCESS_TOKEN_EXPIRE_MINUTES - Token expiration time
  • DEFAULT_PAGE_SIZE - Default pagination size
  • MAX_PAGE_SIZE - Maximum items per page

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors