Manage data after login

This commit is contained in:
2025-06-08 13:58:43 +03:30
parent e9a6789dfa
commit 7a7141866e
20 changed files with 1175 additions and 33 deletions
@@ -0,0 +1,76 @@
package org.to.telegramfinalproject.Database;
import org.to.telegramfinalproject.Models.Channel;
import org.to.telegramfinalproject.Models.Group;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class ChannelDatabase {
public static List<Channel> getChannelsByUser(UUID internalUuid) {
List<Channel> channels = new ArrayList<>();
String sql = """
SELECT c.* FROM channels c
JOIN channel_subscribers cs ON c.internal_uuid = cs.channel_id
WHERE cs.user_id = ?
""";
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, internalUuid);
ResultSet rs = stmt.executeQuery();
while (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.setCreator_id(UUID.fromString(rs.getString("creator_id")));
channel.setImage_url(rs.getString("image_url"));
channel.setDescription(rs.getString("description"));
channel.setCreated_at(rs.getTimestamp("created_at").toLocalDateTime());
channels.add(channel);
}
} catch (SQLException e) {
e.printStackTrace();
}
return channels;
}
public static List<Channel> searchChannels(String keyword) {
List<Channel> result = new ArrayList<>();
String sql = "SELECT * FROM channels WHERE channel_name ILIKE ? OR channel_id ILIKE ?";
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, "%" + keyword + "%");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Channel channel = new Channel(
UUID.fromString(rs.getString("internal_uuid")),
rs.getString("channel_name"),
UUID.fromString(rs.getString("creator_id")),
rs.getTimestamp("created_at").toLocalDateTime()
);
channel.setChannel_id(rs.getString("channel_id"));
channel.setImage_url(rs.getString("image_url"));
channel.setDescription(rs.getString("description"));
result.add(channel);
}
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
}
@@ -0,0 +1,158 @@
package org.to.telegramfinalproject.Database;
import org.to.telegramfinalproject.Models.Contact;
import org.to.telegramfinalproject.Models.User;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class ContactDatabase {
public ContactDatabase(){}
private static Connection getConnection() throws SQLException {
return ConnectionDb.connect();
}
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);
return stmt.executeUpdate() > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public boolean removeContact(UUID user_id, UUID contact_id) {
String sql = "DELETE FROM contacts WHERE user_id = ? AND contact_id = ?";
try (Connection connection = getConnection()) {
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setObject(1, user_id);
stmt.setObject(2, contact_id);
return stmt.executeUpdate() > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public boolean blockContact(UUID user_id, UUID contact_id) {
String sql = "UPDATE contacts SET is_blocked = TRUE WHERE user_id = ? AND contact_id = ?";
try (Connection connection = getConnection()) {
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setObject(1, user_id);
stmt.setObject(2, contact_id);
return stmt.executeUpdate() > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public boolean unblockContact(UUID user_id, UUID contact_id) {
String sql = "UPDATE contacts SET is_blocked = FALSE WHERE user_id = ? AND contact_id = ?";
try (Connection connection = getConnection()) {
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setObject(1, user_id);
stmt.setObject(2, contact_id);
return stmt.executeUpdate() > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public static List<Contact> getContacts(UUID user_id) {
List<Contact> contacts = new ArrayList<>();
String sql = "SELECT * FROM contacts WHERE user_id = ?";
try (Connection connection = getConnection()) {
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setObject(1, user_id);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
UUID contactId = (UUID) rs.getObject("contact_id");
boolean isBlocked = rs.getBoolean("is_blocked");
Timestamp addedAt = rs.getTimestamp("added_at");
Contact contact = new Contact(user_id, contactId);
contact.setIs_blocked(isBlocked);
contact.setAdd_at(addedAt.toLocalDateTime());
contacts.add(contact);
}
} catch (SQLException e) {
e.printStackTrace();
}
return contacts;
}
public 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);
stmt.setObject(1, user_id);
stmt.setObject(2, contact_id);
ResultSet rs = stmt.executeQuery();
return rs.next();
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public boolean isBlocked(UUID user_id, UUID contact_id) {
String sql = "SELECT is_blocked FROM contacts WHERE user_id = ? AND contact_id = ?";
try (Connection connection = getConnection()) {
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setObject(1, user_id);
stmt.setObject(2, contact_id);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
return rs.getBoolean("is_blocked");
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
public List<Contact> searchContacts(UUID user_id, String searchTerm) {
List<Contact> results = new ArrayList<>(); //ILIKE case-insensitive
String sql = """
SELECT c.contact_id, c.added_at, c.is_blocked
FROM contacts c
JOIN users u ON c.contact_id = u.internal_uuid
WHERE c.user_id = ? AND (u.user_id ILIKE ? OR u.profile_name ILIKE ?)
""";
try (Connection connection = getConnection()) {
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setObject(1, user_id);
stmt.setString(2, "%" + searchTerm + "%");
stmt.setString(3, "%" + searchTerm + "%");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
UUID contactId = (UUID) rs.getObject("contact_id");
boolean isBlocked = rs.getBoolean("is_blocked");
Timestamp addedAt = rs.getTimestamp("added_at");
Contact contact = new Contact(user_id, contactId);
contact.setIs_blocked(isBlocked);
contact.setAdd_at(addedAt.toLocalDateTime());
results.add(contact);
}
} catch (SQLException e) {
e.printStackTrace();
}
return results;
}
}
@@ -0,0 +1,78 @@
package org.to.telegramfinalproject.Database;
import org.to.telegramfinalproject.Models.Group;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class GroupDatabase {
public static List<Group> getGroupsByUser(UUID internalUuid) {
List<Group> groups = new ArrayList<>();
String sql = """
SELECT g.* FROM groups g
JOIN group_members gm ON g.internal_uuid = gm.group_id
WHERE gm.user_id = ?
""";
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, internalUuid);
ResultSet rs = stmt.executeQuery();
while (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.setCreator_id(UUID.fromString(rs.getString("creator_id")));
group.setImage_url(rs.getString("image_url"));
group.setDescription(rs.getString("description"));
group.setCreated_at(rs.getTimestamp("created_at").toLocalDateTime());
groups.add(group);
}
} catch (SQLException e) {
e.printStackTrace();
}
return groups;
}
public static List<Group> searchGroups(String keyword) {
List<Group> result = new ArrayList<>();
String sql = "SELECT * FROM groups WHERE group_name ILIKE ? OR group_id ILIKE ?";
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, "%" + keyword + "%");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Group group = new Group(
UUID.fromString(rs.getString("internal_uuid")),
rs.getString("group_name"),
UUID.fromString(rs.getString("creator_id")),
rs.getTimestamp("created_at").toLocalDateTime()
);
group.setGroup_id(rs.getString("group_id"));
group.setImage_url(rs.getString("image_url"));
group.setDescription(rs.getString("description"));
result.add(group);
}
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
}
@@ -0,0 +1,135 @@
package org.to.telegramfinalproject.Database;
import org.to.telegramfinalproject.Models.Message;
import java.sql.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class MessageDatabase {
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();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, messageId);
stmt.setObject(2, userId);
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static List<Message> getUnreadMessages(UUID userId) {
List<Message> messages = new ArrayList<>();
String sql = """
SELECT m.* FROM messages m
LEFT JOIN message_receipts r ON m.message_id = r.message_id AND r.user_id = ?
WHERE r.user_id IS NULL
AND (
(m.receiver_type = 'private' AND m.receiver_id = ?)
OR
(m.receiver_type = 'group' AND EXISTS (
SELECT 1 FROM group_members gm WHERE gm.group_id = m.receiver_id AND gm.user_id = ?
))
OR
(m.receiver_type = 'channel' AND EXISTS (
SELECT 1 FROM channel_subscribers cs WHERE cs.channel_id = m.receiver_id AND cs.user_id = ?
))
)
""";
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, userId);
stmt.setObject(2, userId);
stmt.setObject(3, userId);
stmt.setObject(4, userId);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
messages.add(new Message(
UUID.fromString(rs.getString("message_id")),
rs.getObject("sender_id") != null ? UUID.fromString(rs.getString("sender_id")) : null,
rs.getString("receiver_type"),
UUID.fromString(rs.getString("receiver_id")),
rs.getString("content"),
rs.getString("message_type"),
rs.getString("file_url"),
rs.getTimestamp("send_at").toLocalDateTime(),
rs.getString("status"),
rs.getObject("reply_to_id") != null ? UUID.fromString(rs.getString("reply_to_id")) : null,
rs.getBoolean("is_edited"),
rs.getObject("original_message_id") != null ? UUID.fromString(rs.getString("original_message_id")) : null,
rs.getObject("forwarded_by") != null ? UUID.fromString(rs.getString("forwarded_by")) : null,
rs.getObject("forwarded_from") != null ? UUID.fromString(rs.getString("forwarded_from")) : null
));
}
} catch (SQLException e) {
e.printStackTrace();
}
return messages;
}
public static LocalDateTime getLastMessageTimeBetween(UUID user1, UUID user2, String type) {
String sql = """
SELECT MAX(send_at) FROM messages
WHERE receiver_type = ?
AND (
(sender_id = ? AND receiver_id = ?)
OR (sender_id = ? AND receiver_id = ?)
)
""";
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, type);
stmt.setObject(2, user1);
stmt.setObject(3, user2);
stmt.setObject(4, user2);
stmt.setObject(5, user1);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
Timestamp ts = rs.getTimestamp(1);
return ts != null ? ts.toLocalDateTime() : null;
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static LocalDateTime getLastMessageTime(UUID receiverId, String type) {
String sql = "SELECT MAX(send_at) FROM messages WHERE receiver_id = ? AND receiver_type = ?";
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, receiverId);
stmt.setString(2, type);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
Timestamp ts = rs.getTimestamp(1);
return ts != null ? ts.toLocalDateTime() : null;
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
@@ -3,10 +3,7 @@ package org.to.telegramfinalproject.Database;
import org.to.telegramfinalproject.Models.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@@ -203,5 +200,87 @@ public class userDatabase {
return false;
}
}
public static void updateUserStatus(UUID uuid, String status) {
String sql = "UPDATE users SET status = ? WHERE internal_uuid = ?";
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, status);
stmt.setObject(2, uuid);
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void updateLastSeen(UUID uuid) {
String sql = "UPDATE users SET last_seen = CURRENT_TIMESTAMP WHERE internal_uuid = ?";
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, uuid);
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static User findByInternalUUID(UUID internalUuid) {
String sql = "SELECT * FROM users WHERE internal_uuid = ?";
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, internalUuid);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
User user = new User(
rs.getString("user_id"),
UUID.fromString(rs.getString("internal_uuid")),
rs.getString("username"),
rs.getString("password"),
rs.getString("profile_name")
);
user.setBio(rs.getString("bio"));
user.setImage_url(rs.getString("image_url"));
user.setStatus(rs.getString("status"));
Timestamp lastSeenTs = rs.getTimestamp("last_seen");
if (lastSeenTs != null) {
user.setLast_seen(lastSeenTs.toLocalDateTime());
}
return user;
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public List<User> searchUsers(String keyword) {
String query = "SELECT * FROM users WHERE user_id ILIKE ? OR profile_name ILIKE ?"; //(ILIKE) case_insensitive
List<User> result = new ArrayList<>();
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(query)) {
stmt.setString(1, "%" + keyword + "%");
stmt.setString(2, "%" + keyword + "%");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
result.add(extractUser(rs));
}
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
}