Merge branch 'develop' into Sidebar-Menu
# Conflicts: # src/main/java/org/to/telegramfinalproject/Client/ActionHandler.java # src/main/java/org/to/telegramfinalproject/Database/PrivateChatDatabase.java # src/main/java/org/to/telegramfinalproject/Server/ClientHandler.java
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
package org.to.telegramfinalproject.Database;
|
||||
|
||||
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 ArchivedChatDatabase {
|
||||
public static boolean archiveChat(UUID userId, UUID chatId, String chatType) {
|
||||
String sql = "INSERT INTO archived_chats (user_id, chat_id, chat_type) VALUES (?, ?, ?) ON CONFLICT DO NOTHING";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setObject(1, userId);
|
||||
ps.setObject(2, chatId);
|
||||
ps.setString(3, chatType);
|
||||
return ps.executeUpdate() > 0;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static boolean unarchiveChat(UUID userId, UUID chatId) {
|
||||
String sql = "DELETE FROM archived_chats WHERE user_id = ? AND chat_id = ?";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setObject(1, userId);
|
||||
ps.setObject(2, chatId);
|
||||
return ps.executeUpdate() > 0;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isChatArchived(UUID userId, UUID chatId) {
|
||||
String sql = "SELECT 1 FROM archived_chats WHERE user_id = ? AND chat_id = ?";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setObject(1, userId);
|
||||
ps.setObject(2, chatId);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
return rs.next();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static List<UUID> getArchivedChats(UUID userId) {
|
||||
List<UUID> list = new ArrayList<>();
|
||||
String sql = "SELECT chat_id FROM archived_chats WHERE user_id = ?";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setObject(1, userId);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
while (rs.next()) {
|
||||
list.add(UUID.fromString(rs.getString("chat_id")));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.to.telegramfinalproject.Database;
|
||||
|
||||
import org.to.telegramfinalproject.Models.FileAttachment;
|
||||
import org.to.telegramfinalproject.Models.Message;
|
||||
|
||||
import java.sql.*;
|
||||
@@ -12,41 +13,106 @@ 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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""";
|
||||
|
||||
|
||||
|
||||
public static boolean insertMessage(UUID messageId, UUID senderId, UUID receiverId,
|
||||
String receiverType, String content, String messageType) {
|
||||
String sql = "INSERT INTO messages (message_id, sender_id, receiver_type, receiver_id, content, message_type) " +
|
||||
"VALUES (?, ?, ?, ?, ?, ?)";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
PreparedStatement ps = 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());
|
||||
ps.setObject(1, messageId);
|
||||
ps.setObject(2, senderId);
|
||||
ps.setString(3, receiverType);
|
||||
ps.setObject(4, receiverId);
|
||||
ps.setString(5, content);
|
||||
ps.setString(6, messageType);
|
||||
|
||||
stmt.executeUpdate();
|
||||
return ps.executeUpdate() > 0;
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.err.println("❌ Error saving message: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static boolean insertAttachments(UUID messageId, List<FileAttachment> attachments) {
|
||||
String sql = "INSERT INTO message_attachments (attachment_id, message_id, file_url, file_type) " +
|
||||
"VALUES (?, ?, ?, ?)";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
|
||||
for (FileAttachment att : attachments) {
|
||||
ps.setObject(1, UUID.randomUUID());
|
||||
ps.setObject(2, messageId);
|
||||
ps.setString(3, att.getFileUrl());
|
||||
ps.setString(4, att.getFileType());
|
||||
ps.addBatch();
|
||||
}
|
||||
|
||||
ps.executeBatch();
|
||||
return true;
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static void markGloballyDeleted(UUID chatId) {
|
||||
String sql = "UPDATE messages SET is_deleted_globally = true WHERE receiver_id = ? AND receiver_type = 'private'";
|
||||
try (Connection conn = ConnectionDb.connect(); PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setObject(1, chatId);
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void logDeletedMessagesFor(UUID chatId, UUID userId) {
|
||||
List<Message> messages = MessageDatabase.privateChatHistory(chatId);
|
||||
for (Message message : messages) {
|
||||
if (!isMessageDeleted(message.getMessage_id(), userId)) {
|
||||
String sql = """
|
||||
INSERT INTO deleted_messages (message_id, user_id)
|
||||
VALUES (?, ?)
|
||||
ON CONFLICT (message_id, user_id) DO NOTHING
|
||||
""";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setObject(1, message.getMessage_id());
|
||||
ps.setObject(2, userId);
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isMessageDeleted(UUID messageId, UUID userId) {
|
||||
String sql = "SELECT 1 FROM deleted_messages WHERE message_id = ? AND user_id = ?";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setObject(1, messageId);
|
||||
ps.setObject(2, userId);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
return rs.next();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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();
|
||||
@@ -100,7 +166,6 @@ public class MessageDatabase {
|
||||
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,
|
||||
@@ -118,6 +183,31 @@ public class MessageDatabase {
|
||||
return messages;
|
||||
}
|
||||
|
||||
public static List<FileAttachment> getAttachments(UUID messageId) {
|
||||
List<FileAttachment> attachments = new ArrayList<>();
|
||||
String sql = "SELECT file_url, file_type FROM message_attachments WHERE message_id = ?";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
|
||||
stmt.setObject(1, messageId);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
|
||||
while (rs.next()) {
|
||||
attachments.add(new FileAttachment(
|
||||
rs.getString("file_url"),
|
||||
rs.getString("file_type")
|
||||
));
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return attachments;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static LocalDateTime getLastMessageTimeBetween(UUID user1, UUID user2, String type) {
|
||||
String sql = """
|
||||
@@ -177,7 +267,6 @@ public class MessageDatabase {
|
||||
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"),
|
||||
(UUID) rs.getObject("reply_to_id"),
|
||||
@@ -217,25 +306,18 @@ public class MessageDatabase {
|
||||
return result;
|
||||
}
|
||||
|
||||
public static List<Message> privateChatHistory(UUID user1, UUID user2) {
|
||||
public static List<Message> privateChatHistory(UUID chatId) {
|
||||
List<Message> result = new ArrayList<>();
|
||||
String sql = """
|
||||
SELECT * FROM messages
|
||||
WHERE receiver_type = 'private'
|
||||
AND (
|
||||
(sender_id = ? AND receiver_id = ?)
|
||||
OR (sender_id = ? AND receiver_id = ?)
|
||||
)
|
||||
AND receiver_id = ?
|
||||
ORDER BY send_at
|
||||
""";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setObject(1, user1);
|
||||
stmt.setObject(2, user2);
|
||||
stmt.setObject(3, user2);
|
||||
stmt.setObject(4, user1);
|
||||
|
||||
stmt.setObject(1, chatId);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
while (rs.next()) {
|
||||
result.add(extractMessage(rs));
|
||||
@@ -248,6 +330,7 @@ public class MessageDatabase {
|
||||
|
||||
|
||||
|
||||
|
||||
public static List<Message> groupChatHistory(UUID groupId) {
|
||||
List<Message> result = new ArrayList<>();
|
||||
String sql = """
|
||||
@@ -360,4 +443,42 @@ public class MessageDatabase {
|
||||
}
|
||||
|
||||
|
||||
public List<Message> getMessagesForPrivateChat(UUID user1, UUID user2) {
|
||||
UUID chatId = PrivateChatDatabase.findChatIdByUsers(user1, user2);
|
||||
if (chatId == null) return new ArrayList<>();
|
||||
return findByReceiver("private", chatId);
|
||||
}
|
||||
|
||||
public static List<Message> findByReceiver(String receiverType, UUID receiverId) {
|
||||
List<Message> messages = new ArrayList<>();
|
||||
String sql = "SELECT * FROM messages WHERE receiver_type = ? AND receiver_id = ? ORDER BY send_at";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
|
||||
ps.setString(1, receiverType);
|
||||
ps.setObject(2, receiverId);
|
||||
|
||||
ResultSet rs = ps.executeQuery();
|
||||
while (rs.next()) {
|
||||
UUID messageId = (UUID) rs.getObject("message_id");
|
||||
UUID senderId = (UUID) rs.getObject("sender_id");
|
||||
UUID recId = (UUID) rs.getObject("receiver_id");
|
||||
String type = rs.getString("receiver_type");
|
||||
String content = rs.getString("content");
|
||||
String messageType = rs.getString("message_type");
|
||||
LocalDateTime sendAt = rs.getTimestamp("send_at").toLocalDateTime();
|
||||
|
||||
Message message = new Message(messageId, senderId, recId, type, content, messageType, sendAt);
|
||||
messages.add(message);
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return messages;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
package org.to.telegramfinalproject.Database;
|
||||
|
||||
import org.to.telegramfinalproject.Models.PrivateChat;
|
||||
|
||||
import java.sql.*;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
@@ -8,6 +15,24 @@ import java.util.UUID;
|
||||
|
||||
public class PrivateChatDatabase {
|
||||
|
||||
|
||||
public static List<UUID> getMembers(UUID privateChatId) {
|
||||
String sql = "SELECT user1_id, user2_id FROM private_chat WHERE chat_id = ?";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setObject(1, privateChatId);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
if (rs.next()) {
|
||||
UUID user1 = (UUID) rs.getObject("user1_id");
|
||||
UUID user2 = (UUID) rs.getObject("user2_id");
|
||||
return new ArrayList<>(Arrays.asList(user1, user2));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
public static UUID getOrCreateSavedMessagesChat(UUID userId) {
|
||||
String query = "SELECT chat_id FROM private_chat WHERE user1_id = ? AND user2_id = ?";
|
||||
try (Connection conn = ConnectionDb.connect()) {
|
||||
@@ -35,4 +60,230 @@ public class PrivateChatDatabase {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static UUID findChatIdByUsers(UUID user1, UUID user2) {
|
||||
UUID u1 = user1.compareTo(user2) < 0 ? user1 : user2;
|
||||
UUID u2 = user1.compareTo(user2) < 0 ? user2 : user1;
|
||||
|
||||
String sql = "SELECT chat_id FROM private_chat WHERE user1_id = ? AND user2_id = ?";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
|
||||
ps.setObject(1, u1);
|
||||
ps.setObject(2, u2);
|
||||
|
||||
ResultSet rs = ps.executeQuery();
|
||||
if (rs.next()) {
|
||||
return (UUID) rs.getObject("chat_id");
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public static UUID getOrCreateChat(UUID user1, UUID user2) {
|
||||
UUID u1 = user1.compareTo(user2) < 0 ? user1 : user2;
|
||||
UUID u2 = user1.compareTo(user2) < 0 ? user2 : user1;
|
||||
|
||||
String select = "SELECT chat_id FROM private_chat WHERE user1_id = ? AND user2_id = ?";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement ps = conn.prepareStatement(select)) {
|
||||
ps.setObject(1, u1);
|
||||
ps.setObject(2, u2);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
if (rs.next()) {
|
||||
return UUID.fromString(rs.getString("chat_id"));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
UUID newChatId = UUID.randomUUID();
|
||||
String insert = "INSERT INTO private_chat(chat_id, user1_id, user2_id) VALUES (?, ?, ?)";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement ps = conn.prepareStatement(insert)) {
|
||||
ps.setObject(1, newChatId);
|
||||
ps.setObject(2, u1);
|
||||
ps.setObject(3, u2);
|
||||
ps.executeUpdate();
|
||||
return newChatId;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static List<PrivateChat> findChatsOfUser(UUID userId) {
|
||||
List<PrivateChat> chats = new ArrayList<>();
|
||||
String sql = "SELECT * FROM private_chat WHERE user1_id = ? OR user2_id = ?";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
|
||||
ps.setObject(1, userId);
|
||||
ps.setObject(2, userId);
|
||||
|
||||
ResultSet rs = ps.executeQuery();
|
||||
while (rs.next()) {
|
||||
UUID chatId = (UUID) rs.getObject("chat_id");
|
||||
UUID user1 = (UUID) rs.getObject("user1_id");
|
||||
UUID user2 = (UUID) rs.getObject("user2_id");
|
||||
|
||||
chats.add(new PrivateChat(chatId, user1, user2));
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return chats;
|
||||
}
|
||||
|
||||
|
||||
public static UUID getOtherUserInChat(UUID chatId, UUID currentUserId) {
|
||||
String sql = "SELECT user1_id, user2_id FROM private_chat WHERE chat_id = ?";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setObject(1, chatId);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
if (rs.next()) {
|
||||
UUID user1 = (UUID) rs.getObject("user1_id");
|
||||
UUID user2 = (UUID) rs.getObject("user2_id");
|
||||
return currentUserId.equals(user1) ? user2 : user1;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
private static PrivateChat extractPrivateChat(ResultSet rs) throws SQLException {
|
||||
UUID chatId = UUID.fromString(rs.getString("chat_id"));
|
||||
UUID user1 = rs.getObject("user1_id", UUID.class);
|
||||
UUID user2 = rs.getObject("user2_id", UUID.class);
|
||||
boolean user1Deleted = rs.getBoolean("user1_deleted");
|
||||
boolean user2Deleted = rs.getBoolean("user2_deleted");
|
||||
LocalDateTime createdAt = rs.getTimestamp("created_at").toLocalDateTime();
|
||||
|
||||
return new PrivateChat(chatId, user1, user2, user1Deleted, user2Deleted, createdAt);
|
||||
}
|
||||
|
||||
public static void markChatDeleted(UUID userId, UUID chatId) {
|
||||
String sql = """
|
||||
UPDATE private_chat
|
||||
SET user1_deleted = CASE WHEN user1_id = ? THEN true ELSE user1_deleted END,
|
||||
user2_deleted = CASE WHEN user2_id = ? THEN true ELSE user2_deleted END
|
||||
WHERE chat_id = ?
|
||||
""";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setObject(1, userId);
|
||||
stmt.setObject(2, userId);
|
||||
stmt.setObject(3, chatId);
|
||||
stmt.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void unmarkChatDeleted(UUID userId, UUID chatId) {
|
||||
String sql = """
|
||||
UPDATE private_chat
|
||||
SET user1_deleted = CASE WHEN user1_id = ? THEN false ELSE user1_deleted END,
|
||||
user2_deleted = CASE WHEN user2_id = ? THEN false ELSE user2_deleted END
|
||||
WHERE chat_id = ?
|
||||
""";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setObject(1, userId);
|
||||
stmt.setObject(2, userId);
|
||||
stmt.setObject(3, chatId);
|
||||
stmt.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void markUser1Deleted(UUID chatId) {
|
||||
updateBoolField(chatId, "user1_deleted", true);
|
||||
}
|
||||
|
||||
public static void markUser2Deleted(UUID chatId) {
|
||||
updateBoolField(chatId, "user2_deleted", true);
|
||||
}
|
||||
|
||||
public static void markBothDeleted(UUID chatId) {
|
||||
String sql = "UPDATE private_chat SET user1_deleted = true, user2_deleted = true WHERE chat_id = ?";
|
||||
try (Connection conn = ConnectionDb.connect(); PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setObject(1, chatId);
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static void updateBoolField(UUID chatId, String field, boolean value) {
|
||||
String sql = "UPDATE private_chat SET " + field + " = ? WHERE chat_id = ?";
|
||||
try (Connection conn = ConnectionDb.connect(); PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setBoolean(1, value);
|
||||
ps.setObject(2, chatId);
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static PrivateChat findById(UUID chatId) {
|
||||
String sql = "SELECT * FROM private_chat WHERE chat_id = ?";
|
||||
try (Connection conn = ConnectionDb.connect(); PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setObject(1, chatId);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
if (rs.next()) {
|
||||
UUID user1_id = (UUID) rs.getObject("user1_id");
|
||||
UUID user2_id = (UUID) rs.getObject("user2_id");
|
||||
boolean user1_deleted = rs.getBoolean("user1_deleted");
|
||||
boolean user2_deleted = rs.getBoolean("user2_deleted");
|
||||
LocalDateTime created_at = rs.getTimestamp("created_at").toLocalDateTime();
|
||||
|
||||
return new PrivateChat(chatId, user1_id, user2_id, user1_deleted, user2_deleted, created_at);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public static void clearDeletedFlag(UUID senderId, UUID chatId) {
|
||||
String sql = """
|
||||
UPDATE private_chat
|
||||
SET user1_deleted = CASE WHEN user1_id = ? THEN FALSE ELSE user1_deleted END,
|
||||
user2_deleted = CASE WHEN user2_id = ? THEN FALSE ELSE user2_deleted END
|
||||
WHERE chat_id = ?
|
||||
""";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect(); PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setObject(1, senderId);
|
||||
ps.setObject(2, senderId);
|
||||
ps.setObject(3, chatId);
|
||||
int rows = ps.executeUpdate();
|
||||
System.out.println("✅ clearDeletedFlag updated rows = " + rows);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -313,6 +313,8 @@ public class userDatabase {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public List<User> searchUsers(String keyword, UUID currentUserId) {
|
||||
String query = """
|
||||
SELECT * FROM users
|
||||
|
||||
Reference in New Issue
Block a user