Skip to content

Commit bed8b8c

Browse files
Implementando lógica de cadastrar usuário
1 parent 4b9175c commit bed8b8c

File tree

2 files changed

+162
-19
lines changed

2 files changed

+162
-19
lines changed

src/main/java/com/casaculturaqxd/sgec/controller/CadastrarController.java

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,124 @@
11
package com.casaculturaqxd.sgec.controller;
22

33
import java.io.IOException;
4+
import java.sql.SQLException;
5+
46
import com.casaculturaqxd.sgec.App;
7+
import com.casaculturaqxd.sgec.DAO.UserDAO;
8+
import com.casaculturaqxd.sgec.jdbc.Database;
9+
import com.casaculturaqxd.sgec.jdbc.DatabasePostgres;
10+
import com.casaculturaqxd.sgec.models.User;
11+
512
import javafx.fxml.FXML;
13+
import javafx.scene.control.Alert;
14+
import javafx.scene.control.Alert.AlertType;
15+
import javafx.scene.control.Button;
16+
import javafx.scene.control.PasswordField;
17+
import javafx.scene.control.TextField;
618

719
public class CadastrarController {
820

21+
@FXML
22+
private TextField nomeUsuario;
23+
@FXML
24+
private TextField email;
25+
@FXML
26+
private PasswordField senha;
27+
@FXML
28+
private PasswordField confirmarSenha;
29+
@FXML
30+
private Button signupButton;
31+
32+
private Alert alerta;
33+
private User usuario;
34+
private UserDAO userDAO;
35+
36+
private final Database userConnection = DatabasePostgres.getInstance("URL", "USER_NAME", "PASSWORD");
37+
38+
/**
39+
* Carrega a página com o botão de cadastro desabilitado
40+
*/
941
public void initialize() {
42+
userDAO = new UserDAO(userConnection.getConnection());
43+
alerta = new Alert(AlertType.NONE);
44+
signupButton.setDisable(true);
45+
}
46+
47+
/**
48+
* Cadastra um novo usuário no banco de dados após validar os dados
49+
*
50+
* @throws IOException
51+
* @throws SQLException
52+
*/
53+
@FXML
54+
public void cadastrarUsuario() throws IOException, SQLException {
55+
String inputNome = nomeUsuario.getText();
56+
String inputEmail = email.getText();
57+
String inputSenha = senha.getText();
58+
String inputConfirmarSenha = confirmarSenha.getText();
59+
60+
// Validação: verifica se a senha e a confirmação correspondem
61+
if (!inputSenha.equals(inputConfirmarSenha)) {
62+
alerta.setAlertType(AlertType.ERROR);
63+
alerta.setContentText("As senhas não correspondem");
64+
alerta.show();
65+
return;
66+
}
1067

68+
try {
69+
// Verifica se o usuário já existe
70+
if (userDAO.usuarioExists(inputEmail)) {
71+
alerta.setAlertType(AlertType.ERROR);
72+
alerta.setContentText("Já existe um usuário cadastrado com este e-mail");
73+
alerta.show();
74+
} else {
75+
// Cria novo usuário (editor = false por padrão)
76+
usuario = new User(inputNome, inputEmail, inputSenha, false);
77+
78+
// Insere no banco de dados
79+
boolean sucesso = userDAO.inserir(usuario);
80+
81+
if (sucesso) {
82+
alerta.setAlertType(AlertType.INFORMATION);
83+
alerta.setContentText("Usuário cadastrado com sucesso!");
84+
alerta.show();
85+
86+
// Redireciona para a tela de login
87+
App.setRoot("view/login");
88+
} else {
89+
alerta.setAlertType(AlertType.ERROR);
90+
alerta.setContentText("Falha ao cadastrar usuário");
91+
alerta.show();
92+
}
93+
}
94+
} catch (SQLException e) {
95+
alerta.setAlertType(AlertType.ERROR);
96+
alerta.setContentText("Falha realizando cadastro de usuário");
97+
alerta.show();
98+
}
99+
}
100+
101+
/**
102+
* Propriedade que mantém o botão desativado enquanto todos os campos não são
103+
* preenchidos
104+
*/
105+
@FXML
106+
public void keyReleasedProperty() {
107+
String inputNome = nomeUsuario.getText();
108+
String inputEmail = email.getText();
109+
String inputSenha = senha.getText();
110+
String inputConfirmarSenha = confirmarSenha.getText();
111+
112+
boolean isDisable = inputNome.isEmpty() || inputEmail.isEmpty() ||
113+
inputSenha.isEmpty() || inputConfirmarSenha.isEmpty();
114+
signupButton.setDisable(isDisable);
11115
}
12116

13117
@FXML
14118
public void back() {
15119
try {
16120
App.setRoot("view/login");
17121
} catch (IOException e) {
18-
// TODO Auto-generated catch block
19122
e.printStackTrace();
20123
}
21124
}

src/main/resources/com/casaculturaqxd/sgec/view/cadastro.fxml

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,35 @@
1212
<?import javafx.scene.layout.VBox?>
1313
<?import javafx.scene.text.Font?>
1414

15-
<VBox fx:controller="com.casaculturaqxd.sgec.controller.CadastrarController" id="pageCadastro" alignment="CENTER" maxHeight="1024.0" maxWidth="1440.0" minHeight="-Infinity" minWidth="-Infinity" prefHeight="500.0" prefWidth="600.0" stylesheets="@../css/page/cadastroStyle.css" xmlns="http://javafx.com/javafx/20.0.1" xmlns:fx="http://javafx.com/fxml/1">
15+
<VBox fx:controller="com.casaculturaqxd.sgec.controller.CadastrarController" id="pageCadastro"
16+
alignment="CENTER" maxHeight="1024.0" maxWidth="1440.0" minHeight="-Infinity"
17+
minWidth="-Infinity" prefHeight="500.0" prefWidth="600.0"
18+
stylesheets="@../css/page/cadastroStyle.css" xmlns="http://javafx.com/javafx/20.0.1"
19+
xmlns:fx="http://javafx.com/fxml/1">
1620
<children>
17-
<AnchorPane maxHeight="600.0" maxWidth="800.0" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" VBox.vgrow="ALWAYS">
21+
<AnchorPane maxHeight="600.0" maxWidth="800.0" minHeight="-Infinity" minWidth="-Infinity"
22+
prefHeight="400.0" prefWidth="600.0" VBox.vgrow="ALWAYS">
1823
<VBox.margin>
1924
<Insets />
2025
</VBox.margin>
2126
<children>
22-
<VBox id="camposCadastro" fx:id="confirmeSenha" alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0" prefWidth="450.0" spacing="30.0" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="100.0" AnchorPane.rightAnchor="100.0" AnchorPane.topAnchor="40.0">
27+
<VBox id="camposCadastro" fx:id="confirmeSenha" alignment="CENTER" maxHeight="-Infinity"
28+
maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0"
29+
prefWidth="450.0" spacing="30.0" AnchorPane.bottomAnchor="10.0"
30+
AnchorPane.leftAnchor="100.0" AnchorPane.rightAnchor="100.0"
31+
AnchorPane.topAnchor="40.0">
2332
<children>
2433
<VBox>
2534
<VBox.margin>
2635
<Insets left="20.0" right="20.0" top="40.0" />
2736
</VBox.margin>
2837
<children>
29-
<HBox maxHeight="-Infinity" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="20.0" prefWidth="300.0">
38+
<HBox maxHeight="-Infinity" maxWidth="1.7976931348623157E308"
39+
minHeight="-Infinity" minWidth="-Infinity" prefHeight="20.0"
40+
prefWidth="300.0">
3041
<children>
31-
<Label fx:id="nomeUsuario" prefWidth="122.0" styleClass="texto" text="Nome de usuário">
42+
<Label prefWidth="122.0" styleClass="texto"
43+
text="Nome de usuário">
3244
<HBox.margin>
3345
<Insets left="20.0" top="-5.0" />
3446
</HBox.margin>
@@ -38,7 +50,10 @@
3850
</Label>
3951
</children>
4052
</HBox>
41-
<TextField id="nomeUsuario" maxHeight="-Infinity" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="40.0" prefWidth="300.0">
53+
<TextField fx:id="nomeUsuario" id="nomeUsuario"
54+
onKeyReleased="#keyReleasedProperty" maxHeight="-Infinity"
55+
maxWidth="1.7976931348623157E308" minHeight="-Infinity"
56+
minWidth="-Infinity" prefHeight="40.0" prefWidth="300.0">
4257
<VBox.margin>
4358
<Insets left="20.0" right="20.0" />
4459
</VBox.margin>
@@ -50,9 +65,12 @@
5065
<Insets left="20.0" right="20.0" />
5166
</VBox.margin>
5267
<children>
53-
<HBox maxHeight="-Infinity" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="20.0" prefWidth="300.0">
68+
<HBox maxHeight="-Infinity" maxWidth="1.7976931348623157E308"
69+
minHeight="-Infinity" minWidth="-Infinity" prefHeight="20.0"
70+
prefWidth="300.0">
5471
<children>
55-
<Label fx:id="email" maxWidth="1.7976931348623157E308" styleClass="texto" text="E-mail">
72+
<Label maxWidth="1.7976931348623157E308"
73+
styleClass="texto" text="E-mail">
5674
<HBox.margin>
5775
<Insets left="20.0" top="-5.0" />
5876
</HBox.margin>
@@ -62,7 +80,10 @@
6280
</Label>
6381
</children>
6482
</HBox>
65-
<TextField fx:id="email" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="40.0" prefWidth="300.0">
83+
<TextField fx:id="email" onKeyReleased="#keyReleasedProperty"
84+
maxHeight="1.7976931348623157E308"
85+
maxWidth="1.7976931348623157E308" minHeight="-Infinity"
86+
minWidth="-Infinity" prefHeight="40.0" prefWidth="300.0">
6687
<VBox.margin>
6788
<Insets left="20.0" right="20.0" />
6889
</VBox.margin>
@@ -74,7 +95,9 @@
7495
<Insets left="20.0" right="20.0" />
7596
</VBox.margin>
7697
<children>
77-
<HBox maxHeight="-Infinity" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="20.0" prefWidth="300.0">
98+
<HBox maxHeight="-Infinity" maxWidth="1.7976931348623157E308"
99+
minHeight="-Infinity" minWidth="-Infinity" prefHeight="20.0"
100+
prefWidth="300.0">
78101
<children>
79102
<Label styleClass="texto" text="Senha">
80103
<HBox.margin>
@@ -86,7 +109,10 @@
86109
</Label>
87110
</children>
88111
</HBox>
89-
<PasswordField fx:id="senha" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="40.0" prefWidth="300.0">
112+
<PasswordField fx:id="senha" onKeyReleased="#keyReleasedProperty"
113+
maxHeight="1.7976931348623157E308"
114+
maxWidth="1.7976931348623157E308" minHeight="-Infinity"
115+
minWidth="-Infinity" prefHeight="40.0" prefWidth="300.0">
90116
<VBox.margin>
91117
<Insets left="20.0" right="20.0" />
92118
</VBox.margin>
@@ -98,12 +124,16 @@
98124
<Insets left="20.0" right="20.0" />
99125
</VBox.margin>
100126
<children>
101-
<HBox maxHeight="-Infinity" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="20.0" prefWidth="300.0">
127+
<HBox maxHeight="-Infinity" maxWidth="1.7976931348623157E308"
128+
minHeight="-Infinity" minWidth="-Infinity" prefHeight="20.0"
129+
prefWidth="300.0">
102130
<VBox.margin>
103131
<Insets left="20.0" />
104132
</VBox.margin>
105133
<children>
106-
<Label maxHeight="-Infinity" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="20.0" prefWidth="127.0" styleClass="texto" text="Confirme a senha">
134+
<Label maxHeight="-Infinity" maxWidth="1.7976931348623157E308"
135+
minHeight="-Infinity" minWidth="-Infinity" prefHeight="20.0"
136+
prefWidth="127.0" styleClass="texto" text="Confirme a senha">
107137
<font>
108138
<Font size="16.0" />
109139
</font>
@@ -113,16 +143,24 @@
113143
</Label>
114144
</children>
115145
</HBox>
116-
<PasswordField fx:id="confirmarSenha" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="40.0" prefWidth="300.0">
146+
<PasswordField fx:id="confirmarSenha" onKeyReleased="#keyReleasedProperty"
147+
maxHeight="1.7976931348623157E308"
148+
maxWidth="1.7976931348623157E308" minHeight="-Infinity"
149+
minWidth="-Infinity" prefHeight="40.0" prefWidth="300.0">
117150
<VBox.margin>
118151
<Insets left="20.0" right="20.0" />
119152
</VBox.margin>
120153
</PasswordField>
121154
</children>
122155
</VBox>
123-
<VBox alignment="CENTER" maxHeight="1.7976931348623157E308" prefHeight="29.0" prefWidth="400.0" VBox.vgrow="ALWAYS">
156+
<VBox alignment="CENTER" maxHeight="1.7976931348623157E308" prefHeight="29.0"
157+
prefWidth="400.0" VBox.vgrow="ALWAYS">
124158
<children>
125-
<Button id="botao" fx:id="botaoCadastrar" alignment="CENTER" contentDisplay="TOP" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="30.0" prefWidth="300.0" text="Cadastrar-se">
159+
<Button id="botao" fx:id="signupButton" alignment="CENTER"
160+
contentDisplay="TOP" maxHeight="-Infinity" maxWidth="-Infinity"
161+
minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false"
162+
onAction="#cadastrarUsuario" defaultButton="true" prefHeight="30.0"
163+
prefWidth="300.0" text="Cadastrar-se">
126164
<VBox.margin>
127165
<Insets bottom="10.0" />
128166
</VBox.margin>
@@ -131,7 +169,8 @@
131169
</VBox>
132170
</children>
133171
</VBox>
134-
<VBox AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
172+
<VBox AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"
173+
AnchorPane.topAnchor="0.0">
135174
<children>
136175
<HBox alignment="CENTER" prefHeight="30.0" prefWidth="325.0">
137176
<children>
@@ -144,7 +183,8 @@
144183
</HBox>
145184
</children>
146185
</VBox>
147-
<ImageView fx:id="backLogin" onMouseClicked="#back" fitHeight="34.0" fitWidth="24.0" layoutX="10.0" layoutY="10.0" pickOnBounds="true" preserveRatio="true">
186+
<ImageView fx:id="backLogin" onMouseClicked="#back" fitHeight="34.0" fitWidth="24.0"
187+
layoutX="10.0" layoutY="10.0" pickOnBounds="true" preserveRatio="true">
148188
<image>
149189
<Image url="@../imagens/left-arrow_271218.png" />
150190
</image>

0 commit comments

Comments
 (0)