API Endpoint Overview
Relevant source files
The following files were used as context for generating this wiki page:This document provides a technical overview of the WEBAPI project's backend RESTful API. The API is built using ASP.NET Core and provides endpoints for managing users, products, and orders, forming the core of an e-commerce platform. It follows a standard controller-service pattern, interacting with a SQL Server database via Entity Framework Core.
The API is structured into three main logical areas, each managed by its own controller: User Management (UserController), Product Catalog (ProductController), and Order Processing (OrderController). These controllers handle incoming HTTP requests, delegate business logic to corresponding services, and manage data persistence through the UserDBContext. The system is configured to use dependency injection for services and the database context, enabling a modular and testable architecture.
System Architecture and Services
The application is configured in Program.cs. It sets up controllers, Swagger for API documentation, a database connection to SQL Server using Entity Framework Core, and session management. It also registers services for dependency injection, which decouples the controllers from the concrete implementation of the business logic.
Sources: Program.cs:1-60
The core services are:
IUserService: Handles user-related operations like registration, login, and password management.IProductService: Manages product data, including retrieval and CRUD operations.IOrderService: Responsible for creating, retrieving, and managing customer orders.
Sources: Program.cs:40-42
The following diagram illustrates the high-level architecture and request flow.
This diagram shows how a client request flows through the API controllers, into the service layer for business logic, and finally to the data access layer to interact with the database.
Sources: Program.cs:13-42, Data/UserDBContext.cs
Database Schema
The database schema is defined using Entity Framework Core code-first migrations. The primary entities are User, Product, Order, OrderItem, Category, and Admin. The UserDBContext class brings all entity configurations together and defines the relationships between them.
Sources: Data/UserDBContext.cs:12-36, Migrations/UserDBContextModelSnapshot.cs
The Entity-Relationship diagram below visualizes the database structure.
This ER diagram illustrates the relationships between the main tables in the database, including primary keys, foreign keys, and cardinalities.
Sources: Migrations/UserDBContextModelSnapshot.cs, Models/Order.cs, Models/OrderItem.cs
User Management API (/api/v1/user)
The UserController provides endpoints for user authentication, registration, and account management.
Sources: Controllers/Login/UserController.cs
Endpoints
| Method | Path | Description | Request Body |
|---|---|---|---|
GET | / | Retrieves a list of all users. | N/A |
GET | /{id} | Retrieves a specific user by their ID. | N/A |
POST | / | Adds a new user to the system. | User object |
POST | /register | Registers a new user account. | UserRegister object |
POST | /login | Authenticates a user and returns user info. | UserLogin object |
POST | /forgot-password | Initiates the password reset process by sending a token to the user's email. | ForgotPasswordRequest object |
POST | /reset-password | Resets the user's password using a valid token. | ResetPasswordRequest object |
Sources: Controllers/Login/UserController.cs:28-212
User Login Flow
The following sequence diagram shows the process for a user logging in.
This diagram details the user login process, from the initial request to password validation and the final response.
Sources: Controllers/Login/UserController.cs:113-149, Service/user/UserService.cs:38-42
Password Reset Flow
This flow allows users to reset their password if they have forgotten it.
This diagram shows the two-step password reset process: requesting a token via email and then using that token to set a new password.
Sources: Controllers/Login/UserController.cs:151-212, Service/user/UserService.cs:112-139
Product Management API (/api/v1/product)
The ProductController exposes endpoints for managing the product catalog. It supports full CRUD (Create, Read, Update, Delete) functionality for products.
Sources: Controllers/LoadProduct/ProductController.cs
Endpoints
| Method | Path | Description | Request Body |
|---|---|---|---|
GET | / | Retrieves a list of all products. | N/A |
GET | /{id} | Retrieves a specific product by its ID. | N/A |
GET | /new | Retrieves a list of new products. | N/A |
POST | / | Adds a new product. | Product object |
PUT | /{id} | Updates an existing product. | Product object |
DELETE | /{id} | Deletes a product by its ID. | N/A |
Sources: Controllers/LoadProduct/ProductController.cs:21-86
Order Management API (/api/v1/order)
The OrderController handles all operations related to customer orders, including creation, retrieval, and status updates.
Sources: Controllers/Order/OrderController.cs
Endpoints
| Method | Path | Description | Request Body |
|---|---|---|---|
POST | / | Creates a new order. | CreateOrderRequest DTO |
GET | / | Retrieves all orders. | N/A |
GET | /{id} | Retrieves a specific order by its ID. | N/A |
GET | /user/{CustomerID} | Retrieves all orders for a specific customer. | N/A |
PUT | /{id} | Updates an entire order. | UpdateOrderRequest DTO |
PATCH | /{id} | Partially updates an order's status, address, or delivery method. | UpdateOrderStatusRequest DTO |
DELETE | /{id} | Deletes an order by its ID. | N/A |
Sources: Controllers/Order/OrderController.cs:22-131
Create Order Flow
Creating an order involves submitting customer and product details. The API then generates a unique order code and saves the order and its associated items to the database.
This diagram illustrates the sequence of events when a new order is created, from the client's request to the final database transaction.
Sources: Controllers/Order/OrderController.cs:22-63, Dtos/CreateOrderRequest.cs
Conclusion
The WEBAPI project provides a well-structured and comprehensive set of API endpoints for an e-commerce application. By leveraging ASP.NET Core, Entity Framework, and a service-oriented architecture, it offers a robust backend for managing users, products, and orders. The API is clearly defined through controllers and DTOs, with business logic encapsulated in services, making it maintainable and scalable.