REST API for library book management using FastAPI and PostgreSQL.
- CRUD operations for books
- Search by title or author
- Pagination (1-10 books per page)
- API Key authentication
- Docker support
- PostgreSQL integration
- Pytest tests
- User management using JWT token authorization and argon2
- Borrow book logic system
- login endpoints
- token creation JWT
- password hashin (argon2)
- install dependencies
- apply current_user to borrow router
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 --buildThis will also run the tests.
API runs at:
http://localhost:8000Swagger docs:
http://localhost:8000/docsAdd header:
X-API-Key: test_api_keydocker compose run --rm test 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'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'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'