Overview
TheReservasController handles all CRUD operations for reservation management in the system. It provides endpoints for listing, creating, updating, deleting, and searching reservations.
Namespace: capa_presentacion.Controllers
Base Route: /Reservas
Methods
ListarReservas
Retrieves a list of all reservations in the system.HTTP Method
GET
Route
/Reservas/ListarReservas
Returns
Type:ActionResult
- Success (200): Returns a view with
List<CE_Reservas>containing all reservations - Error: Returns a view with an empty list and sets error message in
TempData["ErrorMessage"]
Response Model
Error Handling
- Validates
ModelStatebefore processing - Catches exceptions and stores error message in
TempData - Returns empty list on error instead of throwing
Usage Example
Source
Controllers/ReservasController.cs:16
InsertarReservas (GET)
Displays the form for creating a new reservation with dropdown lists for clients and canchas.HTTP Method
GET
Route
/Reservas/InsertarReservas
Returns
Type:ActionResult
Returns a view with ReservaViewModel containing:
Reserva: EmptyCE_ReservasobjectListaClientes: List of all available clientsListaCanchas: List of all available sports fields
View Model
Usage Example
Notes
- Initializes empty
CE_Reservasobject to prevent null reference errors - Loads client and cancha lists from business layer
- Uses ViewModel pattern to pass multiple data collections to view
Source
Controllers/ReservasController.cs:35
InsertarReservas (POST)
Creates a new reservation in the system.HTTP Method
POST
Route
/Reservas/InsertarReservas
Parameters
The reservation view model containing the reservation dataProperties:
Reserva(CE_Reservas): The reservation object with the following fields:IdCancha(int): ID of the sports fieldIdCliente(int): ID of the client making the reservationIdUsuario(int): ID of the user creating the reservationFechaReserva(DateTime): Date of the reservationHoraInicio(TimeSpan): Start timeHoraFin(TimeSpan): End timeComentario(string): Optional commentsEstado(bool): Active status
Returns
Type:ActionResult
- Success (302): Redirects to
ListarReservasaction - Error (404): Reserva object is null - “No se encontro el modelo”
- Error (500): Exception during creation - “Error al agregar la reserva: “
Error Responses
Reserva object is null
Error during reservation creation
Usage Example
Notes
- Validates that
reservas.Reservais not null instead of usingModelState.IsValid - Previous implementation using
CE_Reservasdirectly is commented out (lines 51-70) - Uses ViewModel approach for better integration with dropdown-based forms
Source
Controllers/ReservasController.cs:74
Actualizar (GET)
Retrieves a specific reservation for editing.HTTP Method
GET
Route
/Reservas/Actualizar/{id}
Parameters
The unique identifier of the reservation to update
Returns
Type:ActionResult
- Success (200): Returns view with the
CE_Reservasobject - Error (404): Reservation not found - “No se pudo actualizar el cliente con el id: “
Error Responses
Reservation with specified ID not found
Usage Example
Notes
- Error message references “cliente” instead of “reserva” (copy-paste issue)
Source
Controllers/ReservasController.cs:94
Actualizar (POST)
Updates an existing reservation.HTTP Method
POST
Route
/Reservas/Actualizar
Parameters
The reservation object with updated informationProperties:
IdReserva(int): ID of the reservation to updateIdCancha(int): Updated sports field IDIdCliente(int): Updated client IDIdUsuario(int): User IDFechaReserva(DateTime): Updated dateHoraInicio(TimeSpan): Updated start timeHoraFin(TimeSpan): Updated end timeComentario(string): Updated commentsEstado(bool): Updated status
Returns
Type:ActionResult
- Success (302): Redirects to
ListarReservasaction - Error (500): Exception during update - “Error al actualizar la cancha: “
Error Responses
Error during reservation update
Usage Example
Notes
- The method updates the reservation regardless of
ModelState.IsValidstatus (lines 110-116) - Both validation paths lead to the same update operation
- Error message references “cancha” instead of “reserva” (copy-paste issue)
- Missing
[HttpPost]attribute but behaves as POST method
Source
Controllers/ReservasController.cs:106
Eliminar
Deletes a reservation from the system.HTTP Method
POST (inferred from usage pattern)
Route
/Reservas/Eliminar
Parameters
The unique identifier of the reservation to delete
Returns
Type:ActionResult
- Success (302): Redirects to
ListarReservasaction - Error (500): Exception during deletion - “Error al eliminar la reserva: “
Error Responses
Error during reservation deletion
Usage Example
Notes
- Includes try-catch error handling
- Returns appropriate error message to user
- Missing
[HttpPost]attribute
Source
Controllers/ReservasController.cs:130
BuscarReservaNombre (GET)
Displays the search form for finding reservations by client name.HTTP Method
GET
Route
/Reservas/BuscarReservaNombre
Returns
Type:ActionResult
Returns the view for searching reservations.
Usage Example
Source
Controllers/ReservasController.cs:147
BuscarReservaNombre (POST)
Searches for reservations by client name.HTTP Method
POST
Route
/Reservas/BuscarReservaNombre
Parameters
The client name to search for (partial match supported)
- If empty or null: Returns all reservations
- If provided: Returns filtered reservations matching the name
Returns
Type:ActionResult
Success (200): Returns view with List<CE_Reservas> containing:
- All reservations if search term is empty
- Filtered reservations matching the client name if search term provided
Usage Example
Search Behavior
Notes
- No error handling implemented
- Returns view (not redirect) to maintain search state
- Searches by client name using
Reservas.ListarNombre()
Source
Controllers/ReservasController.cs:155
Dependencies
- Business Layer:
CN_Reservasfromcapa_negocioCN_Clientesfromcapa_negocioCN_Canchasfromcapa_negocio
- Entity Layer:
CE_Reservasfromcapa_entidadCE_Clientesfromcapa_entidadCE_Canchasfromcapa_entidad
- View Models:
ReservaViewModelfromcapa_presentacion.Models - Framework: ASP.NET Core MVC
Error Handling Strategy
- Model Validation: Checks
ModelState.IsValidor null validation before processing requests - Try-Catch Blocks: Wraps operations in exception handlers (most methods)
- User Feedback: Uses
TempDatafor error messages in views - Status Codes: Returns appropriate HTTP status codes (404, 500)
- Graceful Degradation: Returns empty lists instead of error pages when listing fails
Design Patterns
- ViewModel Pattern: Uses
ReservaViewModelto combine multiple entities for form dropdowns - Repository Pattern: Delegates data access to business layer (
CN_Reservas) - PRG Pattern: Post-Redirect-Get pattern after successful mutations
Known Issues
- Error Messages: Several methods reference wrong entity types in error messages (“cliente”, “cancha” instead of “reserva”)
- Missing Attributes: Some POST methods missing
[HttpPost]attribute - Inconsistent Validation: Mix of
ModelState.IsValidchecks and null checks - Redundant Code: Update methods execute regardless of validation result (lines 110-116)