Skip to content

Commit

Permalink
Feat: Added JavaDoc to new strucutre
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianBalunan committed Apr 23, 2026
1 parent 7bc1951 commit 8100494
Show file tree
Hide file tree
Showing 11 changed files with 372 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ protected void authTokenisSet() {
}
private void populateFields(){
DatabaseConnection conn = new DatabaseConnection();
CharityDAO charitySelect = new CharityDAO(conn);
ArrayList<Feedback> feedbacks = charitySelect.getFeedbackforCharityUUID(charity.getUUID().toString());
FeedbackDAO feedbackDAO = new FeedbackDAO(conn);
ArrayList<Feedback> feedbacks = feedbackDAO.getFeedbackforCharityUUID(charity.getUUID().toString());
displayFeedbacks(feedbacks);
}
private void displayFeedbacks(ArrayList<Feedback> feedbacks){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
import ntnu.systemutvikling.team6.controller.components.*;
import ntnu.systemutvikling.team6.database.DAO.FeedbackDAO;
import ntnu.systemutvikling.team6.database.DAO.MessageDAO;
import ntnu.systemutvikling.team6.database.DatabaseConnection;
import ntnu.systemutvikling.team6.database.DAO.CharityDAO;
Expand Down Expand Up @@ -56,8 +57,8 @@ public void populateFields() {

// Messages
DatabaseConnection conn = new DatabaseConnection();
CharityDAO charitySelect = new CharityDAO(conn);
ArrayList<Feedback> feedbacks = charitySelect.getFeedbackforCharityUUID(authToken.isCharityUser().getUUID().toString());
FeedbackDAO feedbackDAO = new FeedbackDAO(conn);
ArrayList<Feedback> feedbacks = feedbackDAO.getFeedbackforCharityUUID(authToken.isCharityUser().getUUID().toString());
displayFeedbacks(feedbacks);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public List<String> getCategoriesFromDB() {
throw new RuntimeException(
"ERROR: Something went wrong during fetching categories from database.");
}

return categories;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
import ntnu.systemutvikling.team6.models.user.User;

/**
* Data access class responsible for reading charity-related data from the database.
*
* <p>Provides methods to retrieve all charities (with their associated feedback and users) as well
* Data access class responsible for acsessing charity-related data from the database.
* <p>
* Primarily provides read methods to retrieve all charities (with their associated feedback and users) as well
* as feedback entries for a specific charity by UUID.
*
* <p>All queries are executed against a MySQL database via a {@link DatabaseConnection}.
* <p>
* All queries are executed against a MySQL database via a {@link DatabaseConnection}.
*/
public class CharityDAO {
private final DatabaseConnection connection;
Expand All @@ -39,7 +39,7 @@ public CharityDAO(DatabaseConnection connection) {
* who submitted each piece of feedback.
*
* <p>The query performs a LEFT JOIN between the {@code Charities}, {@code Feedback}, {@code
* User}, {@code CharityVanity}, and {@code category(s)} tables. Each unique charity is added once
* User} and {@code category(s)} tables and a INNER JOIN with {@code CharityVanity} table. Each unique charity is added once
* to the registry; any feedback rows found for that charity are appended to its feedback list.
*
* <p>Note: charities with no feedback and categories are still included in the result due to the
Expand Down Expand Up @@ -139,6 +139,8 @@ public CharityRegistry getCharitiesFromDB() {
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("ERROR: Something went wrong during updating charities table.");
} finally {
conn = null;
}

return registry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,41 @@

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
* This Data Access Object is responsible for communication to the Database for a potensial user that is also a CharityUser.
*
* <p>
* CharityUsers have additional features and priviliges that regular users don't have. Methods
* specified provide the opportunity to save a new name or description.
* </p>
* <p>
* All queries are executed against a MySQL database via a {@link DatabaseConnection}.
* </p>
*
*/
public class CharityUserDAO {

private DatabaseConnection connection;

/**
* Constructs a new {@code CharityUserDAO} with the given database connection.
*
* @param connection the {@link DatabaseConnection} to use for executing queries; must not be
* {@code null}
*/
public CharityUserDAO(DatabaseConnection connection) {
this.connection = connection;
}

/**
* Updates the Charity's name in the {@code CharityVanity} table in the database by getting the Charity object in question.
*
* @param charity Charity containing the new name for the database
* @return True or False based on if the update succeed or not
*/
public boolean updateCharityVanityName(Charity charity){
Connection conn = null;
String sql = """
Expand All @@ -40,6 +66,13 @@ public boolean updateCharityVanityName(Charity charity){
}

}

/**
* Updates the Charity's description in the {@code CharityVanity} table in the database by getting the Charity object in question.
*
* @param charity Charity containing the new name for the database
* @return True or False based on if the update succeed or not
*/
public boolean updateCharityVanityDescription(Charity charity){
Connection conn = null;
String sql = """
Expand All @@ -64,4 +97,69 @@ public boolean updateCharityVanityDescription(Charity charity){
}

}

/**
* Gets the Charity that the User is connected to by using {@code CharityUsers} as an identifie/bridge.
*
* @param uuid Id of the User.
* @return The Charity the CharityUser is connected to.
*/
public Charity getUserCharityUser(String uuid){
if (uuid == null || uuid.isBlank()) {
throw new IllegalArgumentException(
"UUID cannot be null or blank");
}
Charity currentCharity = null;
Connection conn = null;
try {
conn = connection.getMySqlConnection();
String sql_query =
"""
SELECT
c.UUID_charities, c.org_number, c.pre_approved, c.status,
cv.charity_name, cv.charity_link, cv.description, cv.logoURL, cv.key_values, cv.logoBLOB,
cat.category
FROM CharityUsers cu
INNER JOIN Charities c ON c.UUID_charities = cu.TheCharity
INNER JOIN CharityVanity cv ON cv.UUID_charity = c.UUID_charities
LEFT JOIN Charity_Categories cc ON cc.Charities_UUID_charities = c.UUID_charities
LEFT JOIN Categories cat ON cat.category_id = cc.Categories_category_id
WHERE CharityUserId = ?
""";
PreparedStatement stmt = conn.prepareStatement(sql_query);
stmt.setString(1, uuid);
ResultSet rs = stmt.executeQuery();

String lastCharity = null;

while (rs.next()) {
String currentId = rs.getString("UUID_charities");
if (lastCharity == null || !currentId.equals(lastCharity)) {
currentCharity =
new Charity(
rs.getString("UUID_charities"),
rs.getString("org_number"),
rs.getString("charity_name"),
rs.getString("charity_link"),
rs.getString("status"),
rs.getBoolean("pre_approved"),
rs.getString("description"),
rs.getString("logoURL"),
rs.getString("key_values"),
rs.getBytes("logoBLOB"));
lastCharity = currentId;
}

String categoryName = rs.getString("category");
if (categoryName != null && !currentCharity.getCategory().contains(categoryName)) {
currentCharity.getCategory().add(categoryName);
}
}

} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("ERROR: Something went wrong during updating charities table.");
}
return currentCharity;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,27 @@
import ntnu.systemutvikling.team6.models.user.User;

/**
* This class is responsible for sending concurrent information about the donation to the Donation
* Database. Usally called from the DonationPageController, where the user confirms their donation.
* This class is responsible for sending and receiving concurrent information about the donation to and from the Donation
* Database. Usually called from the Donation related controller, where the user is able to view their donations.
*/
public class DonationDAO {
private final DatabaseConnection connection;

/**
* Constructs a new {@code CharityUserDAO} with the given database connection.
*
* @param connection the {@link DatabaseConnection} to use for executing queries; must not be
* {@code null}
*/
public DonationDAO(DatabaseConnection connection) {
this.connection = connection;
}

/**
* Retrieves all donations from the database, each populated with its associated {@link Charity}.
*
* <p>The query performs an INNER JOIN between the {@code Donations} and {@code Charities} tables
* on the charity UUID foreign key. Donations without a matching charity are excluded from the
* <p>The query performs an INNER JOIN between the {@code Donations}, {@code Charities} and {@code User} tables
* on the donations row Charity_id and user_id. Donations without a matching charity are excluded from the
* result.
*
* @return a {@link DonationRegistry} containing all matched donations; never {@code null}, but
Expand Down Expand Up @@ -80,6 +87,18 @@ public DonationRegistry getDonationFromDB() {
return registry;
}

/**
* Retrieves all donation data from the {@code Donations} table for a spesific user using its ID.
*
* <p>
* Query selects from {@code Donations}, then INNER JOINS with {@code Charities} and {@code CharityVanity}
* to get names among other thing, and {@code User} table to get the donor. At the end uses a WHERE-clause to
* get the specified User.
* </p>
*
* @param uuid the java.UUID for the user but in string.
* @return Returns a DOnationRegistry with all donations.
*/
public DonationRegistry getDonationForUser(String uuid) {
DonationRegistry registry = null;
Connection conn = null;
Expand Down Expand Up @@ -140,6 +159,19 @@ public DonationRegistry getDonationForUser(String uuid) {
return registry;
}

/**
* Retrieves all donation data from the {@code Donations} table for a specific Charity using its ID.
*
* <p>
* Mostly used by Charity Users to view how much money they have.
* Query selects from {@code Donations}, then INNER JOINS with {@code Charities} and {@code CharityVanity}
* to get names among other thing, and {@code User} table to get the donor. At the end uses a WHERE - clause
* to get the Charity in question.
* </p>
*
* @param uuid the java.UUID for the user but in string.
* @return Returns a DOnationRegistry with all donations.
*/
public DonationRegistry getDonationForCharity(String uuid) {
DonationRegistry registry = null;
Connection conn = null;
Expand Down Expand Up @@ -199,12 +231,12 @@ public DonationRegistry getDonationForCharity(String uuid) {
}
return registry;
}

/**
* Gets the total ammount of donations for a given charity, and sends it to the database throught
* Gets a Donation object for a given charity and by a User, and sends it to the database through
* MySQL.
*
* @param charity
* @param amount
* @param donation Donation object containing all relevant information to send to database.
*/
public void addDonation(Donation donation) {
String sql_query =
Expand Down
Loading

0 comments on commit 8100494

Please sign in to comment.