From 35f72aa90a7030d5dc498f333b1306518a78a4f5 Mon Sep 17 00:00:00 2001 From: AdrianBalunan Date: Tue, 21 Apr 2026 08:14:00 +0200 Subject: [PATCH] Feat: We'll see if feedback work/messages work --- .../components/FeedbackCardController.java | 27 ++ .../profileOrgInboxController.java | 138 ++++++++++ .../team6/database/DAO/MessageDAO.java | 76 ++++++ .../team6/models/user/Role.java | 1 + .../fxml/components/feedbackCard.fxml | 40 +++ .../resources/fxml/profile_org_Inbox.fxml | 254 +++++------------- 6 files changed, 349 insertions(+), 187 deletions(-) create mode 100644 helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/components/FeedbackCardController.java create mode 100644 helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/profileCharity/profileOrgInboxController.java create mode 100644 helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/MessageDAO.java create mode 100644 helpmehelpapplication/src/main/resources/fxml/components/feedbackCard.fxml diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/components/FeedbackCardController.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/components/FeedbackCardController.java new file mode 100644 index 00000000..61bd7079 --- /dev/null +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/components/FeedbackCardController.java @@ -0,0 +1,27 @@ +package ntnu.systemutvikling.team6.controller.components; + +import javafx.fxml.FXML; +import javafx.scene.control.Label; +import ntnu.systemutvikling.team6.models.Feedback; +import ntnu.systemutvikling.team6.models.user.Message; + +public class FeedbackCardController extends BaseController { + @FXML private Label usernameLabel; + @FXML private Label dateLabel; + @FXML private Label commentLabel; + + private Feedback feedback; + + @Override + protected void authTokenisSet() { + + } + + public void setMessage(Feedback feedback) { + this.feedback = feedback; + + usernameLabel.setText(feedback.isAnonymous() ? "Anonymous Doner" : feedback.getUser().getUsername()); + dateLabel.setText(feedback.getDate().toString()); + commentLabel.setText(feedback.getComment()); + } +} 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 new file mode 100644 index 00000000..0bc6db9f --- /dev/null +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/profileCharity/profileOrgInboxController.java @@ -0,0 +1,138 @@ +package ntnu.systemutvikling.team6.controller.profileCharity; + +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.control.TextArea; +import javafx.scene.control.TextField; +import javafx.scene.layout.FlowPane; +import javafx.stage.Stage; +import ntnu.systemutvikling.team6.controller.components.*; +import ntnu.systemutvikling.team6.database.DAO.MessageDAO; +import ntnu.systemutvikling.team6.database.DatabaseConnection; +import ntnu.systemutvikling.team6.database.Readers.CharitySelect; +import ntnu.systemutvikling.team6.models.Charity; +import ntnu.systemutvikling.team6.models.Feedback; +import ntnu.systemutvikling.team6.models.user.Inbox; +import ntnu.systemutvikling.team6.models.user.Message; +import ntnu.systemutvikling.team6.models.user.User; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +public class profileOrgInboxController extends BaseController { + @FXML + private NavbarController navbarController; + @FXML + private FlowPane cardsContainer; + @FXML private Label charityNameLabel; + @FXML private TextField messageTitleField; + @FXML private TextArea messageContentField; + + + @Override + protected void authTokenisSet() { + if (!isLoggedin() || authToken.isCharityUser() == null) { + 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() { + Charity usersCharity = authToken.isCharityUser(); + charityNameLabel.setText(usersCharity.getName()); + + // Messages + DatabaseConnection conn = new DatabaseConnection(); + CharitySelect charitySelect = new CharitySelect(conn); + ArrayList feedbacks = charitySelect.getFeedbackforCharityUUID(authToken.isCharityUser().getUUID().toString()); + displayFeedbacks(feedbacks); + } + + private void displayFeedbacks(ArrayList feedbacks){ + cardsContainer.getChildren().clear(); + if (feedbacks.isEmpty()) { + Label empty = new Label("You have no Feedbacks"); + empty.setStyle("-fx-text-fill: #777777; -fx-font-size: 14;"); + cardsContainer.getChildren().add(empty); + } + + for (Feedback feedback : feedbacks) { + try { + FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/components/feedbackCard.fxml")); + Parent card = loader.load(); + FeedbackCardController cardController = loader.getController(); + cardController.setMessage(feedback); + cardController.setAuthToken(authToken); + cardsContainer.getChildren().add(card); + } catch (IOException e) { + throw new RuntimeException("Could not load organization card.", e); + } + } + } + + @FXML + private void handleSendMessage(ActionEvent event) { + String title = messageTitleField.getText().trim(); + String content = messageContentField.getText().trim(); + + if (title.isBlank() || content.isBlank()) { + showAlert(Alert.AlertType.ERROR, "Empty fields", "Please fill in both title and message."); + return; + } + + try { + DatabaseConnection conn = new DatabaseConnection(); + MessageDAO messageDAO = new MessageDAO(conn); + Charity charity = authToken.isCharityUser(); + Message message = new Message(title, authToken.isCharityUser(), content); + messageDAO.addMessage(message, authToken.getCurrentUser().getId().toString()); + showAlert(Alert.AlertType.INFORMATION, "Sent!", "Your message has been sent to all donors."); + messageTitleField.clear(); + messageContentField.clear(); + } catch (Exception e) { + e.printStackTrace(); + showAlert(Alert.AlertType.ERROR, "Error", "Something went wrong sending the message."); + } + } + + // Sidebar Methods + @FXML + private void switchToPaymentsPage(ActionEvent event) { + LoaderScene.LoadScene("profile_org_Payments", event, null, null, authToken); + } + + @FXML + private void switchToEditPage(ActionEvent event) { + LoaderScene.LoadScene("profile_org_edit", event, null, null, authToken); + } + + @FXML + private void switchToSettingsPage(ActionEvent event) { + LoaderScene.LoadScene("profile_user_Settings", event, null, null, authToken); + } + + + @FXML + private void handleLogout(ActionEvent event) { + authToken.logout(); + showAlert(Alert.AlertType.INFORMATION, "Logging out", "Logging out..."); + LoaderScene.LoadScene("FrontPage", event, null, null, authToken); + } +} 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 new file mode 100644 index 00000000..788b812e --- /dev/null +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/MessageDAO.java @@ -0,0 +1,76 @@ +package ntnu.systemutvikling.team6.database.DAO; + +import ntnu.systemutvikling.team6.database.DatabaseConnection; +import ntnu.systemutvikling.team6.models.user.Message; + +import java.sql.Connection; +import java.sql.Date; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +public class MessageDAO { + private final DatabaseConnection connection; + + public MessageDAO(DatabaseConnection connection){ + this.connection = connection; + } + public boolean addMessage(Message message, String user_id){ + // First fetch all unique donors for this charity + List donorIds = getDonorIdsForCharity(message.getFrom().getUUID().toString()); + + if (donorIds.isEmpty()) return false; + String sql = """ + INSERT INTO Messages + (UUID_message, message_title, message_content, message_date, sender_charity_id, user_id) + VALUES (?, ?, ?, ?, ?, ?) + """; + + try (Connection conn = connection.getMySqlConnection(); + PreparedStatement stmt = conn.prepareStatement(sql)) { + + for (String donorId : donorIds) { + stmt.setString(1, message.getId().toString()); + stmt.setString(2, message.getTitle()); + stmt.setString(3, message.getContent()); + stmt.setDate(4, Date.valueOf(message.getTimeAndDate())); + stmt.setString(5, message.getFrom().getUUID().toString()); + stmt.setString(6, user_id); + stmt.addBatch(); + } + + int[] results = stmt.executeBatch(); + return results.length > 0; + + } catch (SQLException e) { + e.printStackTrace(); + throw new RuntimeException("ERROR: Failed to send messages to donors."); + } + } + + private List getDonorIdsForCharity(String charityId) { + List donorIds = new ArrayList<>(); + String sql = """ + SELECT DISTINCT user_id + FROM Donations + WHERE charity_id = ? + """; + + try (Connection conn = connection.getMySqlConnection(); + PreparedStatement stmt = conn.prepareStatement(sql)) { + + stmt.setString(1, charityId); + try (ResultSet rs = stmt.executeQuery()) { + while (rs.next()) { + donorIds.add(rs.getString("user_id")); + } + } + } catch (SQLException e) { + e.printStackTrace(); + throw new RuntimeException("ERROR: Failed to fetch donor IDs."); + } + return donorIds; + } +} diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/models/user/Role.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/models/user/Role.java index 94e0b228..c68752d6 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/models/user/Role.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/models/user/Role.java @@ -8,4 +8,5 @@ public enum Role { NORMAL_USER, CHARITY_USER, + ADMIN, } diff --git a/helpmehelpapplication/src/main/resources/fxml/components/feedbackCard.fxml b/helpmehelpapplication/src/main/resources/fxml/components/feedbackCard.fxml new file mode 100644 index 00000000..2029ce46 --- /dev/null +++ b/helpmehelpapplication/src/main/resources/fxml/components/feedbackCard.fxml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/helpmehelpapplication/src/main/resources/fxml/profile_org_Inbox.fxml b/helpmehelpapplication/src/main/resources/fxml/profile_org_Inbox.fxml index e369d53c..64629c95 100644 --- a/helpmehelpapplication/src/main/resources/fxml/profile_org_Inbox.fxml +++ b/helpmehelpapplication/src/main/resources/fxml/profile_org_Inbox.fxml @@ -19,7 +19,9 @@ - + + + @@ -34,80 +36,8 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + @@ -121,7 +51,7 @@ -