diff --git a/src/main/java/NTNU/IDATT1002/controllers/SignUp.java b/src/main/java/NTNU/IDATT1002/controllers/SignUp.java index 07d2c1e2eec7117fd84f0d1853e9ae77daff5d99..43bf425f8ba93b008a5710b7dab54be3ea13b53a 100644 --- a/src/main/java/NTNU/IDATT1002/controllers/SignUp.java +++ b/src/main/java/NTNU/IDATT1002/controllers/SignUp.java @@ -6,24 +6,17 @@ import java.time.LocalDate; import java.time.ZoneId; import java.util.ArrayList; import java.util.Date; -import java.util.Optional; import NTNU.IDATT1002.App; import NTNU.IDATT1002.service.UserService; +import java.util.regex.Pattern; import javafx.event.ActionEvent; import javafx.scene.control.Button; import javafx.scene.control.DatePicker; import javafx.scene.control.PasswordField; import javafx.scene.control.TextField; import javafx.scene.layout.GridPane; -import javafx.scene.text.Text; - import javax.persistence.EntityManager; -import java.io.IOException; -import java.time.Instant; -import java.time.LocalDate; -import java.time.ZoneId; -import java.util.Date; /** * Controls the buttons and changeable elements on signup.fxml, @@ -42,10 +35,9 @@ public class SignUp { public TextField signup_phoneNr; public DatePicker signup_birthDate; - public Text signup_error; public UserService userService; - public Button signup_btn; + private boolean check; public SignUp() { EntityManager entityManager = App.ex.getEntityManager(); @@ -74,34 +66,98 @@ public class SignUp { signupFields.add(signup_password); signupFields.add(signup_phoneCode); signupFields.add(signup_phoneNr); - boolean blank = false; - for (TextField signupField : signupFields) { - if (signupField.getText().trim().isEmpty()){ - signupField.setPromptText("*"); - signupField.setStyle("-fx-prompt-text-fill: red"); - blank = true; - } - } + LocalDate birthLocalDate = signup_birthDate.getValue(); + + + + if (validateInfo(username, firstName, lastName, email, password, phoneCode, phoneNr, birthLocalDate)) { + { + Instant instant = Instant.from(birthLocalDate.atStartOfDay(ZoneId.systemDefault())); + Date birthDate = Date.from(instant); - if (signup_birthDate.getValue() == null){ - signup_birthDate.setPromptText("*"); - signup_error.setText("* fields required to sign up"); + + if (userService.createUser(email, username, firstName, lastName, phoneCode, phoneNr, birthDate, password).isPresent()) { + //TODO: Return message to user to confirm that user has been succsessfully registered + App.setRoot("login"); + } + }} } - else if (blank) { - signup_error.setText("* fields required to sign up"); + + public void cancel (ActionEvent event) throws IOException { + App.setRoot("login"); } - else{ - LocalDate birthLocalDate = signup_birthDate.getValue(); - Instant instant = Instant.from(birthLocalDate.atStartOfDay(ZoneId.systemDefault())); - Date birthDate = Date.from(instant); - if(userService.createUser(email, username, firstName, lastName, phoneCode, phoneNr, birthDate, password).isPresent()) { - //TODO: Return message to user to confirm that user has been succsessfully registered - App.setRoot("login"); + + + /** + * Checks both that the user put info in the necessary textfields and that the username and/or email isnt in use. + * + * @param username + * @param firstName + * @param lastName + * @param email + * @param password + * @param phoneCode + * @param phoneNR + * @param birthdate + * + */ + private boolean validateInfo (String username, String firstName, String lastName, String email, String password, String phoneCode, String phoneNR, LocalDate birthdate){ + check = true; + + userService.getUsers().stream().forEach(x -> { + if (x.getUsername().equals(username)) { + signup_username.clear(); + signup_username.setStyle("-fx-prompt-text-fill: red"); + signup_username.setPromptText("Username already taken"); + check = false; + } + }); + + userService.getUsers().stream().forEach(x -> { + if (x.getEmail().equals(email)) { + signup_username.clear(); + signup_username.setStyle("-fx-prompt-text-fill: red"); + signup_username.setPromptText("Email is already in use"); + check = false; + } + }); + + if (username.isEmpty()){ + signup_username.setStyle("-fx-prompt-text-fill: red"); + signup_username.setPromptText("Please enter a username"); + check = false; + }if (firstName.isEmpty()){ + signup_firstName.setStyle("-fx-prompt-text-fill: red"); + signup_firstName.setPromptText("Please enter your firstname"); + check = false; + }if (lastName.isEmpty()){ + signup_lastName.setStyle("-fx-prompt-text-fill: red"); + signup_lastName.setPromptText("Please enter your surname"); + check = false; + }if (email.isEmpty()){ + signup_email.setStyle("-fx-prompt-text-fill: red"); + signup_email.setPromptText("Please enter your email"); + check = false; + }if (password.isEmpty()){ + signup_password.setStyle("-fx-prompt-text-fill: red"); + signup_password.setPromptText("Please enter a password"); + check = false; + }if (phoneCode.isEmpty() || !(Pattern.matches("[0-9]+", phoneCode))){ + signup_phoneCode.clear(); + signup_phoneCode.setStyle("-fx-prompt-text-fill: red"); + signup_phoneCode.setPromptText("Please enter phnoe code"); + check = false; + }if (phoneNR.isEmpty() || !(Pattern.matches("[0-9]+", phoneNR))){ + signup_phoneNr.clear(); + signup_phoneNr.setStyle("-fx-prompt-text-fill: red"); + signup_phoneNr.setPromptText("Please enter your phone number"); + check = false; + }if (birthdate == null){ + signup_birthDate.setStyle("-fx-prompt-text-fill: red"); + signup_birthDate.setPromptText("Please enter your birthdate"); + check = false; } + return check; } - } - public void cancel(ActionEvent event) throws IOException { - App.setRoot("login"); - } } diff --git a/src/main/java/NTNU/IDATT1002/service/UserService.java b/src/main/java/NTNU/IDATT1002/service/UserService.java index 7b549630610d39003c48782b6e5db8f881a17eef..b5c5d3e105c0928f6647a15427cbce3f7e6fd312 100644 --- a/src/main/java/NTNU/IDATT1002/service/UserService.java +++ b/src/main/java/NTNU/IDATT1002/service/UserService.java @@ -7,6 +7,7 @@ import NTNU.IDATT1002.repository.LoginRepository; import NTNU.IDATT1002.repository.UserRepository; import NTNU.IDATT1002.utils.Authentication; +import java.util.List; import javax.persistence.EntityManager; import java.util.ArrayList; import java.util.Date; @@ -126,4 +127,8 @@ public class UserService { } return false; } + + public List<User> getUsers(){ + return userRepository.findAll(); + } }