Skip to content

tahakocal/ECommerceOrderSystem

Repository files navigation

E-Commerce Order System

A mini backend system for an e-commerce company that processes orders and sends notifications to users, built with .NET 9, RabbitMQ, Redis, and SQL Server.

Architecture

The solution follows Clean Architecture principles with the following projects:

  • ECommerce.Domain: Core business entities and enums
  • ECommerce.Application: Business logic, interfaces, DTOs, and services
  • ECommerce.Infrastructure: Data access, external service implementations (RabbitMQ, Redis, SQL Server)
  • ECommerce.API: REST API with JWT authentication
  • ECommerce.OrderProcessor: Background worker service that consumes RabbitMQ messages

Features

  • ✅ Create orders via REST API
  • ✅ Input validation with FluentValidation
  • ✅ Order queuing with RabbitMQ
  • ✅ Background order processing
  • ✅ Redis caching with 2-minute TTL
  • ✅ JWT authentication
  • ✅ Serilog logging with file and console sinks
  • ✅ Correlation ID tracking
  • ✅ Swagger UI documentation
  • ✅ Docker Compose for infrastructure
  • ✅ Unit tests for core business logic

Prerequisites

  • .NET 9 SDK
  • Docker and Docker Compose
  • Visual Studio 2022 or JetBrains Rider (optional)

Getting Started

1. Clone the Repository

git clone https://github.com/tahakocal/ECommerceOrderSystem.git
cd ECommerceOrderSystem

2. Start Infrastructure Services

docker-compose up -d

This will start:

  • SQL Server on port 1433
  • RabbitMQ on port 5672 (Management UI on 15672)
  • Redis on port 6379

3. Apply Database Migrations

Migration files are already included in the project. You just need to apply them:

# Install EF Core tools globally (if not already installed)
dotnet tool install --global dotnet-ef

# Apply existing migrations to create database schema
cd src/ECommerce.Infrastructure
dotnet ef database update --startup-project ../ECommerce.API

This will create:

  • ECommerceDB database
  • Users table
  • Orders table
  • OrderLogs table
  • OutboxMessages table

4. Run the API

cd src/ECommerce.API
dotnet run

The API will be available at:

5. Run the Order Processor Worker

In a new terminal:

cd src/ECommerce.OrderProcessor
dotnet run

API Endpoints

Detailed API documentation with request/response examples is available at: Swagger UI: https://localhost:7219/swagger or http://localhost:5079/swagger

Health Check

  • GET /health - API health status

Test Credentials

A test user is automatically created when the application starts with sample data:

{
  "username": "tahakocal",
  "password": "123456"
}

Sample Orders (automatically seeded):

  • Order 1: PROD-001, Quantity: 2, Status: Completed, Payment: CreditCard (5 days ago)
  • Order 2: PROD-002, Quantity: 1, Status: Processing, Payment: BankTransfer (2 days ago)
  • Order 3: PROD-003, Quantity: 3, Status: Pending, Payment: CreditCard (1 hour ago)

You can use these credentials to login and test the API endpoints, including retrieving the pre-seeded orders.

Authentication

  • POST /api/auth/register - Register new user
  • POST /api/auth/login - Login with credentials

Orders (Requires Authentication)

  • POST /api/orders - Create new order
  • GET /api/orders/{userId} - Get user's orders (cached)

Order Processing Flow

  1. Client sends order request to API
  2. API validates input using FluentValidation
  3. Order is saved to SQL Server database
  4. Order event is saved to OutboxMessages table (for reliability)
  5. System attempts to publish to RabbitMQ order-placed queue
    • If successful, marks outbox message as processed
    • If RabbitMQ is down, message remains in outbox for later processing
  6. Redis cache for user's orders is invalidated
  7. Background worker picks up the message from queue
  8. Worker processes the order (simulated with 5-second delay)
  9. Processing log is stored in Redis
  10. Notification is logged (can be extended to send actual notifications)

Logging

Serilog Configuration:

  • Console Sink: Structured logging with timestamp and level
  • File Sink: Daily rolling files with 7-day retention
  • Correlation ID: Every request gets a unique correlation ID for tracing

Log Files:

  • API: logs/api-log-{Date}.txt
  • Worker: logs/worker-log-{Date}.txt

Log Levels:

  • Information: API calls, user actions, order processing
  • Warning: Validation failures, failed login attempts
  • Error: Exceptions, connection failures

Design Patterns Used

  • Clean Architecture: Separation of concerns with domain-centric design
  • Repository Pattern: Data access abstraction
  • Dependency Injection: Loose coupling and testability
  • Outbox Pattern: Reliable message delivery with transactional guarantees
  • Message Queue Pattern: Asynchronous processing with RabbitMQ

Security Considerations

  • JWT authentication for API endpoints
  • Input validation on all requests
  • Secure password storage with PBKDF2-SHA256 hashing
  • HTTPS enforcement in production
  • Correlation IDs for request tracking

Monitoring & Debugging

RabbitMQ Management UI

URL: http://localhost:15672
Credentials: guest/guest

Monitor queues:

  • Navigate to "Queues" tab
  • Check order-placed queue for message count
  • View message rates and processing

Testing

Run unit tests with:

dotnet test

Tests cover:

  • Order creation and validation
  • Authentication (login/register)
  • Caching operations
  • Message queue publishing
  • Error handling

if you have any questions, please feel free to ask. Note: Some comments were made with the help of AI. I hope it helps you.

Thanks for reading.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages