From 9c3a8b1e38dedbf16c3686fa9e4a20322906469d Mon Sep 17 00:00:00 2001 From: Daniil Date: Wed, 14 Oct 2026 20:52:15 +0300 Subject: [PATCH 1/7] feat: add UserCreate form and controller --- .../controllers/UserCreateFormController.java | 94 +++++++++++++++++++ src/main/resources/fxml/UserCreateForm.fxml | 53 +++++++++++ 2 files changed, 147 insertions(+) create mode 100644 src/main/java/com/DesktopApplicationClientJava/controllers/UserCreateFormController.java create mode 100644 src/main/resources/fxml/UserCreateForm.fxml diff --git a/src/main/java/com/DesktopApplicationClientJava/controllers/UserCreateFormController.java b/src/main/java/com/DesktopApplicationClientJava/controllers/UserCreateFormController.java new file mode 100644 index 0000000..83312d3 --- /dev/null +++ b/src/main/java/com/DesktopApplicationClientJava/controllers/UserCreateFormController.java @@ -0,0 +1,94 @@ +package com.DesktopApplicationClientJava.controllers; + +import com.DesktopApplicationClientJava.controllers.utils.Controller; +import com.DesktopApplicationClientJava.entities.Role; +import com.DesktopApplicationClientJava.entities.User; +import com.DesktopApplicationClientJava.services.ServiceException; +import com.DesktopApplicationClientJava.services.data.RegisterData; +import com.DesktopApplicationClientJava.validation.EmailValidator; +import javafx.collections.FXCollections; +import javafx.event.ActionEvent; +import javafx.fxml.FXML; +import javafx.scene.control.*; + +import java.util.Arrays; + +public class UserCreateFormController extends Controller { + + @FXML + private TextField emailField; + @FXML private PasswordField passwordField; + @FXML private Label errorLabel; + @FXML private Button submitButton; + @FXML private ChoiceBox rolesChoiceBox; + @FXML private TextField firstNameField; + @FXML private TextField lastNameField; + + @FXML + public void initialize() { + // Способ 1: через setItems + rolesChoiceBox.setItems(FXCollections.observableArrayList( + Role.USER.toString(), + Role.MODER.toString(), + Role.ADMIN.toString() + )); + // Установить значение по умолчанию (опционально) + rolesChoiceBox.setValue(Role.USER.toString()); + + System.out.println(rolesChoiceBox.getValue()); + + } + + @FXML + private void onSubmit(ActionEvent event) { + String email = emailField.getText() == null ? "" : emailField.getText().trim(); + String password = passwordField.getText() == null ? "" : passwordField.getText(); + String roleStirng = rolesChoiceBox.getValue() == null ? "" : rolesChoiceBox.getValue(); + String firstName = firstNameField.getText() == null ? "" : firstNameField.getText().trim(); + String lastName = lastNameField.getText() == null ? "" : lastNameField.getText().trim(); + + if (email.isBlank() || password.isBlank() || roleStirng.isBlank() || firstName.isBlank() || lastName.isBlank()) { + showError("Заполните все поля"); + return; + } + + if (!(roleStirng.equals(Role.USER.toString()) || roleStirng.equals(Role.MODER.toString()) + || roleStirng.equals(Role.ADMIN.toString()))) { + showError("Роль не определена"); + return; + } + + + + Role role = Role.valueOf(roleStirng); + + String emailError = EmailValidator.validate(email); + if (emailError != null) { + showError(emailError); + return; + } + + try { + RegisterData registerData = new RegisterData(email, password, role, firstName, lastName); + User user = services.getUsersService().create(registerData); + if (user == null) { + return; + } + hideError(); + } catch (ServiceException e) { + showError(e.getMessage()); + } + } + + private void showError(String message) { + errorLabel.setText(message); + errorLabel.setVisible(true); + errorLabel.setManaged(true); + } + + private void hideError() { + errorLabel.setText(""); + errorLabel.setVisible(false); + errorLabel.setManaged(false); + } +} diff --git a/src/main/resources/fxml/UserCreateForm.fxml b/src/main/resources/fxml/UserCreateForm.fxml new file mode 100644 index 0000000..752ce6a --- /dev/null +++ b/src/main/resources/fxml/UserCreateForm.fxml @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + + + + + +