Work on add contact to contact list
This commit is contained in:
@@ -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 {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user