Skip to content

Lotline

A full-stack car dealership inventory app, built test-first. Buying a car is one atomic SQL statement, so two buyers can never take the last unit.

backend tests, 98% coverage
135
frontend tests
79
concurrent buyers for 5 cars; exactly 5 succeed
25 → 5
  • Python
  • FastAPI
  • SQLAlchemy 2
  • PostgreSQL
  • JWT
  • React
  • Vite
  • Tailwind
  • pytest
  • Vitest
  • Docker
  • GitHub Actions
The Lotline admin dashboard: search filters above vehicle cards with prices, stock bars and purchase buttons.

The problem

Inventory apps often read the stock, check it, then write. Under load, two buyers can both see one car left and both buy it.

Pipeline

  1. React app
  2. JWT check
  3. Route
  4. Service rules
  5. Conditional UPDATE
  6. Purchase row, same transaction
Routes only validate; business rules live in services; all SQL lives in repositories. The highlighted stage is where the key decision sits.

How it works

Three backend layers: thin routes that validate and authorise, services that hold the business rules, and repositories, the only code that talks to the database.

Every behaviour was built test-first: a failing test commit followed by the commit that makes it pass.

Roles are read from the database on every request rather than trusted from the token. Admin accounts exist only through the seed script.

A purchase snapshots make, model and price, so a buyer's history survives the vehicle being edited or deleted.

The decision

I chose
Make a purchase one UPDATE … SET quantity = quantity - 1 WHERE quantity > 0, in the same transaction as the purchase row.
Instead of
Reading the stock, checking it in Python, then writing.
Because
The database enforces the rule, so no application lock is needed. I temporarily swapped in the naive read-then-write version to confirm the concurrency test fails against it, and it does.

What broke, and the fix

  1. A final audit found tokens without an expiry claim being accepted, passwords over bcrypt's 72-byte limit, an integer overflow on PostgreSQL and a timing difference in login.

    Each one got a failing test first, then a fix.

What I would do next

  • Add Alembic migrations before the schema changes on a deployed database.
  • Deploy it, so there is a live demo to try.