Skip to content

Commit

Permalink
Feat: Turned DAO objects into non-static object for coupling
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianBalunan committed Apr 15, 2026
1 parent 72045a1 commit 939ef16
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@
* Database. Usally called from the DonationPageController, where the user confirms their donation.
*/
public class DonationDAO {
private static final DatabaseConnection connection = new DatabaseConnection();
private final DatabaseConnection connection;

/**
public DonationDAO(DatabaseConnection connection) {
this.connection = connection;
}

/**
* Gets the total ammount of donations for a given charity, and sends it to the database throught
* MySQL.
*
* @param charity
* @param amount
*/
public static void addDonation(Charity charity, User user, double amount) {
public void addDonation(Charity charity, User user, double amount) {
String sql_query =
"""
INSERT INTO Donations (UUID_Donations, amount, date, charity_id, user_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@
*/
public class UserDAO {

private static final DatabaseConnection connection = new DatabaseConnection();
private final DatabaseConnection connection;

/**
public UserDAO(DatabaseConnection connection) {
this.connection = connection;
}

/**
* Gets the user and settings information and sends it to the database through MySQL.
*
*
* @param user the user to be saved in the database.
* @throws RuntimeException if a database error occurs during the operation
* @return true or false based on if the register is a success or not
*/
public static void registerUser(User user) {

public boolean registerUser(User user) {
String userSql =
"""
INSERT INTO User (
Expand All @@ -48,6 +53,9 @@ INSERT INTO Settings (
VALUES (?, ?, ?, ?)
""";

int psUserRows = 0;
int psSettingsRows = 0;

try (Connection conn = connection.getMySqlConnection()) {

conn.setAutoCommit(false);
Expand All @@ -61,7 +69,7 @@ INSERT INTO Settings (
psUser.setString(5, user.getPasswordHash());
psUser.setString(6, user.getRole().name());

psUser.executeUpdate();
psUserRows = psUser.executeUpdate();
}

try (PreparedStatement psSettings = conn.prepareStatement(settingsSql)) {
Expand All @@ -71,13 +79,16 @@ INSERT INTO Settings (
psSettings.setString(3, user.getSettings().getLanguage().name());
psSettings.setBoolean(4, user.getSettings().isLightMode());

psSettings.executeUpdate();
psSettingsRows = psSettings.executeUpdate();

}

conn.commit();

return psUserRows > 0 && psSettingsRows > 0;
} catch (SQLException e) {
throw new RuntimeException("Failed to insert user into database", e);
e.printStackTrace();
return false;
}
}
}

0 comments on commit 939ef16

Please sign in to comment.