E-commerce cart and orders API
The backend of an online shop: products, stock, cart, checkout and order history, with stock never going negative.
What you'll learn
- โModelling a real domain
- โTransactions and optimistic locking
- โDocker compose for app + database
What you'll build
- โProducts with categories and search
- โCart per user (add, update qty, remove)
- โCheckout creates an order and reduces stock atomically
- โOrder history and status updates
Step-by-step guide
- 1
Model the domain
Product(id, name, price, stock, @Version version), Cart, CartItem, Order, OrderItem (copy the price at checkout time).
- 2
Product catalogue
GET /api/products with filters (category, min/max price) and paging; admin-only create/update.
- 3
Cart endpoints
POST /api/cart/items, PATCH quantity, DELETE item; validate quantity does not exceed stock.
- 4
Checkout transaction
@Transactional checkout(): re-read products, check stock, reduce it, create the order, clear the cart. The @Version field makes concurrent checkouts safe (optimistic locking).
- 5
Handle conflicts
Catch ObjectOptimisticLockingFailureException and return 409 "Stock changed, please retry".
- 6
Order status
PLACED โ PAID โ SHIPPED โ DELIVERED, with an admin endpoint to move it forward.
- 7
Run with Docker
Write a Dockerfile (eclipse-temurin:21-jre) and docker-compose.yml with the app and postgres:16; start with docker compose up.
- 8
Integration tests
Use Testcontainers to run tests against a real PostgreSQL.
๐ Make it stand out
- +Payment gateway in test mode (Razorpay/Stripe test keys)
- +Coupons and discounts
- +Send order emails