Skip to content

Commit

Permalink
Feat: Donation work. Syntax fixes and sturcutalr fixes. Favourite But…
Browse files Browse the repository at this point in the history
…ton is removed from charity.
  • Loading branch information
AdrianBalunan committed Apr 20, 2026
1 parent 530a61d commit 61862b7
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 186 deletions.
Binary file modified docs/SqlDatabase/ER-DiagramFile.mwb
Binary file not shown.
Binary file modified docs/SqlDatabase/ER-DiagramFile.mwb.bak
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}

/**
Expand All @@ -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.
*
Expand Down Expand Up @@ -109,15 +126,26 @@ public void Donate(ActionEvent event) {
return;
}

Optional<ButtonType> 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());
confirm.setContentText("Are you sure?");
Optional<ButtonType> 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!",
Expand All @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.Cursor?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Hyperlink?>
<?import javafx.scene.control.Label?>
Expand All @@ -25,6 +24,7 @@

<columnConstraints>
<ColumnConstraints percentWidth="100.0" />
<ColumnConstraints />
</columnConstraints>

<rowConstraints>
Expand Down Expand Up @@ -129,11 +129,6 @@
</Hyperlink>

<VBox alignment="CENTER" prefHeight="89.0" prefWidth="191.0" spacing="22.0">
<Button prefHeight="55.0" prefWidth="190.0" style="-fx-background-color:#4453D9; -fx-background-radius:30; -fx-text-fill:white;" text="Favorite">
<font>
<Font name="System Bold" size="16.0" />
</font>
</Button>

<Button onAction="#switchToDonationPage" prefHeight="55.0" prefWidth="190.0" style="-fx-background-color:#4E7C3F; -fx-background-radius:30; -fx-text-fill:white;" text="Donate">
<font>
Expand Down
Loading

0 comments on commit 61862b7

Please sign in to comment.