diff --git a/src/main/java/org/example/petshopdesktop/api/endpoints/EmployeeApi.java b/src/main/java/org/example/petshopdesktop/api/endpoints/EmployeeApi.java index 6cc81e29..e3a8ad52 100644 --- a/src/main/java/org/example/petshopdesktop/api/endpoints/EmployeeApi.java +++ b/src/main/java/org/example/petshopdesktop/api/endpoints/EmployeeApi.java @@ -41,4 +41,8 @@ public class EmployeeApi { public EmployeeResponse createEmployee(EmployeeRequest request) throws Exception { return apiClient.post("/api/v1/employees", request, EmployeeResponse.class); } + + public EmployeeResponse updateEmployee(Long id, EmployeeRequest request) throws Exception { + return apiClient.put("/api/v1/employees/" + id, request, EmployeeResponse.class); + } } diff --git a/src/main/java/org/example/petshopdesktop/controllers/StaffAccountsController.java b/src/main/java/org/example/petshopdesktop/controllers/StaffAccountsController.java index 746b3361..ffb67aa9 100644 --- a/src/main/java/org/example/petshopdesktop/controllers/StaffAccountsController.java +++ b/src/main/java/org/example/petshopdesktop/controllers/StaffAccountsController.java @@ -59,6 +59,9 @@ public class StaffAccountsController { @FXML private Button btnCreateAccount; + @FXML + private Button btnEditAccount; + private final ObservableList staffAccounts = FXCollections.observableArrayList(); private FilteredList filtered; @@ -76,13 +79,26 @@ public class StaffAccountsController { txtSearch.textProperty().addListener((obs, o, n) -> applyFilter(n)); + tvStaff.getSelectionModel().selectedItemProperty().addListener((obs, oldValue, newValue) -> { + if (btnEditAccount != null) { + btnEditAccount.setDisable(newValue == null); + } + }); + if (!UserSession.getInstance().isAdmin()) { lblError.setText("Access restricted."); tvStaff.setDisable(true); btnCreateAccount.setDisable(true); + if (btnEditAccount != null) { + btnEditAccount.setDisable(true); + } return; } + if (btnEditAccount != null) { + btnEditAccount.setDisable(true); + } + refresh(); } @@ -110,6 +126,32 @@ public class StaffAccountsController { } } + @FXML + void btnEditAccountClicked(ActionEvent event) { + lblError.setText(""); + StaffAccount selected = tvStaff.getSelectionModel().getSelectedItem(); + if (selected == null) { + lblError.setText("Select a staff account to edit."); + return; + } + try { + FXMLLoader loader = new FXMLLoader(getClass().getResource("/org/example/petshopdesktop/dialogviews/staff-edit-dialog-view.fxml")); + Stage dialog = new Stage(); + dialog.initOwner(tvStaff.getScene().getWindow()); + dialog.initModality(Modality.APPLICATION_MODAL); + dialog.setTitle("Edit Staff Account"); + dialog.setScene(new Scene(loader.load())); + dialog.setResizable(false); + var controller = (org.example.petshopdesktop.controllers.dialogcontrollers.StaffEditDialogController) loader.getController(); + controller.setStaffAccount(selected); + dialog.showAndWait(); + refresh(); + } catch (Exception e) { + ActivityLogger.getInstance().logException("StaffAccountsController.btnEditAccountClicked", e, "Opening staff edit dialog"); + lblError.setText("Could not open staff account editor."); + } + } + private void refresh() { lblError.setText(""); tvStaff.setDisable(true); @@ -145,12 +187,13 @@ public class StaffAccountsController { String lastName = names[1]; String email = employee.getEmail() != null ? employee.getEmail() : ""; String phone = employee.getPhone() != null ? employee.getPhone() : ""; + String role = employee.getRole() != null ? employee.getRole() : "STAFF"; boolean active = employee.getActive() != null ? employee.getActive() : false; Timestamp createdAt = employee.getCreatedAt() != null ? Timestamp.from(employee.getCreatedAt().atZone(ZoneId.systemDefault()).toInstant()) : null; - return new StaffAccount(userId, employeeId, username, firstName, lastName, email, phone, active, createdAt); + return new StaffAccount(userId, employeeId, username, firstName, lastName, email, phone, role, active, createdAt); } private String[] splitFullName(String fullName) { diff --git a/src/main/java/org/example/petshopdesktop/controllers/dialogcontrollers/StaffEditDialogController.java b/src/main/java/org/example/petshopdesktop/controllers/dialogcontrollers/StaffEditDialogController.java new file mode 100644 index 00000000..5ed6316e --- /dev/null +++ b/src/main/java/org/example/petshopdesktop/controllers/dialogcontrollers/StaffEditDialogController.java @@ -0,0 +1,144 @@ +package org.example.petshopdesktop.controllers.dialogcontrollers; + +import javafx.application.Platform; +import javafx.event.ActionEvent; +import javafx.fxml.FXML; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.control.PasswordField; +import javafx.scene.control.TextField; +import javafx.stage.Stage; +import org.example.petshopdesktop.Validator; +import org.example.petshopdesktop.api.dto.employee.EmployeeRequest; +import org.example.petshopdesktop.api.endpoints.EmployeeApi; +import org.example.petshopdesktop.models.StaffAccount; +import org.example.petshopdesktop.util.ActivityLogger; + +public class StaffEditDialogController { + + @FXML + private TextField txtFirstName; + + @FXML + private TextField txtLastName; + + @FXML + private TextField txtEmail; + + @FXML + private TextField txtPhone; + + @FXML + private TextField txtUsername; + + @FXML + private PasswordField txtPassword; + + @FXML + private PasswordField txtPasswordConfirm; + + @FXML + private Label lblError; + + @FXML + private Button btnSave; + + private StaffAccount staffAccount; + + public void setStaffAccount(StaffAccount staffAccount) { + this.staffAccount = staffAccount; + txtFirstName.setText(staffAccount.getFirstName()); + txtLastName.setText(staffAccount.getLastName()); + txtEmail.setText(staffAccount.getEmail()); + txtPhone.setText(staffAccount.getPhone()); + txtUsername.setText(staffAccount.getUsername()); + } + + @FXML + void btnSaveClicked(ActionEvent event) { + lblError.setText(""); + if (staffAccount == null) { + lblError.setText("No staff account selected."); + return; + } + + String firstName = value(txtFirstName); + String lastName = value(txtLastName); + String email = value(txtEmail); + String phone = value(txtPhone); + String username = value(txtUsername); + String password = txtPassword.getText() == null ? "" : txtPassword.getText().trim(); + String confirm = txtPasswordConfirm.getText() == null ? "" : txtPasswordConfirm.getText().trim(); + + if (firstName.isBlank() || lastName.isBlank()) { + lblError.setText("First name and last name are required."); + return; + } + if (email.isBlank()) { + lblError.setText("Email is required."); + return; + } + if (phone.isBlank()) { + lblError.setText("Phone is required."); + return; + } + String phoneError = Validator.isValidPhoneNumber(phone, "Phone"); + if (!phoneError.isEmpty()) { + lblError.setText(phoneError.trim()); + return; + } + if (username.isBlank()) { + lblError.setText("Username is required."); + return; + } + if (!password.isEmpty() && password.length() < 6) { + lblError.setText("Password must be at least 6 characters."); + return; + } + if (!password.equals(confirm)) { + lblError.setText("Passwords do not match."); + return; + } + + btnSave.setDisable(true); + + new Thread(() -> { + try { + EmployeeRequest request = new EmployeeRequest(); + request.setUsername(username); + request.setPassword(password.isEmpty() ? null : password); + request.setFirstName(firstName); + request.setLastName(lastName); + request.setEmail(email); + request.setPhone(phone); + request.setRole(staffAccount.getRole()); + request.setActive(staffAccount.isActive()); + + EmployeeApi.getInstance().updateEmployee(staffAccount.getEmployeeId(), request); + + Platform.runLater(this::close); + } catch (Exception e) { + ActivityLogger.getInstance().logException("StaffEditDialogController.btnSaveClicked", e, "Updating staff account"); + String msg = e.getMessage() == null ? "Could not update staff account." : e.getMessage(); + Platform.runLater(() -> { + lblError.setText(msg); + btnSave.setDisable(false); + }); + } + }).start(); + } + + @FXML + void btnCancelClicked(ActionEvent event) { + close(); + } + + private void close() { + Stage stage = (Stage) btnSave.getScene().getWindow(); + stage.close(); + } + + private static String value(TextField tf) { + return tf.getText() == null ? "" : tf.getText().trim(); + } +} diff --git a/src/main/java/org/example/petshopdesktop/models/StaffAccount.java b/src/main/java/org/example/petshopdesktop/models/StaffAccount.java index e1b0ab62..59923338 100644 --- a/src/main/java/org/example/petshopdesktop/models/StaffAccount.java +++ b/src/main/java/org/example/petshopdesktop/models/StaffAccount.java @@ -10,10 +10,11 @@ public class StaffAccount { private final String lastName; private final String email; private final String phone; + private final String role; private final boolean active; private final Timestamp createdAt; - public StaffAccount(long userId, long employeeId, String username, String firstName, String lastName, String email, String phone, boolean active, Timestamp createdAt) { + public StaffAccount(long userId, long employeeId, String username, String firstName, String lastName, String email, String phone, String role, boolean active, Timestamp createdAt) { this.userId = userId; this.employeeId = employeeId; this.username = username; @@ -21,6 +22,7 @@ public class StaffAccount { this.lastName = lastName; this.email = email; this.phone = phone; + this.role = role; this.active = active; this.createdAt = createdAt; } @@ -59,6 +61,10 @@ public class StaffAccount { return phone; } + public String getRole() { + return role; + } + public boolean isActive() { return active; } diff --git a/src/main/resources/org/example/petshopdesktop/dialogviews/staff-edit-dialog-view.fxml b/src/main/resources/org/example/petshopdesktop/dialogviews/staff-edit-dialog-view.fxml new file mode 100644 index 00000000..354ea20d --- /dev/null +++ b/src/main/resources/org/example/petshopdesktop/dialogviews/staff-edit-dialog-view.fxml @@ -0,0 +1,96 @@ + + + + + + + + + + + + + + + + + + + +