Getting Started
Relevant source files
The following files were used as context for generating this wiki page:
- Program.cs
- Properties/launchSettings.json
- Data/UserDBContext.cs
- Controllers/Login/UserController.cs
- Configuration/UserConfiguration.cs
- Configuration/ProductConfiguration.cs
- Configuration/OrderConfiguration.cs
- Configuration/CategoryConfiguration.cs
- Configuration/AdminConfiguration.cs
- Migrations/UserDBContextModelSnapshot.cs
- Controllers/LoadProduct/ProductController.cs
- Controllers/Order/OrderController.cs
- Seeders/DatabaseSeeder.cs
- Data/UserDBContextFactory.cs
This document provides a technical overview of the WEBAPI project, an ASP.NET Core application designed to serve as the backend for an e-commerce platform. It covers the initial setup, core architecture, database schema, and key API endpoints. The primary purpose is to guide developers in understanding the project structure, running the application locally, and interacting with its main components.
The application is built using Entity Framework Core for data access, with a clear separation of concerns through services, controllers, and data transfer objects (DTOs). It includes functionalities for user management (registration, login, password reset), product catalog management, and order processing.
Application Startup and Configuration
The application's entry point and service configuration are defined in Program.cs. It sets up all necessary services and configures the HTTP request pipeline.
Sources: Program.cs
Service Registration
The following services are registered when the application starts:
- Controllers with JSON Options: Configures controllers and sets JSON serialization to ignore object cycles.
- API Explorer & Swagger: Enables API documentation and a UI for testing endpoints.
- DbContext: Registers
UserDBContextwith a SQL Server provider, using a connection string namedUserDataBase. - Session Management: Configures session state with a 5-minute idle timeout.
- Dependency Injection: Registers custom application services:
IUserService(Transient)IProductService(Scoped)IOrderService(Scoped)
- HttpClient: Registers
IHttpClientFactory.
Sources: Program.cs:13-40
HTTP Request Pipeline
The middleware pipeline is configured to handle incoming requests. In a development environment, it includes Swagger for API documentation.
The following diagram illustrates the middleware pipeline configuration.
Sources: Program.cs:45-62
Launch Profiles
The project can be launched using several profiles defined in launchSettings.json. These profiles configure the application URL, environment variables, and whether to launch a browser.
| Profile | Command | Application URL | Launch URL | Environment |
|---|---|---|---|---|
http | Project | http://localhost:5048 | swagger | Development |
https | Project | https://localhost:7032;http://localhost:5048 | swagger | Development |
IIS Express | IISExpress | http://localhost:19162 (IIS) | swagger | Development |
Sources: Properties/launchSettings.json:15-43
Database and Data Models
The application uses Entity Framework Core to interact with a SQL Server database. The database context, models, and configurations define the data layer.
Database Context (UserDBContext)
UserDBContext is the main EF Core context class. It defines DbSet properties for all major entities and applies entity configurations and data seeding during model creation.
The context registers the following entities:
UserOrderProductAdminOrderItemCategory
Sources: Data/UserDBContext.cs:15-39
Entity Relationship Diagram
The diagram below shows the relationships between the main database tables as defined by the EF Core models and configurations.
Data Seeding
The application seeds an initial User into the database on model creation. This is useful for initial setup and testing.
// Seeders/DatabaseSeeder.cs
public static void Seed(this ModelBuilder modelBuilder)
{
_ = modelBuilder.Entity<User>().HasData(
new User()
{
Id = 1,
Email = "chihtt123@gmail.com",
UserName = "chi",
Password = "123",
Role = true
}
);
}
Sources: Seeders/DatabaseSeeder.cs:8-20, Data/UserDBContext.cs:29
API Endpoints
The application exposes a RESTful API for managing users, products, and orders. Controllers are organized by feature.
Key Controllers
| Controller | Route Prefix | Description |
|---|---|---|
UserController | api/v1/user | Handles user registration, login, password management, and user data retrieval. |
ProductController | api/v1/product | Manages the product catalog, including creating, reading, updating, and deleting products. |
OrderController | api/v1/order | Manages customer orders, including creation, retrieval, and status updates. |
Sources: Controllers/Login/UserController.cs:13, Controllers/LoadProduct/ProductController.cs:9, Controllers/Order/OrderController.cs:12
User Login Flow
The following sequence diagram illustrates the process for a user logging into the system.
Example Endpoints
| Method | Endpoint | Controller | Description |
|---|---|---|---|
POST | /api/v1/user/register | UserController | Registers a new user. |
POST | /api/v1/user/login | UserController | Authenticates a user and returns their details. |
POST | /api/v1/user/forgot-password | UserController | Sends a password reset token to the user's email. |
GET | /api/v1/product | ProductController | Retrieves a list of all products. |
GET | /api/v1/product/{id} | ProductController | Retrieves a single product by its ID. |
POST | /api/v1/order | OrderController | Creates a new order from a CreateOrderRequest. |
GET | /api/v1/order/user/{CustomerID} | OrderController | Retrieves all orders for a specific customer. |
PATCH | /api/v1/order/{id} | OrderController | Partially updates an order's status, address, or delivery method. |
Sources: Controllers/Login/UserController.cs, Controllers/LoadProduct/ProductController.cs, Controllers/Order/OrderController.cs