-
Notifications
You must be signed in to change notification settings - Fork 51
Open
Description
Refactorización de manejo de estado de usuarios
1️. Crear clase de lógica de negocio: UserStatusService.java
public class UserStatusService {
/**
* Cambia el estado del usuario según su estado actual.
* Si se agregan más estados, solo se modifica esta clase.
*/
public String toggleStatus(String currentStatus) {
switch (currentStatus.toLowerCase()) {
case "panding":
return "approved";
case "approved":
return "panding";
default:
// Por si se agregan nuevos estados
return currentStatus;
}
}
}1️. Modificar el método jTable1MouseClicked(evt) en Admin.java
//antes:
if(ck.equalsIgnoreCase("panding")){
}else{
}
//ahora:
private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {
if(evt.getClickCount() == 2) {
DefaultTableModel RecordTable = (DefaultTableModel) jTable1.getModel();
int selectedRows = jTable1.getSelectedRow();
String currentStatus = (String) RecordTable.getValueAt(selectedRows, 5);
String email = (String) RecordTable.getValueAt(selectedRows, 1);
int confirm = JOptionPane.showConfirmDialog(this, "Change status of " + currentStatus + "?");
if(confirm == JOptionPane.YES_OPTION){
try {
// Llamamos al servicio de negocio
UserStatusService statusService = new UserStatusService();
String newStatus = statusService.toggleStatus(currentStatus);
// Actualizamos en la base de datos
DatabaseConnection db = new MySQLConnection();
try (Connection con = db.getConnection();
PreparedStatement pst = con.prepareStatement("UPDATE signup SET status=? WHERE email=?")) {
pst.setString(1, newStatus);
pst.setString(2, email);
pst.executeUpdate();
}
s(); // refresca tabla
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Error updating status");
}
}
}
}Gracias a esto Admin.java ya no decide la regla de negocio, solo llama a UserStatusService, es decir, si mañana agregas otro estado o cambias la lógica de aprobación, solo necesitas modificar UserStatusService.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels