Skip to content

chekwube-ononuju/enterprise-web-api

Repository files navigation

Enterprise Web API

A comprehensive ASP.NET Core 7 Web API project demonstrating enterprise-level development practices with authentication, validation, and modern architectural patterns.

🌟 Features

  • ASP.NET Core Web API 7.0 - Latest framework version
  • Entity Framework Core - Database access with SQL Server
  • ASP.NET Core Identity - User management and authentication
  • JWT Bearer Authentication - Secure token-based authentication
  • AutoMapper - Object-to-object mapping
  • FluentValidation - Input validation with fluent syntax
  • Swagger/OpenAPI - API documentation and testing interface
  • Dependency Injection - Loosely coupled architecture
  • Repository Pattern - Data access abstraction
  • Service Layer - Business logic separation

πŸ—οΈ Project Structure

EnterpriseWebAPI/
β”œβ”€β”€ Controllers/           # API controllers
β”‚   β”œβ”€β”€ AuthController.cs  # Authentication endpoints
β”‚   β”œβ”€β”€ ProductsController.cs # Product CRUD operations
β”‚   └── WeatherForecastController.cs
β”œβ”€β”€ Data/                  # Database context and configurations
β”‚   └── ApplicationDbContext.cs
β”œβ”€β”€ DTOs/                  # Data Transfer Objects
β”‚   β”œβ”€β”€ ProductDtos.cs
β”‚   └── UserDtos.cs
β”œβ”€β”€ Models/                # Entity models
β”‚   β”œβ”€β”€ Product.cs
β”‚   └── User.cs
β”œβ”€β”€ Services/              # Business logic services
β”‚   β”œβ”€β”€ AuthService.cs
β”‚   β”œβ”€β”€ IServices.cs
β”‚   └── ProductService.cs
β”œβ”€β”€ Validators/            # FluentValidation validators
β”‚   β”œβ”€β”€ ProductValidators.cs
β”‚   └── UserValidators.cs
β”œβ”€β”€ Mapping/               # AutoMapper profiles
β”‚   └── MappingProfile.cs
└── Program.cs             # Application configuration

πŸš€ Getting Started

Prerequisites

  • .NET 7.0 SDK
  • SQL Server (LocalDB for development)
  • Visual Studio 2022 or VS Code

Installation

  1. Clone the repository

    git clone <repository-url>
    cd EnterpriseWebAPI
  2. Restore packages

    dotnet restore
  3. Update database connection string

    Edit appsettings.json and appsettings.Development.json to configure your database connection.

  4. Run the application

    dotnet run
  5. Access Swagger UI

    Navigate to https://localhost:5001 or http://localhost:5000 to access the Swagger documentation.

πŸ“š API Endpoints

Authentication

  • POST /api/auth/register - Register a new user
  • POST /api/auth/login - Login with email/password
  • GET /api/auth/me - Get current user information

Products

  • GET /api/products - Get all products
  • GET /api/products/{id} - Get product by ID
  • POST /api/products - Create new product (requires authentication)
  • PUT /api/products/{id} - Update product (requires authentication)
  • DELETE /api/products/{id} - Delete product (requires authentication)
  • GET /api/products/category/{category} - Get products by category
  • GET /api/products/search?searchTerm={term} - Search products

πŸ” Authentication

The API uses JWT (JSON Web Tokens) for authentication. To access protected endpoints:

  1. Register a new user or login with existing credentials
  2. Include the JWT token in the Authorization header:
    Authorization: Bearer <your-jwt-token>
    

πŸ› οΈ Configuration

JWT Settings (appsettings.json)

{
  "JwtSettings": {
    "SecretKey": "YourSuperSecretKeyThatIsAtLeast32CharactersLong12345",
    "Issuer": "EnterpriseWebAPI",
    "Audience": "EnterpriseWebAPIUsers",
    "ExpirationInHours": 24
  }
}

Database Connection

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=EnterpriseWebAPI;Trusted_Connection=true;MultipleActiveResultSets=true"
  }
}

πŸƒβ€β™‚οΈ Running the Application

Development

dotnet run --environment Development

Production

dotnet run --environment Production

Docker (Optional)

# Build the image
docker build -t enterprise-web-api .

# Run the container
docker run -p 8080:80 enterprise-web-api

πŸ§ͺ Testing

Using Swagger UI

  1. Run the application
  2. Navigate to the Swagger UI (root URL)
  3. Use the "Authorize" button to input your JWT token
  4. Test the endpoints directly from the interface

Using Postman or cURL

Example registration request:

curl -X POST https://localhost:5001/api/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "firstName": "John",
    "lastName": "Doe",
    "email": "john.doe@example.com",
    "password": "Password123",
    "confirmPassword": "Password123"
  }'

πŸ“¦ NuGet Packages

  • Microsoft.EntityFrameworkCore.SqlServer
  • Microsoft.EntityFrameworkCore.Tools
  • Microsoft.EntityFrameworkCore.Design
  • Microsoft.AspNetCore.Identity.EntityFrameworkCore
  • Microsoft.AspNetCore.Authentication.JwtBearer
  • AutoMapper
  • AutoMapper.Extensions.Microsoft.DependencyInjection
  • FluentValidation
  • FluentValidation.AspNetCore
  • Swashbuckle.AspNetCore

🎯 Best Practices Implemented

  • Clean Architecture - Separation of concerns with layers
  • SOLID Principles - Dependency inversion, single responsibility
  • Async/Await - Non-blocking operations
  • Input Validation - FluentValidation for robust validation
  • Error Handling - Consistent error responses
  • Security - JWT authentication, password hashing
  • Documentation - Swagger/OpenAPI integration
  • Logging - Built-in .NET logging
  • Configuration - Environment-specific settings

πŸ”§ Development Commands

# Add migration
dotnet ef migrations add InitialCreate

# Update database
dotnet ef database update

# Build project
dotnet build

# Run tests
dotnet test

# Publish for deployment
dotnet publish -c Release

πŸ“‹ Environment Variables

For production deployment, consider using environment variables:

  • ConnectionStrings__DefaultConnection
  • JwtSettings__SecretKey
  • JwtSettings__Issuer
  • JwtSettings__Audience

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ†˜ Support

For support and questions:

  • Create an issue in the repository
  • Contact the development team
  • Check the API documentation in Swagger UI

Enterprise Web API - Built with ❀️ using ASP.NET Core

About

Enterprise-grade ASP.NET Core Web API with JWT authentication, validation, and comprehensive documentation

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages