Product & Category API
Relevant source files
The following files were used as context for generating this wiki page:The Product & Category API provides a comprehensive set of endpoints for managing product and category information within the application. It follows a standard layered architecture, separating concerns into controllers for handling HTTP requests, services for business logic, and data models for representing database entities. This system allows for creating, reading, updating, and deleting (CRUD) product records, as well as retrieving product and category data.
The core components include the ProductController, which exposes the API endpoints, the IProductService and its implementation ProductService, which encapsulate the business logic for product operations, and the Product and Category entity models, which are managed by Entity Framework Core and mapped to the database via UserDBContext.
Architecture Overview
The API employs a dependency injection pattern to decouple its components. The ProductController receives an IProductService instance, which in turn uses the UserDBContext to interact with the database. This structure promotes testability and maintainability.
The following diagram illustrates the request flow for product-related operations.
This diagram shows the flow of a request from the client through the controller and service layers to the database.
Sources: Controllers/LoadProduct/ProductController.cs:14-17, Service/product/ProductService.cs:13-16, Program.cs:41
Data Models
The API relies on two primary data models: Product and Category. These models are defined as C# classes and are mapped to database tables by Entity Framework Core.
Sources: Models/Category.cs, Migrations/UserDBContextModelSnapshot.cs:175-214, Data/UserDBContext.cs:39
Entity Relationship Diagram
The Product and Category entities have a one-to-many relationship: one Category can have many Products.
This diagram shows the one-to-many relationship between the Category and Product tables.
Sources: Models/Category.cs:15, Migrations/UserDBContextModelSnapshot.cs:298-302
Product Model
The Product model represents a product in the system.
| Property | Type | Constraints | Description |
|---|---|---|---|
Id | int | Primary Key | Unique identifier for the product. |
Title | string | Required, MaxLength(100) | The title of the product. |
Name | string | Required, MaxLength(200) | The full name of the product. |
Description | string | MaxLength(500) | A description of the product. |
Url | string | MaxLength(200) | A URL slug for the product. |
Gender | string | MaxLength(10) | The target gender for the product. |
Price | decimal | Required, Type(decimal(18,2)) | The price of the product. |
ProductImageUrl | string | MaxLength(500) | URL for the product's image. |
CategoryId | int? | Foreign Key | The ID of the category this product belongs to. |
Sources: Configuration/ProductConfiguration.cs:14-40, Migrations/UserDBContextModelSnapshot.cs:175-214
Category Model
The Category model represents a product category.
| Property | Type | Constraints | Description |
|---|---|---|---|
Id | int | Primary Key | Unique identifier for the category. |
Name | string | Required, MaxLength(100) | The name of the category. |
Description | string | MaxLength(500) | A description of the category. |
Products | ICollection<Product> | Navigation Property | A collection of products within this category. |
Sources: Models/Category.cs:10-15, Configuration/CategoryConfiguration.cs:15-22, Migrations/UserDBContextModelSnapshot.cs:68-87
Database Configuration
Entity Framework Core configurations are applied in the UserDBContext to define table names, keys, and property constraints.
Sources: Data/UserDBContext.cs:16-19, Configuration/ProductConfiguration.cs, Configuration/CategoryConfiguration.cs
ProductConfiguration
This class configures the Product entity.
- The table is named
Product. Idis the primary key.- Properties like
Title,Name, andPriceare marked as required. - String properties have maximum lengths defined.
Priceis mapped to adecimal(18,2)column type.
Sources: Configuration/ProductConfiguration.cs:11-40
CategoryConfiguration
This class configures the Category entity.
- The table is named
Categories. Idis the primary key.Nameis required with a max length of 100.Descriptionhas a max length of 500.
Sources: Configuration/CategoryConfiguration.cs:11-22
Service Layer
The business logic for product management is handled by the ProductService, which implements the IProductService interface.
IProductService Interface
This interface defines the contract for product-related operations.
| Method | Return Type | Description |
|---|---|---|
GetProductsAsync() | Task<IEnumerable<Product>> | Retrieves all products. |
GetProductByIdAsync(int id) | Task<Product> | Retrieves a single product by its ID. |
AddProductAsync(Product newProduct) | Task<bool> | Adds a new product. |
UpdateProductAsync(Product updatedProduct) | Task<bool> | Updates an existing product. |
DeleteProductAsync(int id) | Task<bool> | Deletes a product by its ID. |
GetNewProductsAsync() | Task<IEnumerable<Product>> | Retrieves the 3 most recently added products. |
Sources: Service/product/IProductService.cs:7-14
ProductService Implementation
ProductService provides the concrete implementation for the IProductService methods, using UserDBContext to query and manipulate product data. For example, GetNewProductsAsync retrieves the last 3 products by ordering them by ID in descending order and taking the top 3.
Sources: Service/product/ProductService.cs:10-101
The following sequence diagram shows the process of adding a new product.
This diagram illustrates the sequence of calls when a client adds a new product via the API.
Sources: Controllers/LoadProduct/ProductController.cs:43-53, Service/product/ProductService.cs:36-47
API Endpoints
The ProductController exposes several endpoints for interacting with product data. All endpoints are under the base route api/v1/product.
| Method | Route | Description |
|---|---|---|
GET | / | Retrieves a list of all products. |
GET | /{id} | Retrieves a single product by its unique ID. |
POST | / | Creates a new product. Expects a Product object in the request body. |
PUT | /{id} | Updates an existing product. Expects a Product object in the request body. |
DELETE | /{id} | Deletes a product by its unique ID. |
GET | /new | Retrieves the 3 newest products. |
Sources: Controllers/LoadProduct/ProductController.cs:10-85
Conclusion
The Product & Category API provides a well-structured and robust system for managing product data. By leveraging a layered architecture, dependency injection, and Entity Framework Core, it offers a clean separation of concerns, making it maintainable and scalable. The defined endpoints provide comprehensive CRUD functionality, supporting the core e-commerce features of the application.