From 810049432efdbc8a27043436342671c9c73594d2 Mon Sep 17 00:00:00 2001 From: AdrianBalunan Date: Thu, 23 Apr 2026 21:14:22 +0200 Subject: [PATCH] Feat: Added JavaDoc to new strucutre --- .../controller/GiveFeedbackController.java | 4 +- .../profileOrgInboxController.java | 5 +- .../team6/database/DAO/CategoryDAO.java | 1 - .../team6/database/DAO/CharityDAO.java | 14 +-- .../team6/database/DAO/CharityUserDAO.java | 98 +++++++++++++++++++ .../team6/database/DAO/DonationDAO.java | 46 +++++++-- .../team6/database/DAO/FavouritesDAO.java | 63 +++++++++++- .../team6/database/DAO/FeedbackDAO.java | 92 ++++++++++++++++- .../team6/database/DAO/MessageDAO.java | 34 +++++++ .../team6/database/DAO/UserDAO.java | 91 +++++++---------- .../team6/service/AuthenticationService.java | 5 +- 11 files changed, 372 insertions(+), 81 deletions(-) diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/GiveFeedbackController.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/GiveFeedbackController.java index 9a720bf..e6d599f 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/GiveFeedbackController.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/GiveFeedbackController.java @@ -58,8 +58,8 @@ protected void authTokenisSet() { } private void populateFields(){ DatabaseConnection conn = new DatabaseConnection(); - CharityDAO charitySelect = new CharityDAO(conn); - ArrayList feedbacks = charitySelect.getFeedbackforCharityUUID(charity.getUUID().toString()); + FeedbackDAO feedbackDAO = new FeedbackDAO(conn); + ArrayList feedbacks = feedbackDAO.getFeedbackforCharityUUID(charity.getUUID().toString()); displayFeedbacks(feedbacks); } private void displayFeedbacks(ArrayList feedbacks){ diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/profileCharity/profileOrgInboxController.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/profileCharity/profileOrgInboxController.java index f924dfa..335e944 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/profileCharity/profileOrgInboxController.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/profileCharity/profileOrgInboxController.java @@ -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; @@ -56,8 +57,8 @@ public void populateFields() { // Messages DatabaseConnection conn = new DatabaseConnection(); - CharityDAO charitySelect = new CharityDAO(conn); - ArrayList feedbacks = charitySelect.getFeedbackforCharityUUID(authToken.isCharityUser().getUUID().toString()); + FeedbackDAO feedbackDAO = new FeedbackDAO(conn); + ArrayList feedbacks = feedbackDAO.getFeedbackforCharityUUID(authToken.isCharityUser().getUUID().toString()); displayFeedbacks(feedbacks); } diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/CategoryDAO.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/CategoryDAO.java index bd50602..49a0ae7 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/CategoryDAO.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/CategoryDAO.java @@ -47,7 +47,6 @@ public List getCategoriesFromDB() { throw new RuntimeException( "ERROR: Something went wrong during fetching categories from database."); } - return categories; } } diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/CharityDAO.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/CharityDAO.java index 83a9b22..85e73dc 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/CharityDAO.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/CharityDAO.java @@ -14,12 +14,12 @@ import ntnu.systemutvikling.team6.models.user.User; /** - * Data access class responsible for reading charity-related data from the database. - * - *

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. + *

+ * 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. - * - *

All queries are executed against a MySQL database via a {@link DatabaseConnection}. + *

+ * All queries are executed against a MySQL database via a {@link DatabaseConnection}. */ public class CharityDAO { private final DatabaseConnection connection; @@ -39,7 +39,7 @@ public CharityDAO(DatabaseConnection connection) { * who submitted each piece of feedback. * *

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. * *

Note: charities with no feedback and categories are still included in the result due to the @@ -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; diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/CharityUserDAO.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/CharityUserDAO.java index 99d9de8..ee324f7 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/CharityUserDAO.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/CharityUserDAO.java @@ -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. + * + *

+ * CharityUsers have additional features and priviliges that regular users don't have. Methods + * specified provide the opportunity to save a new name or description. + *

+ *

+ * All queries are executed against a MySQL database via a {@link DatabaseConnection}. + *

+ * + */ 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 = """ @@ -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 = """ @@ -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; + } } 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 3e93b2e..8fd3b1a 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 @@ -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}. * - *

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 + *

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 @@ -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. + * + *

+ * 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. + *

+ * + * @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; @@ -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. + * + *

+ * 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. + *

+ * + * @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; @@ -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 = 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 0f6fd2d..2c15d9d 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 @@ -2,24 +2,53 @@ import ntnu.systemutvikling.team6.database.DatabaseConnection; import ntnu.systemutvikling.team6.models.Charity; +import ntnu.systemutvikling.team6.models.Feedback; import ntnu.systemutvikling.team6.models.registry.CharityRegistry; +import ntnu.systemutvikling.team6.models.user.Language; +import ntnu.systemutvikling.team6.models.user.Settings; import ntnu.systemutvikling.team6.models.user.User; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.time.LocalDate; import java.util.ArrayList; import java.util.List; +/** + * Data access class responsible for getting managing favourites for a logged inn user in the application. + *

+ * Methods include gettng users favourites, adding a favourite and checking if the charity was favourited. + *

+ * + *

All queries are executed against a MySQL database via a {@link DatabaseConnection}. + */ public class FavouritesDAO { private final DatabaseConnection connection; + /** + * Constructs a new {@code FavouritesDAO} with the given database connection. + * + * @param connection the {@link DatabaseConnection} to use for executing queries; must not be + * {@code null} + */ public FavouritesDAO(DatabaseConnection connection) { this.connection = connection; } + /** + * Checks if both {@code User} and {@code Charity} are in the same row in the {@code User_has_favourites } table. + * If it is, the charity is considered a favourite by the User + * + *

+ * Uses a Select query with a WHERE-clause that searches for a row with both ids, which considered a favourite. + *

+ * @param user User in question + * @param charity Charity in question of being favourite by User. + * @return + */ public boolean isFavourite(User user, Charity charity) { String sql = "SELECT * FROM User_has_favourites WHERE Favourer = ? AND Favourite_Charity = ?"; @@ -37,6 +66,15 @@ public boolean isFavourite(User user, Charity charity) { return false; } } + + /** + * Send a quick insert query to the {@code User_has_favourites} table, adding a row with favourer and the + * favourite Charity + * + * @param user User or the Favourer in the database + * @param charity Favourite charity in question. + * @return True or False based on if it succeeded or not + */ public boolean addFavourite(User user, Charity charity) { String sql = "INSERT INTO User_has_favourites (Favourer, Favourite_charity) VALUES (?, ?)"; @@ -53,6 +91,18 @@ public boolean addFavourite(User user, Charity charity) { } } + /** + * Does a quick remove query to delete a previous favourite charity. + * + *

+ * This will not invoke and Argument-Exception because the remove query is avabile only after being added + * throught {@code addFavourite} + *

+ * @param user user in question of having a favourite + * @param charity Charity about to be removed a favourite + * @return + */ + public boolean removeFavourite(User user, Charity charity) { String sql = "DELETE FROM User_has_favourites WHERE Favourer = ? AND Favourite_charity = ?"; @@ -68,6 +118,18 @@ public boolean removeFavourite(User user, Charity charity) { return false; } } + + /** + * Get all detailts about the users favourites. + * + *

+ * Uses a Select-query, INNER JOIN between {@code Charities} and {@code CharityVanity} tables, and LEFT JOIN + * {@code Charity_Categories} and {@code Categries} tables to get fill all attributes in the {@code Charity} + * object. At the end, uses a WHERE-clause to grab the right favourer. + *

+ * @param user_id UUID in String for the Favourer + * @return An empty or non-empty List of Charities that have been favourited by User + */ public List getFavouritesForUser(String user_id) { CharityRegistry registry = null; Connection conn = null; @@ -128,5 +190,4 @@ public List getFavouritesForUser(String user_id) { } return registry.getAllCharities(); } - } \ No newline at end of file diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/FeedbackDAO.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/FeedbackDAO.java index 705929a..4b7a5a3 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/FeedbackDAO.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/FeedbackDAO.java @@ -3,19 +3,39 @@ import ntnu.systemutvikling.team6.database.DatabaseConnection; import ntnu.systemutvikling.team6.models.Charity; import ntnu.systemutvikling.team6.models.Feedback; +import ntnu.systemutvikling.team6.models.user.Language; +import ntnu.systemutvikling.team6.models.user.Settings; +import ntnu.systemutvikling.team6.models.user.User; -import java.sql.Connection; -import java.sql.Date; -import java.sql.PreparedStatement; -import java.sql.SQLException; +import java.sql.*; +import java.time.LocalDate; +import java.util.ArrayList; +/** + * This Data Access Object is mainly responsible for handling Feedbacks and communitcating with the DataBase. + * Primarily interacting with the {@code Feedback} table. + */ public class FeedbackDAO { private final DatabaseConnection connection; + /** + * Constructs a new {@code FeedbackDAO} with the given database connection. + * + * @param connection the {@link DatabaseConnection} to use for executing queries; must not be + * {@code null} + */ public FeedbackDAO(DatabaseConnection connection){ this.connection = connection; } + /** + * Send a simple insert query to the {@code Feedback} table. Containing the comment, date, if its an Anonymous + * Feedback and recipients. + * + * @param feedback Feedback object contain all relevant info, as said above. + * @param toCharity Dedicated Charity object, the recipient. + * @return + */ public boolean addFeedbackToCharity(Feedback feedback, Charity toCharity){ String sql = """ INSERT INTO Feedback @@ -41,4 +61,68 @@ public boolean addFeedbackToCharity(Feedback feedback, Charity toCharity){ } } + /** + * A helper function that retrieves all feedback entries associated with a specific charity, + * identified by its UUID. Currently, has no use. + * + *

Each {@link Feedback} object is populated with the associated {@link User} (without settings + * or inbox data). The query uses a LEFT JOIN between the {@code Feedback} and {@code User} + * tables, filtered by {@code charity_id}. + * + * @param charity_uuid the UUID of the charity whose feedback should be retrieved; must not be + * {@code null} + * @return an {@link ArrayList} of {@link Feedback} objects for the given charity; returns an + * empty list if no feedback exists for that charity + * @throws RuntimeException if any exception occurs while executing the query, wrapping the + * original cause + */ + public ArrayList getFeedbackforCharityUUID(String charity_uuid) { + ArrayList Feedbacks = new ArrayList<>(); + Connection conn = null; + try { + conn = connection.getMySqlConnection(); + String sql_query = + """ + SELECT + f.UUID_feedback, f.feedback_comment, f.feedback_date, f.isAnonymous, f.charity_id, f.user_id, + u.UUID_user, u.user_name, u.user_email, u.user_password, u.role, + s.language, s.lightmode + FROM Feedback f + LEFT JOIN User u ON f.user_id = u.UUID_user + RIGHT JOIN Settings s ON u.UUID_user = s.UUID_user + WHERE f.charity_id = ?; + """; + PreparedStatement stmt = conn.prepareStatement(sql_query); + stmt.setString(1, charity_uuid); + ResultSet rs = stmt.executeQuery(); + + while (rs.next()) { + User userWithSettingsAndNoInbox = + new User( + rs.getString("UUID_User"), + rs.getString("user_name"), + rs.getString("user_email"), + rs.getString("user_password"), + rs.getString("role")); + userWithSettingsAndNoInbox.setSettings(new Settings( + rs.getBoolean("isAnonymous"), + Language.valueOf(rs.getString("language")), + rs.getBoolean("lightmode") + )); + Feedback feedback = + new Feedback( + rs.getString("UUID_feedback"), + userWithSettingsAndNoInbox, + rs.getString("feedback_comment"), + LocalDate.parse(rs.getString("feedback_date"))); + Feedbacks.add(feedback); + } + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException(e); + } finally { + conn = null; + } + return Feedbacks; + } } diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/MessageDAO.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/MessageDAO.java index 8545863..0ad6b9a 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/MessageDAO.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/MessageDAO.java @@ -12,12 +12,34 @@ import java.util.List; import java.util.UUID; +/** + * This Data Access Object is mainly responsible for handling Messages and Message object to communitcate correctly + * with the DataBase. + * Primarily interacting with the {@code Messages} table. + */ public class MessageDAO { private final DatabaseConnection connection; + /** + * Constructs a new {@code FeedbackDAO} with the given database connection. + * + * @param connection the {@link DatabaseConnection} to use for executing queries; must not be + * {@code null} + */ public MessageDAO(DatabaseConnection connection){ this.connection = connection; } + + /** + * Uses a repeated INSERT-query to send a message to all donor (as in Users that have donated). + * + *

+ * First grabs all donors (all Users that have donated to the charity) ids by getting + * the Charity_id throught the attribute. + *

+ * @param message Message object contain all relevant info, including the Charity id. + * @return True or False based on if it succeeded or not + */ public boolean addMessage(Message message){ // First fetch all unique donors for this charity List donorIds = getDonorIdsForCharity(message.getFrom().getUUID().toString()); @@ -51,6 +73,18 @@ public boolean addMessage(Message message){ } } + /** + * Private helper function used in {@code addMessage} method, which finds all distinct User Ids that have donated + * to given Charity. + * + *

+ * Uses a Select query with a distinct modifier to not get duplicate user_id. At the end uses an WHERE-clause + * to get the spesified charity. + *

+ * + * @param charityId Charity's I'd to search for. + * @return An empty or non-empty list of Ids as String. + */ private List getDonorIdsForCharity(String charityId) { List donorIds = new ArrayList<>(); String sql = """ diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/UserDAO.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/UserDAO.java index f82b2b9..b570d8b 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/UserDAO.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/UserDAO.java @@ -15,15 +15,35 @@ * and user settings to the settings database. * * @author Robin Strand Prestmo + * @author Adrian Paul Limpiado Balunan */ public class UserDAO { private final DatabaseConnection connection; + /** + * Constructs a new {@code UserDAO} with the given database connection. + * + * @param connection the {@link DatabaseConnection} to use for executing queries; must not be + * {@code null} + */ public UserDAO(DatabaseConnection connection) { this.connection = connection; } + /** + * Uses a select query to check if the provided Email already exists in {@code User} table. Return value is based on if finds a row or not. + * + *

+ * Check if email is a valid email. + * Uses a Select query to get a row containing the provided email. + * If it exists, return true. + * If not, return false + * Used in tandem with {@code LoginPageController} prevent duplicate Emails appearing on database. + *

+ * @param email Email in question. Check + * @return + */ public boolean isEmailTaken(String email){ if (email == null || email.isBlank() || !email.contains("@") || !email.contains(".")) { throw new IllegalArgumentException( @@ -49,64 +69,7 @@ public boolean isEmailTaken(String email){ } return false; } - 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; - } /** * Retrieves a single {@link User} from the database by their UUID. @@ -600,6 +563,14 @@ INSERT INTO Settings ( } } + /** + * Updates the Users settings in the {@code Settings} table. New values are stored in the Settings object. + * + * @param user User that is going to recieve changes + * @param settings Settings object containing new attributes + * @return True or False based on if the update succeed or not + */ + public boolean updateUserSettings(User user, Settings settings){ Connection conn = null; String sql = """ @@ -628,6 +599,12 @@ public boolean updateUserSettings(User user, Settings settings){ } + /** + * Updates the Users name, email and password in the {@code User} table. New values are stored in oncoming User param. + * + * @param user User that is going to recieve changes + * @return True or False based on if the update succeed or not + */ public boolean updateUserDetails(User user){ Connection conn = null; String sql = """ diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/service/AuthenticationService.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/service/AuthenticationService.java index a54f9a1..0d96d14 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/service/AuthenticationService.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/service/AuthenticationService.java @@ -1,6 +1,8 @@ package ntnu.systemutvikling.team6.service; +import ntnu.systemutvikling.team6.database.DAO.CharityUserDAO; import ntnu.systemutvikling.team6.database.DAO.UserDAO; +import ntnu.systemutvikling.team6.database.DatabaseConnection; import ntnu.systemutvikling.team6.models.Charity; import ntnu.systemutvikling.team6.models.user.Inbox; import ntnu.systemutvikling.team6.models.user.Role; @@ -50,7 +52,8 @@ public boolean login(String email, String password){ if (user != null){ currentUser = user; - isCharityUser = userDataAcsessObject.getUserCharityUser(currentUser.getId().toString()); + CharityUserDAO charityUserDAO = new CharityUserDAO(new DatabaseConnection()); + isCharityUser = charityUserDAO.getUserCharityUser(currentUser.getId().toString()); if (isCharityUser != null){ currentUser.setRole(Role.CHARITY_USER); }