Skip to content

Tran-Nguyen-26/ecommerce-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ›’ E-Commerce Backend

A production-ready RESTful API for an e-commerce platform built with Spring Boot 3, following Modular Monolith architecture with Port & Adapter pattern β€” designed for easy migration to microservices.


πŸ“‹ Table of Contents


πŸ›  Tech Stack

Category Technology
Framework Spring Boot 3.5, Spring Security, Spring Data JPA
Language Java 21
Database PostgreSQL 16
Cache Redis 7
Authentication JWT (Access + Refresh Token)
Payment VNPay
Image Upload Cloudinary
Email JavaMailSender + MailHog (dev)
Real-time Server-Sent Events (SSE)
Documentation Swagger / OpenAPI 3
Testing JUnit 5, Testcontainers, k6
Build Maven

πŸ— Architecture

Modular Monolith + Port & Adapter

The application is structured as a Modular Monolith β€” each business domain is an independent module with clear boundaries, communicating through defined interfaces (Ports).

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                  API Layer                  β”‚
β”‚         (Controllers + Swagger)             β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  User    β”‚  Catalog β”‚   Order   β”‚  Payment  β”‚
β”‚  Module  β”‚  Module  β”‚   Module  β”‚  Module   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚              Port & Adapter                 β”‚
β”‚    (Inter-module communication via          β”‚
β”‚     interfaces, not direct calls)           β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚           Spring Event Bus                  β”‚
β”‚   (Async: OrderCreated β†’ ReserveStock)      β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚   PostgreSQL     β”‚    Redis Cache            β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Key Design Decisions

  • Port & Adapter β€” modules communicate through interfaces, never direct class calls
  • Spring Events β€” async domain events replace tight coupling (e.g., OrderCreatedEvent β†’ Inventory reserves stock)
  • Optimistic Locking β€” prevents overselling without pessimistic locks
  • Idempotency Keys β€” duplicate order requests are safely ignored
  • Snapshot Pattern β€” order items store price/name at time of purchase

Why Modular Monolith?

Each module follows the same internal structure as a microservice:

Handler (Event) β†’ Service β†’ Port Interface
Controller (HTTP) β†’ Service β†’ Port Interface

Migrating to microservices only requires:

  • Replacing @EventListener with @KafkaListener
  • Replacing Port JPA implementations with Feign Clients
  • No changes to Service or Handler layer

πŸ“¦ Modules

Module Responsibility
user Auth (JWT), profile, address, wishlist
catalog Products, categories, variants, image upload
inventory Stock management, optimistic locking
order Cart, order lifecycle, vouchers
payment COD & VNPay integration
review Product reviews & ratings
notification Real-time SSE + email notifications

✨ Features

Authentication & User

  • Register / Login with JWT (Access + Refresh Token)
  • Refresh token rotation β€” old token revoked on refresh
  • Logout β€” revokes refresh token
  • Change password β€” revokes all sessions
  • Forgot password β€” email with reset link (15-min expiry via Redis)
  • Reset password
  • User profile management
  • Multiple shipping addresses

Catalog

  • Product listing with filtering & pagination
  • Category management
  • Product variants (size, color, etc.)
  • Image upload via Cloudinary

Inventory

  • Real-time stock tracking
  • Optimistic Locking β€” concurrent purchase protection
  • Reserve β†’ Commit / Release flow

Order & Cart

  • Persistent cart (1 cart per user)
  • Place order from cart
  • Voucher / discount codes
  • Dynamic shipping fee calculation
  • Order status history (full audit trail)
  • Order cancellation
  • Auto-cancel on payment timeout (15 minutes)

Payment

  • COD β€” auto-confirms order immediately
  • VNPay β€” redirect flow with callback verification
  • Idempotency β€” duplicate payments safely rejected
  • Payment event audit log

Notification

  • Real-time SSE β€” instant order status push to browser
  • Email β€” order status change notifications
  • MailHog for local email testing

Admin

  • User management (ban/unban)
  • Product & category CRUD
  • Inventory management
  • Order management & status updates
  • Voucher management
  • Sales dashboard

πŸš€ Getting Started

Prerequisites

  • Java 21+
  • Maven 3.9+
  • Docker & Docker Compose

1. Clone & Start Infrastructure

git clone https://github.com/Tran-Nguyen-26/ecommerce.git
cd ecommerce

# Start PostgreSQL, Redis, MailHog
docker-compose up -d

2. Configure Environment

Copy and update environment variables (or use defaults for local dev):

# application.yml defaults work out of the box for local dev
# PostgreSQL: localhost:5433
# Redis:      localhost:6380
# MailHog:    localhost:1025 (SMTP), localhost:8025 (UI)

3. Run Application

./mvnw spring-boot:run

4. Verify

Service URL
API http://localhost:8080
Swagger UI http://localhost:8080/swagger-ui.html
MailHog UI http://localhost:8025
Health Check http://localhost:8080/actuator/health

πŸ“– API Documentation

Full API docs available at http://localhost:8080/swagger-ui.html

πŸ“ Project Structure

src/main/java/com/ttn/ecommerce/
β”œβ”€β”€ infrastructure/
β”‚   β”œβ”€β”€ config/          # Security, CORS, Swagger
β”‚   └── email/           # EmailService
β”œβ”€β”€ modules/
β”‚   β”œβ”€β”€ catalog/
β”‚   β”‚   β”œβ”€β”€ api/         # Controllers
β”‚   β”‚   β”œβ”€β”€ application/ # Services, DTOs, Ports
β”‚   β”‚   β”œβ”€β”€ domain/      # Entities
β”‚   β”‚   └── infrastructure/ # JPA Repositories, Port impls
β”‚   β”œβ”€β”€ inventory/
β”‚   β”œβ”€β”€ notification/
β”‚   β”œβ”€β”€ order/
β”‚   β”œβ”€β”€ payment/
β”‚   β”œβ”€β”€ review/
β”‚   └── user/
└── shared/
    β”œβ”€β”€ event/           # Domain events + EventPublisherPort
    β”œβ”€β”€ exception/       # GlobalExceptionHandler, ErrorCode
    └── response/        # ApiResponse wrapper

🐳 Docker Services

Service Container Port
PostgreSQL ecommerce-db 5433:5432
Redis ecommerce-redis 6380:6379
MailHog ecommerce-mail 1025 (SMTP), 8025 (UI)

πŸ“ License

MIT

Releases

No releases published

Packages

 
 
 

Contributors