moving add_table to add_logic modern architecture3
This commit is contained in:
@@ -98,23 +98,48 @@ impl AddTableState {
|
||||
|
||||
/// Helper method to add a column from current inputs
|
||||
pub fn add_column_from_inputs(&mut self) -> Option<String> {
|
||||
if self.column_name_input.trim().is_empty() || self.column_type_input.trim().is_empty() {
|
||||
return Some("Both column name and type are required".to_string());
|
||||
let table_name_in = self.table_name_input.trim().to_string();
|
||||
let column_name_in = self.column_name_input.trim().to_string();
|
||||
let column_type_in = self.column_type_input.trim().to_string();
|
||||
|
||||
// Case: "only table name" provided → set it and stay on TableName
|
||||
if !table_name_in.is_empty() && column_name_in.is_empty() && column_type_in.is_empty() {
|
||||
self.table_name = table_name_in;
|
||||
self.table_name_input.clear();
|
||||
self.table_name_cursor_pos = 0;
|
||||
self.current_focus = AddTableFocus::InputTableName;
|
||||
self.has_unsaved_changes = true;
|
||||
return Some(format!("Table name set to '{}'.", self.table_name));
|
||||
}
|
||||
|
||||
// Check for duplicate column names
|
||||
if self.columns.iter().any(|col| col.name == self.column_name_input.trim()) {
|
||||
// Column validation
|
||||
if column_name_in.is_empty() || column_type_in.is_empty() {
|
||||
return Some("Both column name and type are required".to_string());
|
||||
}
|
||||
if self.columns.iter().any(|col| col.name == column_name_in) {
|
||||
return Some("Column name already exists".to_string());
|
||||
}
|
||||
|
||||
// If table_name input present while adding first column, apply it too
|
||||
if !table_name_in.is_empty() {
|
||||
self.table_name = table_name_in;
|
||||
self.table_name_input.clear();
|
||||
self.table_name_cursor_pos = 0;
|
||||
}
|
||||
|
||||
// Add the column
|
||||
self.columns.push(ColumnDefinition {
|
||||
name: self.column_name_input.trim().to_string(),
|
||||
data_type: self.column_type_input.trim().to_string(),
|
||||
name: column_name_in.clone(),
|
||||
data_type: column_type_in.clone(),
|
||||
selected: false,
|
||||
});
|
||||
// Add a corresponding (unselected) index with the same name
|
||||
self.indexes.push(IndexDefinition {
|
||||
name: column_name_in.clone(),
|
||||
selected: false,
|
||||
});
|
||||
|
||||
// Clear inputs and reset focus to column name for next entry
|
||||
// Clear column inputs and set focus for next entry
|
||||
self.column_name_input.clear();
|
||||
self.column_type_input.clear();
|
||||
self.column_name_cursor_pos = 0;
|
||||
@@ -123,23 +148,33 @@ impl AddTableState {
|
||||
self.last_canvas_field = 1;
|
||||
self.has_unsaved_changes = true;
|
||||
|
||||
Some(format!("Column '{}' added successfully", self.columns.last().unwrap().name))
|
||||
Some(format!("Column '{}' added successfully", column_name_in))
|
||||
}
|
||||
|
||||
/// Helper method to delete selected items
|
||||
pub fn delete_selected_items(&mut self) -> Option<String> {
|
||||
let mut deleted_items = Vec::new();
|
||||
let mut deleted_items: Vec<String> = Vec::new();
|
||||
|
||||
// Remove selected columns
|
||||
let initial_column_count = self.columns.len();
|
||||
self.columns.retain(|col| {
|
||||
if col.selected {
|
||||
deleted_items.push(format!("column '{}'", col.name));
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
});
|
||||
let selected_col_names: std::collections::HashSet<String> = self
|
||||
.columns
|
||||
.iter()
|
||||
.filter(|c| c.selected)
|
||||
.map(|c| c.name.clone())
|
||||
.collect();
|
||||
if !selected_col_names.is_empty() {
|
||||
self.columns.retain(|col| {
|
||||
if selected_col_names.contains(&col.name) {
|
||||
deleted_items.push(format!("column '{}'", col.name));
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
});
|
||||
// Also purge indexes for deleted columns
|
||||
self.indexes
|
||||
.retain(|idx| !selected_col_names.contains(&idx.name));
|
||||
}
|
||||
|
||||
// Remove selected indexes
|
||||
let initial_index_count = self.indexes.len();
|
||||
@@ -167,6 +202,8 @@ impl AddTableState {
|
||||
Some("No items selected for deletion".to_string())
|
||||
} else {
|
||||
self.has_unsaved_changes = true;
|
||||
self.column_table_state.select(None);
|
||||
self.index_table_state.select(None);
|
||||
Some(format!("Deleted: {}", deleted_items.join(", ")))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user