The User Management system handles operator accounts that access the Canchas Deportivas platform. Users are the internal staff who manage fields, clients, and reservations.
public class CE_usuarios{ public int IdUsuario { get; set; } public string? Nombre { get; set; } public string? Clave { get; set; } public bool Estado { get; set; }}
The entity stores user credentials with a password field (Clave) and active status tracking.
The business layer provides validation and CRUD operations:
capa_negocio/CN_Usuarios.cs
public class CN_Usuarios{ CD_Usuarios oCD_Usuarios = new CD_Usuarios(); public List<CE_usuarios> Listar() { try { return oCD_Usuarios.ListarUsuarios(); } catch (Exception ex) { throw new Exception("Error al obtener la lista de usuarios: " + ex.Message); } }}
The Insertar method includes comprehensive validation:
capa_negocio/CN_Usuarios.cs
public void Insertar(CE_usuarios usuario){ // Validate username if (string.IsNullOrWhiteSpace(usuario.Nombre)) { throw new ArgumentException("El nombre de usuario no puede estar vacío."); } // Validate password exists if (string.IsNullOrWhiteSpace(usuario.Clave)) { throw new ArgumentException("La contraseña es obligatoria."); } // Validate password length if (usuario.Clave.Length < 6) { throw new ArgumentException("La contraseña debe tener al menos 6 caracteres."); } try { oCD_Usuarios.InsertarUsuarios(usuario); } catch (Exception ex) { throw new Exception("Error al insertar el usuario: " + ex.Message); }}
Password validation requires a minimum of 6 characters. Consider implementing additional security measures like password hashing in production.
The update method validates the user ID and username:
capa_negocio/CN_Usuarios.cs
public void Actualizar(CE_usuarios usuario){ // Validate user ID if (usuario.IdUsuario <= 0) { throw new ArgumentException("Se requiere un ID de Usuario válido para actualizar."); } // Validate username if (string.IsNullOrWhiteSpace(usuario.Nombre)) { throw new ArgumentException("El nombre de usuario no puede estar vacío."); } try { oCD_Usuarios.ActualizarUsuarios(usuario); } catch (Exception ex) { throw new Exception("Error al actualizar el usuario: " + ex.Message); }}
The update method does not re-validate password length, allowing users to keep their existing passwords.
public void Eliminar(int idUsuario){ // Validate user ID if (idUsuario <= 0) { throw new ArgumentException("Se requiere un ID de Usuario para eliminar."); } try { oCD_Usuarios.EliminarUsuarios(idUsuario); } catch (Exception ex) { throw new Exception("Error al eliminar el usuario: " + ex.Message); }}
Be cautious when deleting users who may have created reservations. Consider soft deletion or status changes instead.
Users are tracked in the reservation system through the IdUsuario field:
capa_entidad/CE_Reservas.cs
public class CE_Reservas{ public int IdReserva { get; set; } public int IdCancha { get; set; } public int IdCliente { get; set; } public int IdUsuario { get; set; } // Operator who created the reservation // ... other properties}
In the reservation form, the user ID is typically set to the logged-in operator:
All user management operations follow a consistent error handling pattern:
Try-Catch Wrapper
Validation Exceptions
List Operations
try{ // Perform operation oCD_Usuarios.InsertarUsuarios(usuario);}catch (Exception ex){ throw new Exception("Error al insertar el usuario: " + ex.Message);}
if (usuario.IdUsuario <= 0){ throw new ArgumentException("Se requiere un ID de Usuario válido.");}
public List<CE_usuarios> Listar(){ try { return oCD_Usuarios.ListarUsuarios(); } catch (Exception ex) { throw new Exception("Error al obtener la lista de usuarios: " + ex.Message); }}