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