Work on add contact to contact list

This commit is contained in:
2025-06-12 02:01:06 +03:30
parent 3fdee8c102
commit fb19b55593
17 changed files with 1035 additions and 142 deletions
@@ -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;
}
}
}