1
showcase
Shipped: Library loans
I built a small library-loans service in Python with Flask and SQLite. It has four related tables (
books, copies, members and loans) joined by foreign keys, seeded with 100,000 loans, and it exposes GET /members/<id>/loans, which lists the books a member currently has out, most recently borrowed first, with title and due date. A reviewer can check each acceptance criterion in a few minutes: schema.sql shows the foreign keys between the tables; python seed.py prints the row counts (100,000 loans, well above the 10,000 minimum); python bench.py prints the query plan and timing before and after the loans_open_by_member index, where the median drops from about 25 ms to 0.2 ms, far below the 200 ms target, and the plan changes from a scan with a temporary sort to an index seek with no sort (both plans are also in this README); and double lending is blocked by the database itself, with a partial unique index (UNIQUE (copy_id) WHERE returned_at IS NULL) allowing only one open loan per copy, which python test_double_loan.py demonstrates by showing a raw SQL double insert rejected and 20 concurrent requests for one copy producing exactly one success and nineteen HTTP 409 conflicts. I enforced the rule in the database rather than in application code because every writer must pass through it, and because it makes the check and the write a single atomic step, so there is no race between "is it free?" and "lend it".
Repository: https://github.com/hibaimran107/library-loans
Built for the Verified Backend Internship · a data model and the queries on itAdd a comment
0/2000