This API is based on FastAPI for the routes part and on SQLAlchemy 2.0 for the ORM part.
The logical choice would have been to go with Django and Django Rest Framework as DRF provides most of the boilerplate code for routing and Django already provides a permissions management engine in its ORM.
The choice I made for a FastAPI/SQLA combo is purely based on my will to discover a framework (FastAPI) which I haven't worked with before.
- Creation
- Read
- Update
- Deletion
- Creation
- Read
- Update
- Deletion
- Creation (lend a book)
- Read
- Update (manage lendings with some logic to prevent nonsensical updates)
All of the read & updates operations on lending are behind a JWT based authentication.
As of today this code is absolutely not production ready:
- There is no proper role management in the API, this means anyone can add, edit or delete books & authors
- In my opinion this should be done through a RBAC (Role-Based Access Control) based authentication system. Roles could be "Customer", "Librarian", "Administrator", etc.
- There is a serious threat about race conditions when two (or more) users will try to reserve a book at the same time
- The usage of a
SELECT [...] FROM [...] FOR UPDATE
with Postgres & MariaDB would mitigate the problem by locking the book row
- The usage of a
- The lending management is far from complete and needs way more work to be fully usable in production
- The data model could be reworked to be more in touch with already live lending systems
- The Bibliothèque Nationale de France offers a comprehensive view of their software here: https://www.bnf.fr/fr/modeles-frbr-frad-et-frsad
This API was written as OS agnostic but beware that it was only written and tested under Ubuntu 23.04 and Ubuntu within a WSL environment on Windows.
For both systems the python version is 3.12.
- Virtualenv should be available on your system, on debian-based systems (Debian, Ubuntu, etc.) you can run
sudo apt install python3-venv
if it is not the case. - Create a virtualenv:
python -m venv library
- Enter it:
source library/env/bin/activate
- Install dependencies:
pip install -r requirements.txt
If poetry is available on your machine you can run poetry install
, it will create a virtualenv and install the dependencies.
A Dockerfile
is provided at the root of this project, with compose you can build the images: docker compose build
- First you need to create a user & database with MariaDB or Postgres
- Expose the database URI through the environment:
export SQLALCHEMY_DATABASE_URI="[...]"
(replace the dots with the correct connection string) - Create the database schema:
PYTHONPATH=. python src/bootstrap_database_schema.py
- Start the API:
PYTHONPATH=. uvicorn src.api.main:app
Just run docker compose up api
, this command will:
- Start a PostgreSQL container
- Wait until the postgres container is marked as healthy
- Start the API container
- Create the missing database tables
- Start the API