Skip to content
Snippets Groups Projects
Commit 48ea170c authored by Eilert Tunheim's avatar Eilert Tunheim
Browse files

Fully implemented deleting user!

parent e3b3ab56
Branches
No related tags found
No related merge requests found
......@@ -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;
}
}
}
......@@ -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());
......
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment