added remove contact.
This commit is contained in:
@@ -1,9 +1,11 @@
|
|||||||
package org.to.telegramfinalproject.Database;
|
package org.to.telegramfinalproject.Database;
|
||||||
|
|
||||||
import org.to.telegramfinalproject.Models.Contact;
|
import org.to.telegramfinalproject.Models.Contact;
|
||||||
|
import org.to.telegramfinalproject.Models.ContactEntry;
|
||||||
import org.to.telegramfinalproject.Models.User;
|
import org.to.telegramfinalproject.Models.User;
|
||||||
|
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
@@ -45,7 +47,7 @@ public class ContactDatabase {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
public boolean removeContact(UUID user_id, UUID contact_id) {
|
public static boolean removeContact(UUID user_id, UUID contact_id) {
|
||||||
String sql = "DELETE FROM contacts WHERE user_id = ? AND contact_id = ?";
|
String sql = "DELETE FROM contacts WHERE user_id = ? AND contact_id = ?";
|
||||||
try (Connection connection = getConnection()) {
|
try (Connection connection = getConnection()) {
|
||||||
PreparedStatement stmt = connection.prepareStatement(sql);
|
PreparedStatement stmt = connection.prepareStatement(sql);
|
||||||
@@ -167,35 +169,6 @@ public class ContactDatabase {
|
|||||||
return false;
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static boolean deleteChatOneSide(UUID currentUserId, UUID otherUserId) {
|
public static boolean deleteChatOneSide(UUID currentUserId, UUID otherUserId) {
|
||||||
String sql = """
|
String sql = """
|
||||||
@@ -268,5 +241,50 @@ public class ContactDatabase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static List<ContactEntry> searchContacts(UUID userId, String searchTerm) {
|
||||||
|
List<ContactEntry> results = new ArrayList<>();
|
||||||
|
|
||||||
|
String sql = """
|
||||||
|
SELECT u.internal_uuid, u.user_id, 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, userId);
|
||||||
|
stmt.setString(2, "%" + searchTerm + "%");
|
||||||
|
stmt.setString(3, "%" + searchTerm + "%");
|
||||||
|
|
||||||
|
ResultSet rs = stmt.executeQuery();
|
||||||
|
|
||||||
|
userDatabase userDB = new userDatabase();
|
||||||
|
while (rs.next()) {
|
||||||
|
UUID contactId = (UUID) rs.getObject("internal_uuid");
|
||||||
|
String displayId = rs.getString("user_id");
|
||||||
|
String contact_displayId = userDB.getUserId(contactId);
|
||||||
|
String profileName = userDB.getProfileName(contactId);
|
||||||
|
String imageUrl = userDB.getProfilePicture(contactId);
|
||||||
|
boolean isBlocked = rs.getBoolean("is_blocked");
|
||||||
|
|
||||||
|
String lastSeenString = userDB.getLastSeen(contactId);
|
||||||
|
|
||||||
|
// Convert to LocalDateTime
|
||||||
|
LocalDateTime lastSeen = null;
|
||||||
|
if (!"Unknown".equals(lastSeenString)) {
|
||||||
|
lastSeen = LocalDateTime.parse(lastSeenString);
|
||||||
|
}
|
||||||
|
|
||||||
|
results.add(new ContactEntry(contactId, displayId, contact_displayId, profileName, imageUrl, isBlocked, lastSeen));
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -277,7 +277,7 @@ public class MessageDatabase {
|
|||||||
(UUID) rs.getObject("forwarded_by"),
|
(UUID) rs.getObject("forwarded_by"),
|
||||||
(UUID) rs.getObject("forwarded_from"),
|
(UUID) rs.getObject("forwarded_from"),
|
||||||
rs.getBoolean("is_deleted_globally"),
|
rs.getBoolean("is_deleted_globally"),
|
||||||
rs.getTimestamp("edited_at").toLocalDateTime()
|
(rs.getTimestamp("edited_at") != null) ? rs.getTimestamp("edited_at").toLocalDateTime() : null
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -353,6 +353,61 @@ public class userDatabase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String getProfileName(UUID userId) {
|
||||||
|
String sql = "SELECT profile_name FROM users WHERE internal_uuid = ?";
|
||||||
|
|
||||||
|
try (Connection conn = getConnection();
|
||||||
|
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||||
|
stmt.setObject(1, userId);
|
||||||
|
ResultSet rs = stmt.executeQuery();
|
||||||
|
|
||||||
|
if (rs.next()) {
|
||||||
|
String profileName = rs.getString("profile_name");
|
||||||
|
return profileName;
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return "Unknown";
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getProfilePicture(UUID userId) {
|
||||||
|
String sql = "SELECT image_url FROM users WHERE internal_uuid = ?";
|
||||||
|
|
||||||
|
try (Connection conn = getConnection();
|
||||||
|
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||||
|
stmt.setObject(1, userId);
|
||||||
|
ResultSet rs = stmt.executeQuery();
|
||||||
|
|
||||||
|
if (rs.next()) {
|
||||||
|
String imageUrl = rs.getString("image_url");
|
||||||
|
return imageUrl;
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return "Unknown";
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getUserId(UUID userId) {
|
||||||
|
String sql = "SELECT user_id FROM users WHERE internal_uuid = ?";
|
||||||
|
|
||||||
|
try (Connection conn = getConnection();
|
||||||
|
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||||
|
stmt.setObject(1, userId);
|
||||||
|
ResultSet rs = stmt.executeQuery();
|
||||||
|
|
||||||
|
if (rs.next()) {
|
||||||
|
String user_id = rs.getString("user_id");
|
||||||
|
return user_id;
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return "Unknown";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user