Skip to content

Repository files navigation

My Library API

REST API for library book management using FastAPI and PostgreSQL.

Features

  • CRUD operations for books
  • Search by title or author
  • Pagination (1-10 books per page)
  • API Key authentication
  • Docker support
  • PostgreSQL integration
  • Pytest tests

Improvements and new features (v0.2.0)

  • User management using JWT token authorization and argon2
  • Borrow book logic system

Done

  • login endpoints
  • token creation JWT
  • password hashin (argon2)
  • install dependencies
  • apply current_user to borrow router

Setup

Create .env file:

DATABASE_URL = "postgresql://postgres:postgres@db:5432/library_db"
API_KEY = "test_api_key"
VERSION = "v0.2.0"
JWT = "test_jwt_key"

Run:

docker compose up --build

This will also run the tests.

API runs at:

http://localhost:8000

Swagger docs:

http://localhost:8000/docs

Authentication

Add header:

X-API-Key: test_api_key

Run tests

docker compose run --rm test 

Example requests for books

Create book example:

curl -X 'POST' \
  'http://localhost:8000/books/' \
  -H 'accept: application/json' \
  -H 'x-api-key: test_api_key' \
  -H 'Content-Type: application/json' \
  -d '{
    "title": "Σπουδή Στο Κόκκινο",
    "author": "Conan Doyle",
    "isbn": "123456789",
    "publication_year": 1887
}'

Get books example:

curl -X 'GET' \
  'http://localhost:8000/books/?page=1&size=10' \
  -H 'accept: application/json' \
  -H 'x-api-key: test_api_key'

Search books by title exapmle: requirement: a book's title contains = "Σπουδ"

curl -X 'GET' \
  'http://localhost:8000/books/?page=1&size=10&search=%CE%A3%CF%80%CE%BF%CF%85%CE%B4' \
  -H 'accept: application/json' \
  -H 'x-api-key: test_api_key'

Get book by id example: requirement: a book exists with id=2

curl -X 'GET' \
  'http://localhost:8000/books/2' \
  -H 'accept: application/json' \
  -H 'x-api-key: test_api_key'

Example requests for users

Create user example:

curl -X 'POST' \
  'http://localhost:8000/user/' \
  -H 'accept: application/json' \
  -H 'x-api-key: test_api_key' \
  -H 'Content-Type: application/json' \
  -d '{
  "username": "Dimitris Kavvadas",
  "email": "example@email.com",
  "password": "example_password"
}'

Get users example:

curl -X 'GET' \
  'http://localhost:8000/user/?page=1&size=10' \
  -H 'accept: application/json' \
  -H 'x-api-key: test_api_key'

Get user by id example: requirement: a user exists with id=1

curl -X 'GET' \
  'http://localhost:8000/user/1' \
  -H 'accept: application/json' \
  -H 'x-api-key: test_api_key'

Get users by role example(user):

curl -X 'GET' \
  'http://localhost:8000/user/role/user' \
  -H 'accept: application/json' \
  -H 'x-api-key: test_api_key'

Example requests for borrows

Borrow book example: requirements:

  • a user exists with id=1
  • a book exists with id=2
curl -X 'POST' \
  'http://localhost:8000/borrow/2?user_id=1' \
  -H 'accept: application/json' \
  -H 'x-api-key: test_api_key' \
  -d ''

Return a book example: requirements: a borrowing record exists with id=1

curl -X 'POST' \
  'http://localhost:8000/borrow/1/return' \
  -H 'accept: application/json' \
  -H 'x-api-key: test_api_key' \
  -d ''

Get borrowing records:

curl -X 'GET' \
  'http://localhost:8000/borrow/' \
  -H 'accept: application/json' \
  -H 'x-api-key: test_api_key'

Get borrowing records by user_id: requiremenets: a user exists with id=1

curl -X 'GET' \
  'http://localhost:8000/borrow/user/1' \
  -H 'accept: application/json' \
  -H 'x-api-key: test_api_key'

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages