diff --git a/docs/SqlDatabase/ER-DiagramFile.mwb b/docs/SqlDatabase/ER-DiagramFile.mwb index 15fa8f6..3e2e8b9 100644 Binary files a/docs/SqlDatabase/ER-DiagramFile.mwb and b/docs/SqlDatabase/ER-DiagramFile.mwb differ diff --git a/docs/SqlDatabase/ER-DiagramFile.mwb.bak b/docs/SqlDatabase/ER-DiagramFile.mwb.bak index babd305..15fa8f6 100644 Binary files a/docs/SqlDatabase/ER-DiagramFile.mwb.bak and b/docs/SqlDatabase/ER-DiagramFile.mwb.bak differ diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/DonationPageController.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/DonationPageController.java index 2ae0aee..189471f 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/DonationPageController.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/DonationPageController.java @@ -3,15 +3,14 @@ import java.util.Optional; import javafx.event.ActionEvent; import javafx.fxml.FXML; -import javafx.scene.control.Alert; -import javafx.scene.control.ButtonType; -import javafx.scene.control.Label; -import javafx.scene.control.TextField; +import javafx.scene.control.*; +import javafx.stage.Stage; import ntnu.systemutvikling.team6.controller.components.BaseController; import ntnu.systemutvikling.team6.controller.components.FooterController; import ntnu.systemutvikling.team6.controller.components.LoaderScene; import ntnu.systemutvikling.team6.controller.components.NavbarController; import ntnu.systemutvikling.team6.database.DAO.DonationDAO; +import ntnu.systemutvikling.team6.database.DAO.FavouritesDAO; import ntnu.systemutvikling.team6.database.DatabaseConnection; import ntnu.systemutvikling.team6.models.Charity; import ntnu.systemutvikling.team6.models.user.User; @@ -26,21 +25,26 @@ public class DonationPageController extends BaseController { @FXML private TextField donatioAmount; @FXML private Label CharityName; - + @FXML ToggleButton heartButton; @FXML private TextField donationSearchField; @FXML private NavbarController navbarController; @FXML private FooterController footerController; - private DonationDAO donationSender = new DonationDAO(new DatabaseConnection()); + DatabaseConnection conn = new DatabaseConnection(); + private DonationDAO donationSender = new DonationDAO(conn); + private FavouritesDAO favouritesDAO = new FavouritesDAO(conn); @Override protected void authTokenisSet() { - if (isLoggedin()){ - LoaderScene.LoadScene("frontPage", new ActionEvent(), null, null, authToken); + if (!isLoggedin()){ + showAlert(Alert.AlertType.ERROR, "Not logged inn", "You need to be logged inn to donate."); + Stage stage = (Stage) donationSearchField.getScene().getWindow(); + LoaderScene.LoadScene("loginSite", stage, null, null, authToken); } navbarController.setAuthToken(authToken); footerController.setAuthToken(authToken); + populateFields(); } /** @@ -57,15 +61,28 @@ public void setCharity(Charity charity) { CharityName.setText(charity.getName()); } - /** - * This method is used to switch back to the front page when the user clicks the back button. - * - * @param event - */ - public void switchToFrontPage(ActionEvent event) { - LoaderScene.LoadScene("FrontPage", event, null, null, authToken); + private void populateFields(){ + boolean isFavourite = favouritesDAO.isFavourite(authToken.getCurrentUser(), charity); + heartButton.setSelected(isFavourite); + updateHeartIcon(isFavourite); } + @FXML + private void onHeartToggle() { + boolean selected = heartButton.isSelected(); + User user = authToken.getCurrentUser(); + if (selected) { + favouritesDAO.addFavourite(user, charity); + } else { + favouritesDAO.removeFavourite(user, charity); + } + + updateHeartIcon(selected); + } + + private void updateHeartIcon(boolean selected) { + heartButton.setText(selected ? "♥" : "♡"); + } /** * This method is used to switch back to the Donation page when the user clicks the back button. * @@ -109,6 +126,18 @@ public void Donate(ActionEvent event) { return; } + Optional resultAnonymous = Optional.empty(); + if (authToken.getCurrentUser().getSettings().isAnonymous()){ + Alert confirmAnonymous = new Alert(Alert.AlertType.CONFIRMATION); + confirmAnonymous.setTitle("You're going to donate Anonymously"); + confirmAnonymous.setHeaderText("You're about to make an anonymous donation. The charity and other donors will not be able to see your name amd email assosiated with the donation"); + confirmAnonymous.setContentText("Are you sure?"); + resultAnonymous = confirmAnonymous.showAndWait(); + if (resultAnonymous.isPresent() && resultAnonymous.get() == ButtonType.CANCEL){ + return; + } + } + Alert confirm = new Alert(Alert.AlertType.CONFIRMATION); confirm.setTitle("Confirm Donation"); confirm.setHeaderText("You're about to donate " + amount + " to " + charity.getName()); @@ -116,8 +145,7 @@ public void Donate(ActionEvent event) { Optional result = confirm.showAndWait(); if (result.isPresent() && result.get() == ButtonType.OK) { - // Process donation - //processDonation(charity, user, amount); + donationSender.addDonation(charity, authToken.getCurrentUser(), amount); showAlert( Alert.AlertType.INFORMATION, "Thank you!", @@ -127,19 +155,6 @@ public void Donate(ActionEvent event) { } } - /** - * Invoke DAO object to add the donation to the database. This method is called from the Donate - * method after the user confirms their donation. - * - * @param charity - * @param amount - */ - public void processDonation(Charity charity, User user, double amount) { - donationSender.addDonation(charity, user, amount); - } - - - /** * This method is used to handle the search action when the user clicks the search button. * diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/profileUser/profileUserSettingsController.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/profileUser/profileUserSettingsController.java index 60ed7c5..0e0bf58 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/profileUser/profileUserSettingsController.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/profileUser/profileUserSettingsController.java @@ -56,6 +56,8 @@ public void populateFields(){ nameLabel.setText(user.getUsername()); shortNameLabel.setText(user.getUsername().substring(0,2).toUpperCase().trim()); shortNameLabel2.setText(user.getUsername().substring(0,2).toUpperCase().trim()); + NameField.setText(user.getUsername()); + EmailField.setText(user.getEmail()); // SettingsPage Spesefic: languageComboBox.setValue(settings.getLanguage() == Language.NORWEGIAN ? "Norwegian" : "English"); @@ -69,10 +71,6 @@ public void populateFields(){ "-fx-background-radius: 14; -fx-min-width: 32; -fx-min-height: 28;" + "-fx-max-width: 52; -fx-max-height: 28;" ); - - - NameField.setText(user.getUsername()); - EmailField.setText(user.getEmail()); } @FXML diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/DonationDAO.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/DonationDAO.java index 730acca..dd4b252 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/DonationDAO.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/DonationDAO.java @@ -27,18 +27,19 @@ public DonationDAO(DatabaseConnection connection) { public void addDonation(Charity charity, User user, double amount) { String sql_query = """ - INSERT INTO Donations (UUID_Donations, amount, date, charity_id, user_id) - VALUES (?, ?, ?, ?, ?) + INSERT INTO Donations (UUID_Donations, amount, isAnonymous, date, charity_id, user_id) + VALUES (?, ?, ?, ?, ?, ?) """; try (Connection conn = connection.getMySqlConnection(); PreparedStatement ps = conn.prepareStatement(sql_query)) { conn.setAutoCommit(false); - ps.setString(1, UUID.randomUUID().toString()); + ps.setString(1, user.getId().toString()); ps.setDouble(2, amount); - ps.setDate(3, new Date(System.currentTimeMillis())); - ps.setString(4, charity.getUUID().toString()); - ps.setString(5, user.getId().toString()); + ps.setBoolean(3, user.getSettings().isAnonymous()); + ps.setDate(4, new Date(System.currentTimeMillis())); + ps.setString(5, charity.getUUID().toString()); + ps.setString(6, user.getId().toString()); ps.executeUpdate(); diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/FavouritesDAO.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/FavouritesDAO.java index 2c50e25..f903758 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/FavouritesDAO.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/FavouritesDAO.java @@ -20,8 +20,25 @@ public FavouritesDAO(DatabaseConnection connection) { this.connection = connection; } + public boolean isFavourite(User user, Charity charity) { + String sql = "SELECT * FROM User_has_favourites WHERE Favourer = ? AND Favourite_Charity = ?"; + + try (Connection conn = connection.getMySqlConnection(); + PreparedStatement ps = conn.prepareStatement(sql)) { + + ps.setString(1, user.getId().toString()); + ps.setString(2, charity.getUUID().toString()); + + ResultSet rs = ps.executeQuery(); + return rs.next(); + + } catch (SQLException e) { + e.printStackTrace(); + return false; + } + } public boolean addFavourite(User user, Charity charity) { - String sql = "INSERT INTO Favourites (UUID_user, UUID_charity) VALUES (?, ?)"; + String sql = "INSERT INTO User_has_favourites (Favourer, Favourite_charity) VALUES (?, ?)"; try (Connection conn = connection.getMySqlConnection(); PreparedStatement ps = conn.prepareStatement(sql)) { @@ -37,7 +54,7 @@ public boolean addFavourite(User user, Charity charity) { } public boolean removeFavourite(User user, Charity charity) { - String sql = "DELETE FROM Favourites WHERE UUID_user = ? AND UUID_charity = ?"; + String sql = "DELETE FROM User_has_favourites WHERE Favourer = ? AND Favourite_charity = ?"; try (Connection conn = connection.getMySqlConnection(); PreparedStatement ps = conn.prepareStatement(sql)) { diff --git a/helpmehelpapplication/src/main/resources/fxml/charityPage.fxml b/helpmehelpapplication/src/main/resources/fxml/charityPage.fxml index b35e4b4..d4d611d 100644 --- a/helpmehelpapplication/src/main/resources/fxml/charityPage.fxml +++ b/helpmehelpapplication/src/main/resources/fxml/charityPage.fxml @@ -1,7 +1,6 @@ - @@ -25,6 +24,7 @@ + @@ -129,11 +129,6 @@ - - - - - + + @@ -150,6 +93,11 @@ + @@ -160,83 +108,7 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + +