From 23f53a91355c731b9d633387ec6366c54a3c1b0c Mon Sep 17 00:00:00 2001 From: AdrianBalunan Date: Tue, 21 Apr 2026 01:54:22 +0200 Subject: [PATCH] Feat: Inbox works and some minor fixes to stuff --- .../components/InboxCardController.java | 31 +++ .../profileUserInboxController.java | 111 ++++++++++ .../profileUserInterestController.java | 11 +- .../team6/database/Readers/UserSelect.java | 29 ++- .../resources/fxml/components/inboxCard.fxml | 43 ++++ .../resources/fxml/profile_user_inbox.fxml | 208 ++---------------- 6 files changed, 223 insertions(+), 210 deletions(-) create mode 100644 helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/components/InboxCardController.java create mode 100644 helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/profileUser/profileUserInboxController.java create mode 100644 helpmehelpapplication/src/main/resources/fxml/components/inboxCard.fxml diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/components/InboxCardController.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/components/InboxCardController.java new file mode 100644 index 00000000..21e435cb --- /dev/null +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/components/InboxCardController.java @@ -0,0 +1,31 @@ +package ntnu.systemutvikling.team6.controller.components; + +import javafx.event.ActionEvent; +import javafx.fxml.FXML; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import ntnu.systemutvikling.team6.models.Charity; +import ntnu.systemutvikling.team6.models.user.Message; + +public class InboxCardController extends BaseController{ + @FXML private Label messageFrom; + @FXML private Label messageTitle; + @FXML private Label messageContent; + @FXML private Label messageDate; + + private Message message; + + @Override + protected void authTokenisSet() { + + } + + public void setMessage(Message message) { + this.message = message; + + messageFrom.setText(message.getFrom().getName()); + messageTitle.setText(message.getTitle()); + messageContent.setStyle(message.getContent()); + messageDate.setText(message.getTimeAndDate().toString()); + } +} diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/profileUser/profileUserInboxController.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/profileUser/profileUserInboxController.java new file mode 100644 index 00000000..39352704 --- /dev/null +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/profileUser/profileUserInboxController.java @@ -0,0 +1,111 @@ +package ntnu.systemutvikling.team6.controller.profileUser; + +import javafx.application.Platform; +import javafx.event.ActionEvent; +import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.control.Alert; +import javafx.scene.control.Label; +import javafx.scene.layout.FlowPane; +import javafx.stage.Stage; +import ntnu.systemutvikling.team6.controller.components.*; +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.Inbox; +import ntnu.systemutvikling.team6.models.user.Message; +import ntnu.systemutvikling.team6.models.user.Settings; +import ntnu.systemutvikling.team6.models.user.User; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +public class profileUserInboxController extends BaseController { + @FXML private NavbarController navbarController; + @FXML private FlowPane cardsContainer; + @FXML private Label nameLabel; + @FXML private Label shortNameLabel; + + @Override + protected void authTokenisSet() { + if (!isLoggedin()){ + showAlert(Alert.AlertType.ERROR, "Not logged inn", "You need to be logged inn to donate."); + Platform.runLater(() -> { + Stage stage = (Stage) Stage.getWindows().stream() + .filter(w -> w.isShowing()) + .findFirst() + .orElse(null); + if (stage != null) { + LoaderScene.LoadScene("loginSite", stage, null, null, authToken); + } + }); + } + navbarController.setAuthToken(authToken); + populateFields(); + } + + public void populateFields() { + User user = authToken.getCurrentUser(); + // Names + nameLabel.setText(user.getUsername()); + shortNameLabel.setText(user.getUsername().substring(0, 2).toUpperCase().trim()); + + // Messages + Inbox inbox = authToken.getCurrentUser().getInbox(); + displayInbox(inbox); + } + + private void displayInbox(Inbox inbox) { + cardsContainer.getChildren().clear(); + List messages = inbox.getMessages(); + if(messages.isEmpty()){ + Label empty = new Label("You have no messages"); + empty.setStyle("-fx-text-fill: #777777; -fx-font-size: 14;"); + cardsContainer.getChildren().add(empty); + } + + for (Message message : messages) { + try { + FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/components/inboxCard.fxml")); + Parent card = loader.load(); + InboxCardController cardController = loader.getController(); + cardController.setMessage(message); + cardController.setAuthToken(authToken); + cardsContainer.getChildren().add(card); + } catch (IOException e) { + throw new RuntimeException("Could not load organization card.", e); + } + } + } + + // Sidebar Methods + @FXML + private void handleLogout(ActionEvent event){ + authToken.logout(); + showAlert(Alert.AlertType.INFORMATION, "Logging out", "Logging out..."); + LoaderScene.LoadScene("FrontPage", event, null, null, authToken); + } + + @FXML + private void switchToFrontPage(ActionEvent event){ + LoaderScene.LoadScene("FrontPage", event, null, null, authToken); + } + + @FXML + private void switchToInterestPage(ActionEvent event){ + LoaderScene.LoadScene("profile_user_interests", event, null, null, authToken); + } + + @FXML + private void switchToHistoryPage(ActionEvent event){ + LoaderScene.LoadScene("profile_user_history", event, null, null, authToken); + } + + @FXML + private void switchToSettingsPage(ActionEvent event){ + LoaderScene.LoadScene("profile_user_settings", event, null, null, authToken); + } +} 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 444dfd72..01458563 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,14 +66,9 @@ public void populateFields() { private void displayFavourites(ArrayList favourites) { cardsContainer.getChildren().clear(); if(favourites.isEmpty()){ - Charity noCharityMessageCharity = new Charity( - UUID.randomUUID().toString(), - "osdawdwa.com", - "You have no Favourites", - false, - "Verified" - ); - favourites.add(noCharityMessageCharity); + Label empty = new Label("You have no messages"); + empty.setStyle("-fx-text-fill: #777777; -fx-font-size: 14;"); + cardsContainer.getChildren().add(empty); } for (Charity charity : favourites) { 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 400d2bf9..8ac5cfdc 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 @@ -2,7 +2,6 @@ import java.sql.*; import java.time.LocalDate; -import java.util.ArrayList; import java.util.HashSet; import java.util.UUID; import ntnu.systemutvikling.team6.database.DatabaseConnection; @@ -39,8 +38,7 @@ public boolean isEmailTaken(String email){ throw new IllegalArgumentException( "Email cannot be null or blank," + " and must contain '@' and '.'"); } - try (Connection conn = connection.getMySqlConnection(); - Statement stmt = conn.createStatement()) { + try (Connection conn = connection.getMySqlConnection()) { String mysql = """ @@ -95,11 +93,11 @@ public User getUserFromDBEmailAndPassword(String email, String password) { s.UUID_user, s.isAnonymous, s.language, s.lightmode, m.UUID_message, m.message_title, m.message_content, m.message_date, m.sender_user_id, m.sender_charity_id, m.user_id, cv.charity_name, cv.charity_link, cv.description, cv.logoURL, cv.key_values, cv.logoBLOB, - c.UUID_charities, c.org_number, c.pre_approved, c.status, + c.UUID_charities, c.org_number, c.pre_approved, c.status FROM User u LEFT JOIN Settings s ON u.UUID_User = s.UUID_user LEFT JOIN Messages m ON u.UUID_User = m.user_id - LEFT JOIN Charity c ON m.sender_charity_id = c.UUID_charities + LEFT JOIN Charities c ON m.sender_charity_id = c.UUID_charities LEFT JOIN CharityVanity cv ON c.UUID_charities = cv.UUID_charity WHERE u.user_email = ?; """; @@ -109,7 +107,7 @@ public User getUserFromDBEmailAndPassword(String email, String password) { ResultSet rs = stmt.executeQuery(); - ArrayList addedMessageIds = new ArrayList<>(); + HashSet addedMessageIds = new HashSet<>(); while (rs.next()) { String userId = rs.getString("UUID_User"); System.out.println(rs.getString("user_name")); @@ -201,11 +199,11 @@ public User getUserFromDBUuid(String user_id) { s.UUID_user, s.isAnonymous, s.language, s.lightmode, m.UUID_message, m.message_title, m.message_content, m.message_date, m.sender_user_id, m.sender_charity_id, m.user_id, cv.charity_name, cv.charity_link, cv.description, cv.logoURL, cv.key_values, cv.logoBLOB, - c.UUID_charities, c.org_number, c.pre_approved, c.status, + c.UUID_charities, c.org_number, c.pre_approved, c.status FROM User u LEFT JOIN Settings s ON u.UUID_User = s.UUID_user LEFT JOIN Messages m ON u.UUID_User = m.user_id - LEFT JOIN Charity c ON m.sender_charity_id = c.UUID_charities + LEFT JOIN Charities c ON m.sender_charity_id = c.UUID_charities LEFT JOIN CharityVanity cv ON c.UUID_charities = cv.UUID_charity WHERE u.UUID_User = ?; """; @@ -214,7 +212,7 @@ public User getUserFromDBUuid(String user_id) { ResultSet rs = stmt.executeQuery(); String lastUserid = null; - ArrayList addedMessageIds = new ArrayList<>(); + HashSet addedMessageIds = new HashSet<>(); while (rs.next()) { String userId = rs.getString("UUID_User"); if (lastUserid == null || !userId.equals(lastUserid)) { @@ -300,11 +298,11 @@ public UserRegistry getUsersFromDB() { s.UUID_user, s.isAnonymous, s.language, s.lightmode, m.UUID_message, m.message_title, m.message_content, m.message_date, m.sender_user_id, m.sender_charity_id, m.user_id, cv.charity_name, cv.charity_link, cv.description, cv.logoURL, cv.key_values, cv.logoBLOB, - c.UUID_charities, c.org_number, c.pre_approved, c.status, + c.UUID_charities, c.org_number, c.pre_approved, c.status FROM User u LEFT JOIN Settings s ON u.UUID_User = s.UUID_user LEFT JOIN Messages m ON u.UUID_User = m.user_id - LEFT JOIN Charity c ON m.sender_charity_id = c.UUID_charities + LEFT JOIN Charities c ON m.sender_charity_id = c.UUID_charities LEFT JOIN CharityVanity cv ON c.UUID_charities = cv.UUID_charity """; Statement stmt = conn.createStatement(); @@ -312,7 +310,7 @@ public UserRegistry getUsersFromDB() { User currentUser = null; String lastUserid = null; - ArrayList addedMessageIds = new ArrayList<>(); + HashSet addedMessageIds = new HashSet<>(); while (rs.next()) { String userId = rs.getString("UUID_User"); @@ -395,7 +393,7 @@ public Settings getSettingsForUser(String user_id) { conn = connection.getMySqlConnection(); String sql_query = """ - SELECT User_UUID_User, isAnonymous, language, lightmode FROM Settings + SELECT UUID_user, isAnonymous, language, lightmode FROM Settings WHERE UUID_user = ?; """; PreparedStatement stmt = conn.prepareStatement(sql_query); @@ -441,11 +439,10 @@ public Inbox getInboxForUser(String user_id) { SELECT m.UUID_message, m.message_title, m.message_content, m.message_date, m.sender_user_id, m.sender_charity_id, m.user_id, cv.charity_name, cv.charity_link, cv.description, cv.logoURL, cv.key_values, cv.logoBLOB, - c.UUID_charities, c.org_number, c.pre_approved, c.status, + c.UUID_charities, c.org_number, c.pre_approved, c.status FROM Messages m - LEFT JOIN Charity c ON m.sender_charity_id = c.UUID_charities + LEFT JOIN Charities c ON m.sender_charity_id = c.UUID_charities LEFT JOIN CharityVanity cv ON c.UUID_charities = cv.UUID_charity - SELECT UUID_message, message_title, message_content, message_date, sender_user_id, sender_charity_id, user_id FROM Messages WHERE user_id = ?; """; PreparedStatement stmt = conn.prepareStatement(sql_query); diff --git a/helpmehelpapplication/src/main/resources/fxml/components/inboxCard.fxml b/helpmehelpapplication/src/main/resources/fxml/components/inboxCard.fxml new file mode 100644 index 00000000..810e370e --- /dev/null +++ b/helpmehelpapplication/src/main/resources/fxml/components/inboxCard.fxml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/helpmehelpapplication/src/main/resources/fxml/profile_user_inbox.fxml b/helpmehelpapplication/src/main/resources/fxml/profile_user_inbox.fxml index 40992efc..14a8630d 100644 --- a/helpmehelpapplication/src/main/resources/fxml/profile_user_inbox.fxml +++ b/helpmehelpapplication/src/main/resources/fxml/profile_user_inbox.fxml @@ -1,12 +1,8 @@ - - - - @@ -18,7 +14,9 @@ - + + + @@ -33,80 +31,8 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + @@ -119,8 +45,14 @@ - @@ -129,11 +61,11 @@ - -