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 dcd037a..400d2bf 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,6 +2,8 @@ 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; import ntnu.systemutvikling.team6.models.Charity; @@ -91,10 +93,14 @@ public User getUserFromDBEmailAndPassword(String email, String password) { SELECT u.UUID_User, u.user_name, u.user_email, u.user_password, u.role, 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 + 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, 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 CharityVanity cv ON c.UUID_charities = cv.UUID_charity WHERE u.user_email = ?; """; PreparedStatement stmt = conn.prepareStatement(sql_query); @@ -103,7 +109,7 @@ public User getUserFromDBEmailAndPassword(String email, String password) { ResultSet rs = stmt.executeQuery(); - + ArrayList addedMessageIds = new ArrayList<>(); while (rs.next()) { String userId = rs.getString("UUID_User"); System.out.println(rs.getString("user_name")); @@ -132,15 +138,34 @@ public User getUserFromDBEmailAndPassword(String email, String password) { user.setInbox(new Inbox()); } String messageId = rs.getString("UUID_message"); - if (messageId != null) { - Message message = - new Message( - rs.getString("message_title"), - UUID.fromString(rs.getString("sender_charity_id")), - rs.getString("message_content"), - LocalDate.parse(rs.getString("message_date"))); + if (messageId != null && !addedMessageIds.contains(messageId)) { + addedMessageIds.add(messageId); + Charity fromCharity = null; + String charityId = rs.getString("UUID_charities"); + if (charityId != null) { + fromCharity = new Charity( + charityId, + 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") + ); + } - user.getInbox().addMessage(message); + if (fromCharity != null) { + Message message = new Message( + rs.getString("message_title"), + fromCharity, + rs.getString("message_content"), + LocalDate.parse(rs.getString("message_date")) + ); + user.getInbox().addMessage(message); + } } } } catch (SQLException e) { @@ -173,11 +198,15 @@ public User getUserFromDBUuid(String user_id) { """ SELECT u.UUID_User, u.user_name, u.user_email, u.user_password, u.role, - s.User_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 + 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, FROM User u - LEFT JOIN Settings s ON u.UUID_User = s.User_UUID_user + 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 CharityVanity cv ON c.UUID_charities = cv.UUID_charity WHERE u.UUID_User = ?; """; PreparedStatement stmt = conn.prepareStatement(sql_query); @@ -185,6 +214,7 @@ public User getUserFromDBUuid(String user_id) { ResultSet rs = stmt.executeQuery(); String lastUserid = null; + ArrayList addedMessageIds = new ArrayList<>(); while (rs.next()) { String userId = rs.getString("UUID_User"); if (lastUserid == null || !userId.equals(lastUserid)) { @@ -207,15 +237,34 @@ public User getUserFromDBUuid(String user_id) { lastUserid = userId; } String messageId = rs.getString("UUID_message"); - if (messageId != null) { - Message message = - new Message( - rs.getString("message_title"), - UUID.fromString(rs.getString("sender_charity_id")), - rs.getString("message_content"), - LocalDate.parse(rs.getString("message_date"))); + if (messageId != null && !addedMessageIds.contains(messageId)) { + addedMessageIds.add(messageId); + Charity fromCharity = null; + String charityId = rs.getString("UUID_charities"); + if (charityId != null) { + fromCharity = new Charity( + charityId, + 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") + ); + } - user.getInbox().addMessage(message); + if (fromCharity != null) { + Message message = new Message( + rs.getString("message_title"), + fromCharity, + rs.getString("message_content"), + LocalDate.parse(rs.getString("message_date")) + ); + user.getInbox().addMessage(message); + } } } } catch (SQLException e) { @@ -248,17 +297,22 @@ public UserRegistry getUsersFromDB() { """ SELECT u.UUID_User, u.user_name, u.user_email, u.user_password, u.role, - s.User_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 + 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, FROM User u - LEFT JOIN Settings s ON u.UUID_User = s.User_UUID_user + 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 CharityVanity cv ON c.UUID_charities = cv.UUID_charity """; Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql_query); User currentUser = null; String lastUserid = null; + ArrayList addedMessageIds = new ArrayList<>(); while (rs.next()) { String userId = rs.getString("UUID_User"); @@ -284,15 +338,34 @@ public UserRegistry getUsersFromDB() { lastUserid = userId; } String messageId = rs.getString("UUID_message"); - if (messageId != null) { - Message message = - new Message( - rs.getString("message_title"), - UUID.fromString(rs.getString("sender_charity_id")), - rs.getString("message_content"), - LocalDate.parse(rs.getString("message_date"))); + if (messageId != null && !addedMessageIds.contains(messageId)) { + addedMessageIds.add(messageId); + Charity fromCharity = null; + String charityId = rs.getString("UUID_charities"); + if (charityId != null) { + fromCharity = new Charity( + charityId, + 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") + ); + } - currentUser.getInbox().addMessage(message); + if (fromCharity != null) { + Message message = new Message( + rs.getString("message_title"), + fromCharity, + rs.getString("message_content"), + LocalDate.parse(rs.getString("message_date")) + ); + currentUser.getInbox().addMessage(message); + } } } } catch (SQLException e) { @@ -365,6 +438,13 @@ public Inbox getInboxForUser(String user_id) { conn = connection.getMySqlConnection(); String sql_query = """ + 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, + FROM Messages m + LEFT JOIN Charity 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 = ?; """; @@ -373,13 +453,32 @@ public Inbox getInboxForUser(String user_id) { ResultSet rs = stmt.executeQuery(); while (rs.next()) { - Message message = - new Message( - rs.getString("message_title"), - UUID.fromString(rs.getString("sender_charity_id")), - rs.getString("message_content"), - LocalDate.parse(rs.getString("message_date"))); - inbox.addMessage(message); + Charity fromCharity = null; + String charityId = rs.getString("UUID_charities"); + if (charityId != null) { + fromCharity = new Charity( + charityId, + 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") + ); + } + + if (fromCharity != null) { + Message message = new Message( + rs.getString("message_title"), + fromCharity, + rs.getString("message_content"), + LocalDate.parse(rs.getString("message_date")) + ); + inbox.addMessage(message); + } } } catch (SQLException e) { e.printStackTrace(); diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/models/user/Message.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/models/user/Message.java index 3a2a75d..9a2e07c 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/models/user/Message.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/models/user/Message.java @@ -1,5 +1,7 @@ package ntnu.systemutvikling.team6.models.user; +import ntnu.systemutvikling.team6.models.Charity; + import java.time.LocalDate; import java.util.UUID; @@ -13,26 +15,26 @@ public class Message { private final UUID id; private final String title; - private final UUID fromCharityID; + private final Charity fromCharity; private final String content; private final LocalDate timeAndDate; /** - * Creates a message with a unique identifier. The message includes a title, a string who it's + * Creates a message with a unique identifier. The message includes a title, the Charity who it's * from, content and the time and date. * * @param title the title of the message - * @param from who the message is from + * @param charity who the message is from * @param content the content of the message * @throws IllegalArgumentException if title, from or content is null or blank. */ - public Message(String title, UUID from, String content) { + public Message(String title, Charity charity, String content) { if (title == null || title.isBlank()) { throw new IllegalArgumentException("Title cannot be null or blank."); } - if (from == null) { + if (charity == null) { throw new IllegalArgumentException("From cannot be null or blank."); } @@ -42,7 +44,7 @@ public Message(String title, UUID from, String content) { this.id = UUID.randomUUID(); this.title = title; - this.fromCharityID = from; + this.fromCharity = charity; this.content = content; this.timeAndDate = LocalDate.now(); } @@ -52,18 +54,18 @@ public Message(String title, UUID from, String content) { * from, content and the time and date. This one creates a message that has been created before. * * @param title the title of the message - * @param from who the message is from + * @param charity who the message is from * @param content the content of the message * @param date date of when the message was created * @throws IllegalArgumentException if title, from or content is null or blank. */ - public Message(String title, UUID from, String content, LocalDate date) { + public Message(String title, Charity charity, String content, LocalDate date) { if (title == null || title.isBlank()) { throw new IllegalArgumentException("Title cannot be null or blank."); } - if (from == null) { + if (charity == null) { throw new IllegalArgumentException("From cannot be null or blank."); } @@ -77,7 +79,7 @@ public Message(String title, UUID from, String content, LocalDate date) { this.id = UUID.randomUUID(); this.title = title; - this.fromCharityID = from; + this.fromCharity = charity; this.content = content; this.timeAndDate = date; } @@ -90,8 +92,8 @@ public String getTitle() { return title; } - public UUID getFrom() { - return fromCharityID; + public Charity getFrom() { + return fromCharity; } public String getContent() {