Order & OrderItem Data Models
Relevant source files
The following files were used as context for generating this wiki page:- Models/Order.cs
- Models/OrderItem.cs
- Configuration/OrderConfiguration.cs
- Configuration/OrderItemConfiguration.cs
- Controllers/Order/OrderController.cs
- Service/order/IOrderService.cs
- Service/order/OrderService.cs
- Dtos/CreateOrderRequest.cs
- Dtos/UpdateOrderRequest.cs
- Data/UserDBContext.cs
- Migrations/UserDBContextModelSnapshot.cs
The Order and OrderItem data models are fundamental components of the application's e-commerce functionality. They are responsible for representing and persisting customer order information. The Order model captures high-level details of a transaction, such as the customer, total amount, and status. The OrderItem model represents each individual product line within an order, detailing the product, quantity, and price.
These models are managed through a dedicated service layer (IOrderService) and exposed via a RESTful API (OrderController). Entity Framework Core is used for data persistence, with specific configurations defining the database schema, relationships, and constraints for the Orders and OrderItems tables.
Data Model Schema
The core of the order system is composed of the Order and OrderItem entities. An Order has a one-to-many relationship with OrderItem, meaning each order can contain multiple line items.
Order Entity
The Order class represents a single customer order.
Sources: Models/Order.cs
| Property | Type | Description |
|---|---|---|
OrderID | int | Primary Key for the Order. |
OrderCode | string | A unique code for the order. |
CustomerID | int | Foreign Key referencing the User (customer). |
TotalAmount | decimal | The total cost of the order. |
OrderDate | DateTime | The date and time the order was placed. |
DeliveryMethod | string? | The chosen method for delivery. |
PaymentMethod | string? | The method used for payment. |
Address | string | The delivery address. |
Status | string | The current status of the order (e.g., "Pending", "Completed"). |
Customer | User | Navigation property to the associated User. |
OrderItems | List<OrderItem> | Navigation property to the list of items in this order. |
Sources: Models/Order.cs:7-19
OrderItem Entity
The OrderItem class represents a single line item within an Order.
Sources: Models/OrderItem.cs
| Property | Type | Description |
|---|---|---|
Id | int | Primary Key for the OrderItem. |
OrderID | int | Foreign Key referencing the Order. |
ProductID | int | Foreign Key referencing the Product. |
Quantity | int | The quantity of the product ordered. |
UnitPrice | decimal | The price of a single unit of the product at the time of order. |
Order | Order | Navigation property to the parent Order. Hidden from JSON serialization. |
Product | Product | Navigation property to the associated Product. |
Sources: Models/OrderItem.cs:11-21
Class Diagram
This diagram shows the properties and navigation links between the Order and OrderItem models.
Sources: Models/Order.cs, Models/OrderItem.cs
Database Configuration and Relationships
Entity Framework Core configurations are used to map the C# models to the SQL database schema. These configurations define table names, primary keys, column types, constraints, and relationships.
Entity-Relationship Diagram (ERD)
The following ERD illustrates the database table relationships as defined by the EF Core configurations and model snapshot.
OrderConfiguration
This configuration class sets up the Orders table.
- Table Name:
Orders - Primary Key:
OrderID - Relationships: Defines a one-to-many relationship where one
Ordercan have manyOrderItems. - Property Constraints:
OrderDatehas a default SQL value ofGETDATE().TotalAmountis adecimal(18,2).- Various properties are marked as
IsRequired()and haveHasMaxLength()constraints.
Sources: Configuration/OrderConfiguration.cs:11-29, Data/UserDBContext.cs:16
OrderItemConfiguration
This class configures the OrderItems table.
- Table Name:
OrderItems - Primary Key:
Id - Relationships:
- Defines the
many-to-onerelationship back toOrdervia theOrderIDforeign key. - Defines a relationship to
Productvia theProductIDforeign key.
- Defines the
- Property Constraints:
UnitPriceis adecimal(18,2).OrderID,ProductID, andQuantityare required fields.
Sources: Configuration/OrderItemConfiguration.cs:11-28, Data/UserDBContext.cs:20
API Layer
The OrderController provides HTTP endpoints for managing orders. It relies on IOrderService to perform business logic and database operations.
Sources: Controllers/Order/OrderController.cs
Endpoints
| Method | Route | Description | Request Body |
|---|---|---|---|
POST | /api/v1/order | Creates a new order. | CreateOrderRequest |
GET | /api/v1/order | Retrieves all orders. | N/A |
GET | /api/v1/order/{id} | Retrieves a single order by its ID. | N/A |
GET | /api/v1/order/user/{CustomerID} | Retrieves all orders for a specific customer. | N/A |
PUT | /api/v1/order/{id} | Updates an entire order. | UpdateOrderRequest |
PATCH | /api/v1/order/{id} | Partially updates an order's status, address, and delivery method. | UpdateOrderStatusRequest |
DELETE | /api/v1/order/{id} | Deletes an order by its ID. | N/A |
Sources: Controllers/Order/OrderController.cs:18-132
Data Transfer Objects (DTOs)
DTOs are used to shape the data sent to and from the API, decoupling the API contract from the internal data models.
CreateOrderRequest: Used for creating a new order. It contains customer ID, payment/delivery details, and a list ofOrderItemRequestobjects.
Sources: Dtos/CreateOrderRequest.cs:8-17UpdateOrderRequest: Used for updating an existing order with a full set of new data.
Sources: Dtos/UpdateOrderRequest.cs:8-19UpdateOrderStatusRequest: Used for thePATCHendpoint to update only the status, address, and delivery method of an order.
Sources: Dtos/UpdateOrderRequest.cs:21-26
Order Creation Flow
The following sequence diagram illustrates the process of creating a new order via the API.
Service Layer
The service layer abstracts the data access logic from the controllers. IOrderService defines the contract for order-related operations, and OrderService provides the implementation.
Sources: Service/order/IOrderService.cs, Service/order/OrderService.cs
IOrderService Interface
Defines the standard operations for managing orders.
| Method | Return Type | Description |
|---|---|---|
GetOrdersAsync() | Task<IEnumerable<Order>> | Gets all orders with their items, products, and customers. |
GetOrderByIdAsync(int id) | Task<Order> | Gets a single order by ID, including related entities. |
AddOrderAsync(Order newOrder) | Task<bool> | Adds a new order to the database. |
UpdateOrderAsync(int id, Order updatedOrder) | Task<Order?> | Updates an existing order. |
UpdateOrderPartialAsync(...) | Task<Order?> | Partially updates an order's status, address, and delivery method. |
DeleteOrderAsync(int id) | Task<Order?> | Deletes an order from the database. |
GetOrdersByCustomerIdAsync(int customerId) | Task<IEnumerable<Order>> | Gets all orders for a specific customer. |
Sources: Service/order/IOrderService.cs:9-17
OrderService Implementation
The OrderService class implements IOrderService using UserDBContext to interact with the database. When retrieving orders, it uses Entity Framework's Include() and ThenInclude() methods to eagerly load related OrderItems, Product, and Customer data, preventing N+1 query problems and ensuring all necessary data is returned in a single database trip.
Sources: Service/order/OrderService.cs:17-31, Service/order/OrderService.cs:101-106
Summary
The Order and OrderItem models, along with their corresponding configurations, services, and controllers, form a robust system for managing customer orders. The architecture clearly separates concerns between the API interface (controller), business logic (service), and data persistence (EF Core models and context). The use of DTOs ensures a stable API contract, while eager loading in the service layer provides efficient data retrieval.