Backend Messaging Logic
Relevant source files
The following files were used as context for generating this wiki page:
The Backend Messaging Logic facilitates communication between users (buyers and sellers) within the platform. It encompasses the data structures required for persisting message history, the configuration of database relationships, and the service layer logic that manages chat initialization and message delivery. While the backend provides the persistence infrastructure via Entity Framework Core, the frontend consumes these capabilities through a dedicated context that handles real-time state and local storage synchronization.
Data Architecture and Persistence
The system uses a structured approach to store messages within a SQL Server database. The core entity is the Mensajes class, which serves as the data model for individual communication entries.
Messaging Entity Model
The Mensajes entity captures the essential metadata for every message sent within the system, including a unique identifier, the message text, and a timestamp.
| Field | Type | Description |
|---|---|---|
MensajeId | int | Primary key for the message record. |
Texto | string | The actual content of the message. |
Fecha | DateTime | The date and time when the message was sent. |
Sources: Back/Alumnos/Api/Entidades/Mensajes.cs:8-15
Database Configuration
The persistence layer is configured using Fluent API within the MensajesConfiguracion class. This ensures that the MensajeId is correctly mapped as the primary key in the underlying database schema.
public void Configure(EntityTypeBuilder<Mensajes> constructor)
{
constructor.HasKey(u => u.MensajeId);
}
Sources: Back/Alumnos/Api/Persistencia/Configuraciones/MensajesConfiguracion.cs:9-12
The AplicacionBdContexto integrates this entity into the application's database context, allowing for standard CRUD operations and transaction management.
Sources: Back/Alumnos/Api/Persistencia/AplicacionBdContexto.cs:17-18
Messaging Logic Flow
The messaging flow is initiated when a user attempts to contact a seller regarding a specific product. This creates a unique chat session identified by a combination of participant IDs and the product ID.
Chat Session Initialization
Chats are structured as unique associations between a buyer, a seller, and a product. This relationship ensures that conversations remain contextual to specific marketplace items.
Message Transmission
When a message is sent, the system updates the chat state and ensures the timestamp is recorded in ISO format for consistency across different time zones.
API and Security Integration
The backend messaging logic is supported by a robust middleware configuration in Program.cs. This includes JWT authentication to ensure only authorized users can send or receive messages, and CORS policies to allow communication with the frontend.
Security Configurations
- Authentication: Uses
JwtBearerDefaultsto validate identity tokens. - CORS: Configured with
PoliticasCorsto expose theAuthorizationheader, enabling the frontend to manage session tokens during messaging operations. - Database: Connection is established via
UseSqlServerusing theDefaultConexionstring.
Sources: Back/Alumnos/Api/Program.cs:51-60, Back/Alumnos/Api/Program.cs:64-90
Summary
The Backend Messaging Logic provides a reliable foundation for user-to-user communication. By combining a strictly typed Entity Framework model for persistence with a dynamic frontend context for session management, the system ensures that messages are accurately tracked, associated with specific products, and secured through standard JWT authentication protocols. This architecture allows for seamless transitions from product discovery to direct negotiation.