Work on add contact to contact list
This commit is contained in:
@@ -75,4 +75,115 @@ public class ChannelDatabase {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static List<UUID> getSubscriberUUIDs(UUID channelInternalUUID) {
|
||||
List<UUID> subscriberIds = new ArrayList<>();
|
||||
String sql = "SELECT user_id FROM channel_subscribe WHERE channel_id = ?";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setObject(1, channelInternalUUID);
|
||||
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
subscriberIds.add((UUID) rs.getObject("user_id"));
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return subscriberIds;
|
||||
}
|
||||
|
||||
public static Channel findByChannelId(String channelId) {
|
||||
String sql = "SELECT * FROM channels WHERE channel_id = ?";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
|
||||
stmt.setString(1, channelId);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
|
||||
if (rs.next()) {
|
||||
Channel channel = new Channel();
|
||||
channel.setInternal_uuid(UUID.fromString(rs.getString("internal_uuid")));
|
||||
channel.setChannel_id(rs.getString("channel_id"));
|
||||
channel.setChannel_name(rs.getString("channel_name"));
|
||||
channel.setImage_url(rs.getString("image_url"));
|
||||
channel.setCreator_id(UUID.fromString(rs.getString("creator_id")));
|
||||
channel.setDescription(rs.getString("description"));
|
||||
channel.setCreated_at(rs.getTimestamp("created_at").toLocalDateTime());
|
||||
return channel;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public static boolean isUserSubscribed(UUID userId, UUID channelInternalId) {
|
||||
String sql = "SELECT * FROM channel_subscribe WHERE user_id = ? AND channel_id = ?";
|
||||
try (Connection conn = ConnectionDb.connect(); PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setObject(1, userId);
|
||||
stmt.setObject(2, channelInternalId);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
return rs.next();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void addSubscriber(UUID channelInternalId, UUID userId) {
|
||||
String sql = "INSERT INTO channel_subscribe (channel_id, user_id) VALUES (?, ?) ON CONFLICT DO NOTHING";
|
||||
try (Connection conn = ConnectionDb.connect(); PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setObject(1, channelInternalId);
|
||||
stmt.setObject(2, userId);
|
||||
stmt.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static UUID findInternalUUIDByChannelId(String channelId) {
|
||||
String sql = "SELECT internal_uuid FROM channels WHERE channel_id = ?";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setString(1, channelId);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
if (rs.next()) {
|
||||
return (UUID) rs.getObject("internal_uuid");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public static boolean addSubscriberToChannel(UUID userId, UUID channelUUID) {
|
||||
String sql = """
|
||||
INSERT INTO channel_subscribers (channel_id, user_id)
|
||||
VALUES (?, ?)
|
||||
ON CONFLICT DO NOTHING
|
||||
""";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setObject(1, channelUUID);
|
||||
stmt.setObject(2, userId);
|
||||
return stmt.executeUpdate() > 0;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -16,13 +16,17 @@ public class ContactDatabase {
|
||||
return ConnectionDb.connect();
|
||||
}
|
||||
|
||||
public static boolean addContact(UUID userId, UUID contactId) {
|
||||
String sql = """
|
||||
INSERT INTO contacts (user_id, contact_id)
|
||||
VALUES (?, ?)
|
||||
ON CONFLICT DO NOTHING
|
||||
""";
|
||||
|
||||
public boolean addContact(UUID user_id, UUID contact_id) {
|
||||
String sql = "INSERT INTO contacts (user_id, contact_id) VALUES (?, ?) ON CONFLICT DO NOTHING";
|
||||
try (Connection connection = getConnection()) {
|
||||
PreparedStatement stmt = connection.prepareStatement(sql);
|
||||
stmt.setObject(1, user_id);
|
||||
stmt.setObject(2,contact_id);
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setObject(1, userId);
|
||||
stmt.setObject(2, contactId);
|
||||
return stmt.executeUpdate() > 0;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
@@ -31,6 +35,7 @@ public class ContactDatabase {
|
||||
}
|
||||
|
||||
|
||||
|
||||
public boolean removeContact(UUID user_id, UUID contact_id) {
|
||||
String sql = "DELETE FROM contacts WHERE user_id = ? AND contact_id = ?";
|
||||
try (Connection connection = getConnection()) {
|
||||
@@ -94,7 +99,7 @@ public class ContactDatabase {
|
||||
}
|
||||
|
||||
|
||||
public boolean existsContact(UUID user_id, UUID contact_id) {
|
||||
public static boolean existsContact(UUID user_id, UUID contact_id) {
|
||||
String sql = "SELECT 1 FROM contacts WHERE user_id = ? AND contact_id = ? LIMIT 1"; // stop searching when find the first item in DB(LIMIT 1)
|
||||
try (Connection connection = getConnection()) {
|
||||
PreparedStatement stmt = connection.prepareStatement(sql);
|
||||
@@ -155,4 +160,6 @@ public class ContactDatabase {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -76,5 +76,111 @@ public class GroupDatabase {
|
||||
return result;
|
||||
}
|
||||
|
||||
public static List<UUID> getMemberUUIDs(UUID groupInternalUUID) {
|
||||
List<UUID> memberIds = new ArrayList<>();
|
||||
String sql = "SELECT user_id FROM group_members WHERE group_id = ?";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setObject(1, groupInternalUUID);
|
||||
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
memberIds.add((UUID) rs.getObject("user_id"));
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return memberIds;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static Group findByGroupId(String groupId) {
|
||||
String sql = "SELECT * FROM groups WHERE group_id = ?";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
|
||||
stmt.setString(1, groupId);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
|
||||
if (rs.next()) {
|
||||
Group group = new Group();
|
||||
group.setInternal_uuid(UUID.fromString(rs.getString("internal_uuid")));
|
||||
group.setGroup_id(rs.getString("group_id"));
|
||||
group.setGroup_name(rs.getString("group_name"));
|
||||
group.setImage_url(rs.getString("image_url"));
|
||||
group.setCreator_id(UUID.fromString(rs.getString("creator_id")));
|
||||
group.setDescription(rs.getString("description"));
|
||||
group.setCreated_at(rs.getTimestamp("created_at").toLocalDateTime());
|
||||
return group;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static boolean isUserInGroup(UUID userId, UUID groupInternalId) {
|
||||
String sql = "SELECT * FROM group_members WHERE user_id = ? AND group_id = ?";
|
||||
try (Connection conn = ConnectionDb.connect(); PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setObject(1, userId);
|
||||
stmt.setObject(2, groupInternalId);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
return rs.next();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public static void addMember(UUID groupInternalId, UUID userId) {
|
||||
String sql = "INSERT INTO group_members (group_id, user_id) VALUES (?, ?) ON CONFLICT DO NOTHING";
|
||||
try (Connection conn = ConnectionDb.connect(); PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setObject(1, groupInternalId);
|
||||
stmt.setObject(2, userId);
|
||||
stmt.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
public static UUID findInternalUUIDByGroupId(String groupId) {
|
||||
String sql = "SELECT internal_uuid FROM groups WHERE group_id = ?";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setString(1, groupId);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
if (rs.next()) {
|
||||
return (UUID) rs.getObject("internal_uuid");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public static boolean addMemberToGroup(UUID userId, UUID groupUUID) {
|
||||
String sql = """
|
||||
INSERT INTO group_members (group_id, user_id)
|
||||
VALUES (?, ?)
|
||||
ON CONFLICT DO NOTHING
|
||||
""";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setObject(1, groupUUID);
|
||||
stmt.setObject(2, userId);
|
||||
return stmt.executeUpdate() > 0;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,6 +12,41 @@ import java.util.stream.Collectors;
|
||||
public class MessageDatabase {
|
||||
|
||||
|
||||
public static void save(Message message) {
|
||||
String sql = """
|
||||
INSERT INTO messages (
|
||||
message_id, sender_id, receiver_type, receiver_id, content,
|
||||
message_type, file_url, send_at, status,
|
||||
reply_to_id, is_edited, original_message_id, forwarded_by, forwarded_from
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
|
||||
stmt.setObject(1, message.getMessage_id());
|
||||
stmt.setObject(2, message.getSender_id());
|
||||
stmt.setString(3, message.getReceiver_type());
|
||||
stmt.setObject(4, message.getReceiver_id());
|
||||
stmt.setString(5, message.getContent());
|
||||
stmt.setString(6, message.getMessage_type());
|
||||
stmt.setString(7, message.getFile_url());
|
||||
stmt.setObject(8, message.getSend_at());
|
||||
stmt.setString(9, message.getStatus());
|
||||
stmt.setObject(10, message.getReply_to_id());
|
||||
stmt.setBoolean(11, message.isIs_edited());
|
||||
stmt.setObject(12, message.getOriginal_message_id());
|
||||
stmt.setObject(13, message.getForwarded_by());
|
||||
stmt.setObject(14, message.getForwarded_from());
|
||||
|
||||
stmt.executeUpdate();
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.err.println("❌ Error saving message: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void markMessageAsRead(UUID messageId, UUID userId) {
|
||||
String sql = "INSERT INTO message_receipts (message_id, user_id) VALUES (?, ?) ON CONFLICT DO NOTHING";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
|
||||
@@ -228,7 +228,6 @@ public class userDatabase {
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static User findByInternalUUID(UUID internalUuid) {
|
||||
String sql = "SELECT * FROM users WHERE internal_uuid = ?";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user