group and channel name can't be null
This commit is contained in:
@@ -261,8 +261,16 @@ public class ActionHandler {
|
||||
public void createGroup() {
|
||||
System.out.print("Enter group ID: ");
|
||||
String groupId = scanner.nextLine();
|
||||
System.out.print("Enter group name: ");
|
||||
String groupName = scanner.nextLine();
|
||||
String groupName = null;
|
||||
while (groupName == null) {
|
||||
System.out.print("Enter group name: ");
|
||||
String input = scanner.nextLine();
|
||||
if (!input.trim().isEmpty()) {
|
||||
groupName = input;
|
||||
} else {
|
||||
System.out.println("Group name can't be empty.");
|
||||
}
|
||||
}
|
||||
System.out.print("Enter image URL (optional): ");
|
||||
String imageUrl = scanner.nextLine();
|
||||
|
||||
@@ -281,8 +289,16 @@ public class ActionHandler {
|
||||
public void createChannel() {
|
||||
System.out.print("Enter channel ID: ");
|
||||
String channelId = scanner.nextLine();
|
||||
System.out.print("Enter channel name: ");
|
||||
String channelName = scanner.nextLine();
|
||||
String channelName = null;
|
||||
while (channelName == null) {
|
||||
System.out.print("Enter channel name: ");
|
||||
String input = scanner.nextLine();
|
||||
if (!input.trim().isEmpty()) {
|
||||
channelName = input;
|
||||
} else {
|
||||
System.out.println("Channel name can't be empty.");
|
||||
}
|
||||
}
|
||||
System.out.print("Enter image URL (optional): ");
|
||||
String imageUrl = scanner.nextLine();
|
||||
|
||||
|
||||
@@ -576,7 +576,7 @@ public class ChannelDatabase {
|
||||
|
||||
|
||||
public static boolean transferOwnership(UUID channelId, UUID newOwnerUUID) {
|
||||
String sql = """
|
||||
String updateRoles = """
|
||||
UPDATE channel_subscribers
|
||||
SET role = CASE
|
||||
WHEN user_id = ? THEN 'owner'
|
||||
@@ -586,21 +586,41 @@ public class ChannelDatabase {
|
||||
WHERE channel_id = ?
|
||||
""";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
String clearPermissions = """
|
||||
UPDATE channel_subscribers
|
||||
SET permissions = '{}'::jsonb
|
||||
WHERE channel_id = ? AND user_id = ?
|
||||
""";
|
||||
|
||||
stmt.setObject(1, newOwnerUUID);
|
||||
stmt.setObject(2, channelId);
|
||||
try (Connection conn = ConnectionDb.connect()) {
|
||||
conn.setAutoCommit(false);
|
||||
|
||||
stmt.executeUpdate();
|
||||
return true;
|
||||
try (PreparedStatement roleStmt = conn.prepareStatement(updateRoles);
|
||||
PreparedStatement clearPermsStmt = conn.prepareStatement(clearPermissions)) {
|
||||
|
||||
roleStmt.setObject(1, newOwnerUUID);
|
||||
roleStmt.setObject(2, channelId);
|
||||
roleStmt.executeUpdate();
|
||||
|
||||
clearPermsStmt.setObject(1, channelId);
|
||||
clearPermsStmt.setObject(2, newOwnerUUID);
|
||||
clearPermsStmt.executeUpdate();
|
||||
|
||||
conn.commit();
|
||||
return true;
|
||||
|
||||
} catch (SQLException e) {
|
||||
conn.rollback();
|
||||
e.printStackTrace();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public static List<UUID> getChannelSubscriberUUIDs(UUID channelId) {
|
||||
List<UUID> subscriberIds = new ArrayList<>();
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
|
||||
@@ -7,7 +7,7 @@ import java.sql.SQLException;
|
||||
public class ConnectionDb {
|
||||
private static final String JDBC_URL = "jdbc:postgresql://localhost:5432/Telegram";
|
||||
private static final String USERNAME = "postgres";
|
||||
private static final String PASSWORD = "124postpass";
|
||||
private static final String PASSWORD = "Partow@1384";
|
||||
|
||||
public ConnectionDb() {
|
||||
}
|
||||
|
||||
@@ -559,7 +559,7 @@ public class GroupDatabase {
|
||||
|
||||
public static boolean transferOwnership(UUID groupId, UUID newOwnerId) {
|
||||
String demoteOldOwner = "UPDATE group_members SET role = 'admin' WHERE group_id = ? AND role = 'owner'";
|
||||
String promoteNewOwner = "UPDATE group_members SET role = 'owner' WHERE group_id = ? AND user_id = ?";
|
||||
String promoteNewOwner = "UPDATE group_members SET role = 'owner', permissions = '{}'::jsonb WHERE group_id = ? AND user_id = ?";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect()) {
|
||||
conn.setAutoCommit(false);
|
||||
|
||||
Reference in New Issue
Block a user