D-Fitness Gym Web API - Live Demo
This is a .NET 8 Web API for managing a gym application including Users, Trainers, Admins, Memberships, Subscriptions, and Transactions. The project uses Entity Framework Core for database access, follows a layered architecture with Repositories, Services, and Controllers, and supports dependency injection for maintainability.
Live Swagger Docs: Click here 👉 https://dfitnessgym.runasp.net/swagger/index.html
- Project Overview
- Architecture & Design
- Prerequisites
- Setup & Installation
- Environment Variables
- Running the Project
- Folder Structure
- Logging
- Unit Testing
The D-Fitness Gym Web API provides endpoints for:
- Roles: Create, read, update, delete roles.
- Accounts: Manage Users, Trainers, and Admins.
- Memberships & Subscriptions: CRUD operations and tracking subscription status.
- Transactions: Handle subscription payments, salaries, and other transactions.
The project follows clean architecture principles:
- Controllers: Handle HTTP requests and responses (
RolesController, etc.). - Services: Contain business logic and communicate with repositories (
RoleService). - Repositories: Handle database operations using EF Core (
RoleRepository). - Data: Contains
ApplicationDbContextand migrations. - Models:
Enumns→ enum fields.Entities→ database models.DTOs→ data transfer objects for API requests/responses.
- Logging: Injected into controllers and services using
ILogger<T>. - Dependency Injection: Services and repositories registered in
Program.csfor maintainable code. - Unit Testing: Each controller and service can be tested independently using xUnit/Moq.
- .NET 8 SDK
- Visual Studio 2022 or VS Code
- SQL Server (local or remote)
- Git
- Clone the repository
git clone https://github.com/jeethendra2000/D_Fitness_Gym_Backend_API.git
cd D_Fitness_Gym_Backend_API- Open the solution in Visual Studio
- Double click
D-Fitness-Gym.sln
- Install NuGet packages (if not automatically restored):
dotnet restore-
Key packages used:
Microsoft.EntityFrameworkCoreMicrosoft.EntityFrameworkCore.SqlServerMicrosoft.EntityFrameworkCore.ToolsSwashbuckle.AspNetCore(Swagger)DotNetEnv(for environment variables)Moq/xUnit(for unit tests)
- Update Database Connection
- Create a
.envfile in the solution root:
DB_CONNECTION_STRING=Server=localhost;Database=D-FitnessDB;Trusted_Connection=True;
- The connection string is referenced in
Program.csviaDotNetEnv.
Create a .env file in the root of the project and replace the placeholders < > with your actual values:
# Database connections
# Local development connection (Windows Auth)
LOCAL_DB_CONNECTION_STRING=Server=localhost;Database=D-FitnessDB;Trusted_Connection=True;
# Production / Staging connection (SQL Auth, without encryption)
DB_CONNECTION_STRING=Server=<yourServer>; Database=<yourDatabase>; User Id=<yourUserId>; Password=<yourPassword>; Encrypt=False; MultipleActiveResultSets=True;
# Public/Cloud connection (with encryption + certificate trust)
PUBLIC_DB_CONNECTION_STRING=Server=<yourServer>; Database=<yourDatabase>; User Id=<yourUserId>; Password=<yourPassword>; Encrypt=True; TrustServerCertificate=True; MultipleActiveResultSets=True;
# Other secrets
AUTO_MAPPER_LICENCE_KEY=<yourapikey>
- Apply Migrations (if database not created yet):
EntityFrameworkCore\Add-Migration "initial Migrations"
EntityFrameworkCore\update-database- Run the Web API
- From Visual Studio → Press
F5orCtrl+F5
- Swagger UI
- Navigate to:
https://localhost:<port>/swaggerto explore API endpoints.
D-Fitness-Gym/
│
├─ Controllers/ # API endpoints / controllers
├─ Services/ # Business logic layer
│ └─ Interfaces/ # Interfaces for services
├─ Repositories/ # Data access layer for Database operations
│ └─ Interfaces/ # Interfaces for repositories
├─ Data/ # ApplicationDbContext, seeding, EF migrations
├─ Middleware/ # Custom middlewares (e.g. error handling, logging)
├─ Mappings/ # AutoMapper profiles
├─ Models/
│ ├─ Entities/ # EF Core entities (Account, Role, Subscription, etc.)
│ ├─ DTO/ # DTOs for API communication
│ │ ├─ AccountDto/
│ │ └─ RoleDto/
│ └─ Enums/ # Enum definitions
├─ Utils/ # Helper classes (extensions, constants, etc.)
├─ Properties/ # Assembly info
├─ Migrations/ # EF Core migrations (optional inside Data/)
├─ D-Fitness-Gym.sln
├─ Program.cs
├─ Startup.cs # (optional, if you prefer instead of Program.cs)
├─ appsettings.json
├─ appsettings.Development.json
├─ appsettings.Production.json
├─ .env # Env vars (connection strings, secrets)
├─ .gitignore
└─ README.md
D_Fitness_Gym.Tests/
│
├─ Controllers/ # Unit tests for controllers
├─ Services/ # Unit tests for services
├─ Repositories/ # Unit tests for repositories
├─ TestHelpers/ # Test utils (mock data, in-memory DB, etc.)
│
├─ D_Fitness_Gym.Tests.csproj
└─ appsettings.Test.json
- Logging is implemented using
ILogger<T>in Controllers, Services, and Repositories. - Follows industry standard for monitoring API operations, debugging, and error tracking.
-
Uses xUnit for unit testing.
-
Separate project:
D-Fitness-Gym.Tests -
Tests include:
- Controller actions
- Service methods
- Repository operations (optional with in-memory DB)
-
Example test class naming convention:
RolesControllerUnitTests.cs
💡 Industry Practices Followed:
- DTOs for request/response separation
- Dependency Injection for maintainable, testable code
- Logging with
ILogger - Unit tests for critical business logic and APIs
- Environment variables for secrets