diff --git a/src/main/java/com/application/DB/Account_handler.java b/src/main/java/com/application/DB/Account_handler.java index ff6383212d8ff517de9929a1231edd59c3b95957..e5a7cf0596f74fd80208b9d2355f3e8f405afa32 100644 --- a/src/main/java/com/application/DB/Account_handler.java +++ b/src/main/java/com/application/DB/Account_handler.java @@ -8,21 +8,13 @@ import com.google.cloud.bigquery.TableResult; import static com.application.DB.Constants.*; import static com.application.DB.Settings.*; import static com.application.GUI.PopUpWindows.LoginPopup.getPasswordTextField; +import static java.lang.System.err; public class Account_handler { - public static void getAccount(String username, String password) throws Exception { + public static void getAccountInformation(String username, String password) throws Exception { - // Sqlstatement - final String sqlStatement = "SELECT Username, Admin, Phone_no, First_name, Last_name " + - "FROM " + PROJECT_ID + "." + LOCATION_ID + "." + USERS_TABLE_NAME + " " + - "WHERE Username = " + '"' + username + '"' + " " + - "AND Password = " + '"' + password+ '"'; - - System.out.println(sqlStatement); - - // Retrieves the results from the queryjob - TableResult result = HelpingFunctions.createQueryJob(sqlStatement); + TableResult result = logIn(username,password); if(result.getTotalRows() != 0) { for (FieldValueList row : result.iterateAll()) { @@ -51,6 +43,33 @@ public class Account_handler { } } + public static TableResult logIn(String username, String password) throws Exception { + + // Sqlstatement + final String sqlStatement = "SELECT Username, Admin, Phone_no, First_name, Last_name " + + "FROM " + PROJECT_ID + "." + LOCATION_ID + "." + USERS_TABLE_NAME + " " + + "WHERE Username = " + '"' + username + '"' + " " + + "AND Password = " + '"' + password+ '"'; + + System.out.println(sqlStatement); + + // Retrieves the results from the queryjob + return HelpingFunctions.createQueryJob(sqlStatement); + } + + public static TableResult getAccount(String username) throws Exception { + + // Sqlstatement + final String sqlStatement = "SELECT Username " + + "FROM " + PROJECT_ID + "." + LOCATION_ID + "." + USERS_TABLE_NAME + " " + + "WHERE Username = " + '"' + username + '"'; + + System.out.println(sqlStatement); + + // Retrieves the results from the queryjob + return HelpingFunctions.createQueryJob(sqlStatement); + } + public static void addUser(String firstName, String lastName, String phoneNo, String username, String password, boolean isAdmin) throws Exception { // Sqlstatement final String sqlStatement = @@ -62,4 +81,20 @@ public class Account_handler { HelpingFunctions.createQueryJob(sqlStatement); } + public static boolean deleteUser(String username) throws Exception { + + if(getAccount(username).getTotalRows() != 0){ + // Sqlstatement + final String sqlStatement = "DELETE FROM "+ PROJECT_ID + "." + LOCATION_ID + "." + USERS_TABLE_NAME +" WHERE Username = "+'"'+username+'"'; + + System.out.println(sqlStatement); + + HelpingFunctions.createQueryJob(sqlStatement); + + return true; + } else { + return false; + } + } + } diff --git a/src/main/java/com/application/GUI/PopUpWindows/LoginPopup.java b/src/main/java/com/application/GUI/PopUpWindows/LoginPopup.java index d256ef91eeb4e24e4d22d81e7424b9b0c577f292..c9739a3f0918c62b84029f4b34760b2c792f5d57 100644 --- a/src/main/java/com/application/GUI/PopUpWindows/LoginPopup.java +++ b/src/main/java/com/application/GUI/PopUpWindows/LoginPopup.java @@ -11,8 +11,7 @@ import javafx.stage.Stage; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; -import static com.application.DB.Account_handler.addUser; -import static com.application.DB.Account_handler.getAccount; +import static com.application.DB.Account_handler.*; import static com.application.DB.Constants.*; import static com.application.GUI.Panes.LogoBar.getLogin; @@ -39,7 +38,7 @@ public class LoginPopup { loginButton.setOnAction(event -> { try { - getAccount(userNameTextField.getText(), hashPassword(getPasswordTextField().getText())); + getAccountInformation(userNameTextField.getText(), hashPassword(getPasswordTextField().getText())); } catch (Exception e) { e.printStackTrace(); } @@ -76,6 +75,7 @@ public class LoginPopup { Button addUser = new Button("Add User"); Button deleteUser = new Button("Delete User"); Button logout = new Button("Logout"); + Button close = new Button("Close"); addUser.setOnAction(event -> { adminAddUser(); @@ -87,11 +87,12 @@ public class LoginPopup { logout(); window.close(); }); + close.setOnAction(event -> window.close()); VBox layout = new VBox(10); layout.setAlignment(Pos.CENTER); - layout.getChildren().addAll(usernameLabel, usernameTextfield, addUser, deleteUser, logout); + layout.getChildren().addAll(usernameLabel, usernameTextfield, addUser, deleteUser, logout, close); Scene scene = new Scene(layout, 500, 300); scene.getStylesheets().add(InputPopup.class.getResource("/com.application/CSS/styleSheet.css").toExternalForm()); @@ -174,10 +175,28 @@ public class LoginPopup { Label usernameLabel = new Label("Username: "); TextField usernameTextField = new TextField(); + Button close = new Button("Close"); + Button delete = new Button("Delete User"); + + close.setOnAction(event -> window.close()); + delete.setOnAction(event -> { + try { + boolean results = deleteUser(usernameTextField.getText()); + if(results){ + NotificationPopUp.displayNotificationWindow(usernameTextField.getText()+" was successfully deleted!"); + window.close(); + } else { + NotificationPopUp.displayNotificationWindow("Could not find username: " + usernameTextField.getText()); + usernameTextField.clear(); + } + } catch (Exception e) { + e.printStackTrace(); + } + }); VBox layout = new VBox(10); layout.setAlignment(Pos.CENTER); - layout.getChildren().addAll(); + layout.getChildren().addAll(usernameLabel, usernameTextField, delete, close); Scene scene = new Scene(layout, 500, 300); scene.getStylesheets().add(InputPopup.class.getResource("/com.application/CSS/styleSheet.css").toExternalForm()); diff --git a/target/classes/com/application/DB/Account_handler.class b/target/classes/com/application/DB/Account_handler.class index 6e0dd40dcbbfaca02fad27009793c8cbbaf28c39..c09f8ced6631474642158218a0c1f8833b6ff76f 100644 Binary files a/target/classes/com/application/DB/Account_handler.class and b/target/classes/com/application/DB/Account_handler.class differ diff --git a/target/classes/com/application/GUI/PopUpWindows/LoginPopup.class b/target/classes/com/application/GUI/PopUpWindows/LoginPopup.class index d4a94a6fe190f467c0b42a87ea149c36ce524ac6..698cb86ef53e3d533faa5e46e0a6a7d16ec3f077 100644 Binary files a/target/classes/com/application/GUI/PopUpWindows/LoginPopup.class and b/target/classes/com/application/GUI/PopUpWindows/LoginPopup.class differ