Skip to content

Repository files navigation

eCommerce API

A modern, full-featured RESTful e-commerce API built with .NET 9 and Clean Architecture principles. This repository demonstrates industry-standard patterns and best practices for building scalable, maintainable systems.

Table of Contents

Overview

This eCommerce API is a learning-focused project that implements product and category management, JWT authentication, and payment gateway integrations (Stripe & Fawaterek). The solution includes validation, global error handling, and structured logging to illustrate production-ready practices.

Key Features

Authentication & Authorization

  • User registration and login with JWT tokens
  • Token refresh mechanism

Product & Category Management

  • Full CRUD operations for products and categories
  • Product categorization and basic inventory tracking

Payment Processing

  • Stripe integration (credit/debit cards)
  • Fawaterek integration (regional/local payments)

Enterprise Features

  • Centralized validation via MediatR pipeline behavior
  • Global exception handling middleware
  • Structured logging with Serilog (console and rolling files)
  • Docker images
  • Swagger documentation

Architecture & Patterns

Clean Architecture (Four Layers)

Domain          ↔       Application
 ↑                           ↑
 └─── Infrastructure ↔ ── Host
  • Domain — Core business entities
  • Application — Use cases, DTOs, validation, MediatR handlers
  • Infrastructure — Data access, external services, authentication
  • Host — API entry point and controllers

Implemented Design Patterns

Pattern Purpose
Repository Pattern Abstracts data access layer
Unit of Work Manages transactions across repositories
CQRS Separates read (queries) and write (commands) operations
Mediator Routes requests to handlers via MediatR
Pipeline Behavior Automatic validation before handler execution
Middleware Global exception handling and logging
Dependency Injection Centralized service registration
Data Transfer Objects Decouples API contracts from domain models
Factory Dynamic payment service instantiation

Tech Stack

Category Technology
Framework .NET 9 / ASP.NET Core 9.0
Language C# (nullable reference types)
ORM Entity Framework Core 9
Database SQL Server 2019+
CQRS & Mediation MediatR
Validation FluentValidation
Mapping AutoMapper
Logging Serilog
Payment Stripe.net, RestSharp
Documentation Swagger / Swashbuckle

Getting Started

Prerequisites

  • .NET 9 SDK or later
  • SQL Server 2019 or later
  • Visual Studio 2022 / VS Code
  • Docker (optional)

Installation

# Clone repository
git clone <repository-url>
cd eCommerce API

# Restore NuGet packages
dotnet restore

# Apply database migrations
dotnet ef database update --project eCommerce.Infrastruction --startup-project eCommerce.Host

# Run the application
dotnet run --project eCommerce.Host

API: https://localhost:<port>

Swagger UI: https://localhost:<port>/swagger

Configuration

Update appsettings.json with your settings. Sensitive values should be stored in environment variables or a secrets manager in production.

{
  "Stripe": {
    "PublishableKey": "pk_test_YOUR_KEY",
    "SecretKey": "sk_test_YOUR_KEY",
    "WebhookSecret": "webhook_secret_key"
  },
  "Fawaterek": {
    "ApiKey": "YOUR_API_KEY",
    "MerchantId": "YOUR_MERCHANT_ID",
    "BaseUrl": "https://api.fawaterek.com"
  }
}

Important: Never commit API keys to version control. Use environment variables or Azure Key Vault in production.

Advanced Features

Validation Pipeline

Automatic validation through MediatR pipeline behavior. Create validators using FluentValidation:

public class CreateUserValidator : AbstractValidator<CreateUserDto>
{
    public CreateUserValidator()
    {
        RuleFor(x => x.FullName).NotEmpty();
        RuleFor(x => x.Email).EmailAddress();
    }
}

Invalid requests return HTTP 400 with error details automatically.

Global Exception Handling

Unhandled exceptions are caught by middleware, logged via Serilog, and returned as consistent JSON responses:

{
  "statusCode": 500,
  "message": "An error occurred"
}

Structured Logging

Serilog logs all application events to console and daily rolling files in the logs/ directory.

Docker Support

Multi-stage Docker build creates optimized production images:

docker build -t ecommerce-api:latest .
docker run -d -p 8080:8080 ecommerce-api:latest

API Endpoints

Authentication

Method Endpoint Description
POST /api/auth/register Register new user
POST /api/auth/login Login and receive JWT token
POST /api/auth/refresh Refresh expired token

Products

Method Endpoint Description
GET /api/products Get all products
GET /api/products/{id} Get product by ID
POST /api/products Create new product
PUT /api/products/{id} Update product
DELETE /api/products/{id} Delete product

Categories

Method Endpoint Description
GET /api/categories Get all categories
GET /api/categories/{id} Get category by ID
POST /api/categories Create new category
PUT /api/categories/{id} Update category
DELETE /api/categories/{id} Delete category

Payment

Method Endpoint Description
POST /api/payment/checkout Process checkout
POST /api/payment/process Process payment

Project Structure

eCommerce.Domain/
├── Entities/
└── ...

eCommerce.Application/
├── DTOs/
├── Features/
│   ├── Commands/
│   └── Queries/
├── Validation/
├── Services/
├── Common/
│   └── Behaviors/
└── Exceptions/

eCommerce.Infrastruction/
├── Context/
├── Repositories/
├── Services/
├── Middleware/
└── Authentication/

eCommerce.Host/
├── Controllers/
├── Program.cs
└── appsettings.json

License

MIT License - See LICENSE file for details.

About

Backend eCommerce API built with .NET 9. The goal was not to build a huge project, but to implement and practice multiple backend features and architecture patterns.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages