๐ŸŽ™ Free live masterclass ยท Sun, 29 Nov, 7:00 pm IST ยท Java Full Stack roadmap Register free โ†’
Skills101

E-commerce cart and orders API

The backend of an online shop: products, stock, cart, checkout and order history, with stock never going negative.

Advancedโฑ 16โ€“24 hours๐Ÿชœ 8 stepsSpring BootJPAPostgreSQLDocker

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. 1

    Model the domain

    Product(id, name, price, stock, @Version version), Cart, CartItem, Order, OrderItem (copy the price at checkout time).

  2. 2

    Product catalogue

    GET /api/products with filters (category, min/max price) and paging; admin-only create/update.

  3. 3

    Cart endpoints

    POST /api/cart/items, PATCH quantity, DELETE item; validate quantity does not exceed stock.

  4. 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. 5

    Handle conflicts

    Catch ObjectOptimisticLockingFailureException and return 409 "Stock changed, please retry".

  6. 6

    Order status

    PLACED โ†’ PAID โ†’ SHIPPED โ†’ DELIVERED, with an admin endpoint to move it forward.

  7. 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. 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