Work on telegram setting
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package org.to.telegramfinalproject.Database;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.to.telegramfinalproject.Models.Contact;
|
||||
import org.to.telegramfinalproject.Models.ContactEntry;
|
||||
import org.to.telegramfinalproject.Models.User;
|
||||
@@ -99,6 +100,32 @@ public class ContactDatabase {
|
||||
}
|
||||
|
||||
|
||||
public static List<JSONObject> getBlockedUsers(UUID userId) {
|
||||
String sql = """
|
||||
SELECT u.internal_uuid, u.user_id, u.profile_name
|
||||
FROM contacts c
|
||||
JOIN users u ON u.internal_uuid = c.contact_id
|
||||
WHERE c.user_id = ? AND c.is_blocked = TRUE
|
||||
ORDER BY u.profile_name NULLS LAST, u.user_id
|
||||
""";
|
||||
List<JSONObject> out = new ArrayList<>();
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setObject(1, userId);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
JSONObject j = new JSONObject();
|
||||
j.put("internal_uuid", rs.getObject("internal_uuid").toString());
|
||||
j.put("user_id", rs.getString("user_id"));
|
||||
j.put("profile_name", rs.getString("profile_name"));
|
||||
out.add(j);
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
public boolean unblockContact(UUID user_id, UUID contact_id) {
|
||||
String sql = "UPDATE contacts SET is_blocked = FALSE WHERE user_id = ? AND contact_id = ?";
|
||||
|
||||
@@ -409,5 +409,42 @@ public class userDatabase {
|
||||
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
public static String getPasswordHash(UUID userId) {
|
||||
String sql = "SELECT password FROM users WHERE internal_uuid = ?";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setObject(1, userId);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) return rs.getString("password");
|
||||
}
|
||||
} catch (SQLException e) { e.printStackTrace(); }
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean updateUsername(UUID userId, String newUsername) throws SQLException {
|
||||
String sql = "UPDATE users SET username = ? WHERE internal_uuid = ?";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setString(1, newUsername);
|
||||
ps.setObject(2, userId);
|
||||
ps.executeUpdate();
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
// 23505 = unique_violation در PostgreSQL
|
||||
if ("23505".equals(e.getSQLState())) throw e;
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean updatePasswordHash(UUID userId, String newHash) {
|
||||
String sql = "UPDATE users SET password = ? WHERE internal_uuid = ?";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setString(1, newHash);
|
||||
ps.setObject(2, userId);
|
||||
return ps.executeUpdate() > 0;
|
||||
} catch (SQLException e) { e.printStackTrace(); return false; }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user