Products API
Relevant source files
The following files were used as context for generating this wiki page:
The Products API is a core module within the application designed to manage the lifecycle of marketplace items. It facilitates operations such as listing, searching, creating, updating, and deleting products. The system acts as the bridge between the persistence layer (SQL Server) and the React-based frontend, ensuring that data is correctly transformed via Data Transfer Objects (DTOs) before being transmitted over HTTP.
This API is primarily utilized by the Home page for browsing, the PublishProduct page for new listings, and the MyProducts view for inventory management. It enforces data integrity by linking products to specific categories and registered users (sellers).
Architecture and Data Flow
The architecture follows a standard N-tier pattern where the ProductosController handles incoming HTTP requests, interacts with the IAplicacionBdContexto for database operations, and uses extension methods for mapping entities to DTOs.
Backend Processing Flow
When a request is received, the controller applies filters (such as name or availability), executes the query against the database, and converts the resulting entities into simplified DTOs to avoid exposing internal database structures or circular references.
API Endpoints
The ProductosController exposes several RESTful endpoints. The base route for all product-related operations is /productos.
| Method | Endpoint | Description | Parameters |
|---|---|---|---|
| GET | /productos | Retrieves a list of products. | nombre (string), disponible (bool) |
| GET | /productos/{id} | Retrieves details for a specific product. | id (int) |
| POST | /productos | Creates a new product listing. | CrearProductoDto (Body) |
| PUT | /productos/{id} | Modifies an existing product. | ModificarProductoDto (Body) |
| DELETE | /productos/{id} | Removes a product from the system. | id (int) |
Sources: Back/Alumnos/Api/Controllers/ProductoController.cs:19-88, Front/src/services/api.js:80-125
Data Models and Entities
The system distinguishes between database entities used for persistence and DTOs used for network communication.
Product Entity
The Producto class defines the structure within the database, including relationships to categories and users.
Data Transfer Objects (DTOs)
DTOs are used to simplify the payload. For example, the CrearProductoDto uses slugs to identify related entities instead of internal integer IDs.
public class CrearProductoDto
{
public int ProductoID { get; set; }
public string Nombre { get; set; }
public string Descripcion { get; set; }
public decimal Precio { get; set; }
public bool Disponible { get; set; }
public string CategoriaSlug { get; set; }
public string UsuarioSlug { get; set; }
public DateTime FechaPublicacion { get; set; }
}
Sources: Back/Alumnos/Api/Comun/Modelos/Productos/CrearProductoDto.cs:3-14
Implementation Details
Mapping Logic
The project uses static extension methods in MapeoDtos.cs to handle the conversion from Producto entities to BuscarProductosDto. This ensures that only necessary fields are sent to the client.
public static BuscarProductosDto ConvertirDto(this Producto producto)
{
return new BuscarProductosDto
{
ProductoID = producto.ProductoID,
Nombre = producto.Nombre,
Descripcion = producto.Descripcion,
Precio = producto.Precio,
Disponible = producto.Disponible
};
}
Sources: Back/Alumnos/Api/Comun/Utilidades/MapeoDtos.cs:25-35
Frontend Integration
The frontend interacts with these endpoints through a service layer (api.js or api.ts). These services handle authentication headers and JSON serialization.
Conclusion
The Products API provides a robust foundation for the marketplace features of the application. By utilizing DTOs and a decoupled service layer, it maintains a clean separation between the database schema and the user interface, allowing for efficient product searching and management.