From 6cf34deafb27f0ddf870a523d3b59254477fcf5a Mon Sep 17 00:00:00 2001 From: AdrianBalunan Date: Tue, 21 Apr 2026 05:29:51 +0200 Subject: [PATCH] Feat: Implementing charityUser, new methods for showing its a charity user and smaller fixes --- .../controller/CreateUserPageController.java | 2 +- .../components/NavbarController.java | 13 ++++ .../profileUserHistoryController.java | 2 +- .../profileUserInterestController.java | 2 +- .../team6/database/DatabaseSetup.java | 34 +++++------ .../team6/database/Readers/UserSelect.java | 61 +++++++++++++++++++ .../team6/service/AuthenticationService.java | 8 ++- .../resources/fxml/components/navbar.fxml | 3 +- 8 files changed, 103 insertions(+), 22 deletions(-) diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/CreateUserPageController.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/CreateUserPageController.java index 3e54bf9..75dd33c 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/CreateUserPageController.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/CreateUserPageController.java @@ -73,7 +73,7 @@ private void handleCreateAccount(ActionEvent event){ Alert.AlertType.INFORMATION, "Sign up sucsess", "You have registered a new account! Please login with same credentials"); - LoaderScene.LoadScene("profile_user_settings", event, null, null, authToken); + LoaderScene.LoadScene("loginSite", event, null, null, authToken); } } diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/components/NavbarController.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/components/NavbarController.java index 1cbedf0..f9bbc34 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/components/NavbarController.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/components/NavbarController.java @@ -13,11 +13,16 @@ public class NavbarController extends BaseController { @FXML protected TextField frontSearchField; @FXML private Button loginButton; @FXML private Button profileButton; + @FXML private Button toCharityUserButton; @Override protected void authTokenisSet() { boolean loggedIn = super.isLoggedin(); if (loggedIn){ + if (authToken.isCharityUser() != null){ + toCharityUserButton.setVisible(true); + toCharityUserButton.setManaged(true); + } loginButton.setVisible(false); loginButton.setManaged(false); profileButton.setVisible(true); @@ -28,6 +33,8 @@ protected void authTokenisSet() { loginButton.setManaged(true); profileButton.setVisible(false); profileButton.setManaged(false); + toCharityUserButton.setVisible(false); + toCharityUserButton.setManaged(false); } } @@ -59,4 +66,10 @@ private void switchToLoginPage(ActionEvent event) { System.out.println("Click!"); LoaderScene.LoadScene("loginSite", event, null, null, authToken); } + + @FXML + private void switchToCharityUserPage(ActionEvent event) { + System.out.println("Click!"); + LoaderScene.LoadScene("loginSite", event, null, null, authToken); + } } diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/profileUser/profileUserHistoryController.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/profileUser/profileUserHistoryController.java index 8e167b4..59a6556 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/profileUser/profileUserHistoryController.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/profileUser/profileUserHistoryController.java @@ -65,7 +65,7 @@ private void displayDonations(DonationRegistry donationRegistry) { cardsContainer.getChildren().clear(); List donations = donationRegistry.getAllDonations(); if(donations.isEmpty()){ - Label empty = new Label("You have no messages"); + Label empty = new Label("You have no Donations"); empty.setStyle("-fx-text-fill: #777777; -fx-font-size: 14;"); cardsContainer.getChildren().add(empty); } diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/profileUser/profileUserInterestController.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/profileUser/profileUserInterestController.java index 8183cff..7ebffcf 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/profileUser/profileUserInterestController.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/profileUser/profileUserInterestController.java @@ -66,7 +66,7 @@ public void populateFields() { private void displayFavourites(ArrayList favourites) { cardsContainer.getChildren().clear(); if(favourites.isEmpty()){ - Label empty = new Label("You have no messages"); + Label empty = new Label("You have no favourited Charities"); empty.setStyle("-fx-text-fill: #777777; -fx-font-size: 14;"); cardsContainer.getChildren().add(empty); } diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DatabaseSetup.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DatabaseSetup.java index 46a444b..97c4391 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DatabaseSetup.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DatabaseSetup.java @@ -230,23 +230,23 @@ FOREIGN KEY (`Charities_UUID_charities`) -- ----------------------------------------------------- -- Table `apbaluna`.`CharityUsers` -- ----------------------------------------------------- - CREATE TABLE IF NOT EXISTS `apbaluna`.`CharityUsers` ( - `Charities_UUID_charities` CHAR(36) NOT NULL, - `User_UUID_User` CHAR(36) NOT NULL, - PRIMARY KEY (`Charities_UUID_charities`, `User_UUID_User`), - INDEX `fk_Charities_has_User_User1_idx` (`User_UUID_User` ASC) VISIBLE, - INDEX `fk_Charities_has_User_Charities1_idx` (`Charities_UUID_charities` ASC) VISIBLE, - CONSTRAINT `fk_Charities_has_User_Charities1` - FOREIGN KEY (`Charities_UUID_charities`) - REFERENCES `apbaluna`.`Charities` (`UUID_charities`) - ON DELETE NO ACTION - ON UPDATE NO ACTION, - CONSTRAINT `fk_Charities_has_User_User1` - FOREIGN KEY (`User_UUID_User`) - REFERENCES `apbaluna`.`User` (`UUID_User`) - ON DELETE NO ACTION - ON UPDATE NO ACTION) - ENGINE = InnoDB; + CREATE TABLE IF NOT EXISTS `apbaluna`.`CharityUsers` ( + `TheCharity` CHAR(36) NOT NULL, + `CharityUserId` CHAR(36) NOT NULL, + PRIMARY KEY (`TheCharity`, `CharityUserId`), + INDEX `fk_Charities_has_User_User1_idx` (`CharityUserId` ASC) VISIBLE, + INDEX `fk_Charities_has_User_Charities1_idx` (`TheCharity` ASC) VISIBLE, + CONSTRAINT `fk_Charities_has_User_Charities1` + FOREIGN KEY (`TheCharity`) + REFERENCES `apbaluna`.`Charities` (`UUID_charities`) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT `fk_Charities_has_User_User1` + FOREIGN KEY (`CharityUserId`) + REFERENCES `apbaluna`.`User` (`UUID_User`) + ON DELETE NO ACTION + ON UPDATE NO ACTION) + ENGINE = InnoDB; """; String charityVanityTable = """ diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/Readers/UserSelect.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/Readers/UserSelect.java index 8ac5cfd..8e212f1 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/Readers/UserSelect.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/Readers/UserSelect.java @@ -3,9 +3,11 @@ import java.sql.*; import java.time.LocalDate; import java.util.HashSet; +import java.util.Set; import java.util.UUID; 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.registry.UserRegistry; import ntnu.systemutvikling.team6.models.user.*; @@ -59,6 +61,65 @@ public boolean isEmailTaken(String email){ return false; } + public Charity getUserCharityUser(String uuid){ + if (uuid == null || uuid.isBlank()) { + throw new IllegalArgumentException( + "Email cannot be null or blank," + " and must contain '@' and '.'"); + } + 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 matching the given username and password. 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 85ddea6..cd9cbc2 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/service/AuthenticationService.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/service/AuthenticationService.java @@ -2,6 +2,7 @@ import ntnu.systemutvikling.team6.database.DAO.UserDAO; import ntnu.systemutvikling.team6.database.Readers.UserSelect; +import ntnu.systemutvikling.team6.models.Charity; import ntnu.systemutvikling.team6.models.user.Inbox; import ntnu.systemutvikling.team6.models.user.Role; import ntnu.systemutvikling.team6.models.user.Settings; @@ -24,6 +25,8 @@ public class AuthenticationService { /** The currently authenticated user, or {@code null} if no user is logged in. */ private User currentUser; + private Charity isCharityUser; + /** * Constructs an {@code AuthenticationService} with the specified data access objects. * @@ -42,7 +45,7 @@ public AuthenticationService(UserSelect userDataReader, UserDAO userDataSender) * and the method returns {@code true}. *

* - * @param username the username of the user attempting to log in + * @param email the username of the user attempting to log in * @param password the password of the user attempting to log in * @return {@code true} if authentication was successful; {@code false} otherwise */ @@ -51,6 +54,7 @@ public boolean login(String email, String password){ if (user != null){ currentUser = user; + isCharityUser = userDataReader.getUserCharityUser(currentUser.getId().toString()); System.out.println("User gotten"); return true; } @@ -115,4 +119,6 @@ public User getCurrentUser(){ public boolean isLoggedin(){ return currentUser != null; } + + public Charity isCharityUser(){return isCharityUser;} } diff --git a/helpmehelpapplication/src/main/resources/fxml/components/navbar.fxml b/helpmehelpapplication/src/main/resources/fxml/components/navbar.fxml index 741eb7d..164c455 100644 --- a/helpmehelpapplication/src/main/resources/fxml/components/navbar.fxml +++ b/helpmehelpapplication/src/main/resources/fxml/components/navbar.fxml @@ -52,11 +52,12 @@ - + +