Skip to content

Commit

Permalink
Fix: Major design change message contain the charity, instead of char…
Browse files Browse the repository at this point in the history
…ity id.
  • Loading branch information
AdrianBalunan committed Apr 20, 2026
1 parent ee8ba2e commit dc0737a
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -103,7 +109,7 @@ public User getUserFromDBEmailAndPassword(String email, String password) {


ResultSet rs = stmt.executeQuery();

ArrayList<String> addedMessageIds = new ArrayList<>();
while (rs.next()) {
String userId = rs.getString("UUID_User");
System.out.println(rs.getString("user_name"));
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -173,18 +198,23 @@ 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);
stmt.setString(1, user_id);
ResultSet rs = stmt.executeQuery();

String lastUserid = null;
ArrayList<String> addedMessageIds = new ArrayList<>();
while (rs.next()) {
String userId = rs.getString("UUID_User");
if (lastUserid == null || !userId.equals(lastUserid)) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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<String> addedMessageIds = new ArrayList<>();

while (rs.next()) {
String userId = rs.getString("UUID_User");
Expand All @@ -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) {
Expand Down Expand Up @@ -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 = ?;
""";
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ntnu.systemutvikling.team6.models.user;

import ntnu.systemutvikling.team6.models.Charity;

import java.time.LocalDate;
import java.util.UUID;

Expand All @@ -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.");
}

Expand All @@ -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();
}
Expand All @@ -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.");
}

Expand All @@ -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;
}
Expand All @@ -90,8 +92,8 @@ public String getTitle() {
return title;
}

public UUID getFrom() {
return fromCharityID;
public Charity getFrom() {
return fromCharity;
}

public String getContent() {
Expand Down

0 comments on commit dc0737a

Please sign in to comment.