Added support for updating category properties.

This commit is contained in:
2021-04-13 18:18:44 -05:00
parent 6838635c78
commit f014f22ecf
3 changed files with 368 additions and 8 deletions
+150 -7
View File
@@ -13,9 +13,12 @@ namespace InventoryTest
{
public partial class CategoryManager : Form
{
private static string ConnectionString { get; set; }
private static Dictionary<string, int> CategoryIds { get; } = new();
private static Dictionary<string, int> SubCategoryIds { get; } = new();
private string ConnectionString { get; set; }
private Dictionary<string, int> CategoryIds { get; } = new();
private Dictionary<string, int> SubCategoryIds { get; } = new();
private Dictionary<string, Property> CategoryPropertyIds { get; } = new();
private Dictionary<string, Property> SubCategoryPropertyIds { get; } = new();
private Dictionary<string, int> PropertyTypeIds { get; } = new();
public CategoryManager(string connectionString)
{
@@ -30,18 +33,62 @@ namespace InventoryTest
NewCategoryNameTextBox.TextChanged += NewCategoryNameTextBox_TextChanged;
CategoryPropertiesListBox.SelectedIndexChanged += CategoryPropertiesListBox_SelectedIndexChanged;
SubCategoryPropertiesListBox.SelectedIndexChanged += SubCategoryPropertiesListBox_SelectedIndexChanged;
CategoryPropertyInfoNameTextBox.TextChanged += CategoryPropertyInfoNameTextBox_TextChanged;
SubCategoryPropertyInfoNameTextBox.TextChanged += SubCategoryPropertyInfoNameTextBox_TextChanged;
LoadPropertyTypes();
LoadCategories();
}
private void SubCategoryPropertyInfoNameTextBox_TextChanged(object sender, EventArgs e)
{
SubCategoryPropertyInfoUpdateButton.Enabled = SubCategoryPropertyInfoNameTextBox.TextLength > 0 && (SubCategoryPropertiesListBox.SelectedIndex != -1 && SubCategoryPropertiesListBox.SelectedItem.ToString() != SubCategoryPropertyInfoNameTextBox.Text.Trim());
}
private void CategoryPropertyInfoNameTextBox_TextChanged(object sender, EventArgs e)
{
CategoryPropertyInfoUpdateButton.Enabled = CategoryPropertyInfoNameTextBox.TextLength > 0 && (CategoryPropertiesListBox.SelectedIndex != -1 && CategoryPropertiesListBox.SelectedItem.ToString() != CategoryPropertyInfoNameTextBox.Text.Trim()); ;
}
private void LoadPropertyTypes()
{
using var connection = new SqlConnection(ConnectionString);
using var command = new SqlCommand("SELECT * FROM dbo.GetPropertyTypes()", connection);
connection.Open();
var reader = command.ExecuteReader();
while (reader.Read())
{
CategoryPropertyTypeComboBox.Items.Add(reader["Property Type"]);
CategoryPropertyInfoTypeComboBox.Items.Add(reader["Property type"]);
SubCategoryPropertyTypeComboBox.Items.Add(reader["Property Type"]);
SubCategoryTypeInfoComboBox.Items.Add(reader["Property Type"]);
PropertyTypeIds.Add(reader["Property Type"].ToString(), Convert.ToInt32(reader["Property Type ID"]));
}
CategoryPropertyTypeComboBox.SelectedIndex = CategoryPropertyInfoTypeComboBox.SelectedIndex = SubCategoryPropertyTypeComboBox.SelectedIndex = SubCategoryTypeInfoComboBox.SelectedIndex = 3;
}
private void SubCategoryPropertiesListBox_SelectedIndexChanged(object sender, EventArgs e)
{
DeleteSubCategoryPropertyButton.Enabled = SubCategoryPropertiesListBox.SelectedIndex >= 0;
SubCategoryPropertyInfoNameTextBox.Enabled = SubCategoryPropertiesListBox.SelectedIndex >= 0;
SubCategoryTypeInfoComboBox.Enabled = SubCategoryPropertiesListBox.SelectedIndex >= 0;
if (SubCategoryPropertyInfoNameTextBox.Enabled) SubCategoryPropertyInfoNameTextBox.Text = SubCategoryPropertiesListBox.SelectedItem.ToString();
else SubCategoryPropertyInfoNameTextBox.Text = string.Empty;
}
private void CategoryPropertiesListBox_SelectedIndexChanged(object sender, EventArgs e)
{
DeleteCategoryPropertyButton.Enabled = CategoryPropertiesListBox.SelectedIndex >= 0;
CategoryPropertyInfoNameTextBox.Enabled = CategoryPropertiesListBox.SelectedIndex >= 0;
CategoryPropertyInfoTypeComboBox.Enabled = CategoryPropertiesListBox.SelectedIndex >= 0;
if (CategoryPropertyInfoNameTextBox.Enabled) CategoryPropertyInfoNameTextBox.Text = CategoryPropertiesListBox.SelectedItem.ToString();
else CategoryPropertyInfoNameTextBox.Text = string.Empty;
}
private void NewCategoryNameTextBox_TextChanged(object sender, EventArgs e)
@@ -57,7 +104,7 @@ namespace InventoryTest
private void CategoryPropertyNameTextBox_TextChanged(object sender, EventArgs e)
{
CreateCategoryPropertyButton.Enabled = !string.IsNullOrEmpty(CategoryPropertyNameTextBox.Text);
CreateCategoryPropertyButton.Enabled = CategoryComboBoxManager.Items.Count > 0 && !string.IsNullOrEmpty(CategoryPropertyNameTextBox.Text);
}
private void SubCategoryComboBoxManager_SelectedIndexChanged(object sender, EventArgs e)
@@ -74,6 +121,7 @@ namespace InventoryTest
DeleteSubCategoryPropertyButton.Enabled = false;
var id = CategoryIds[CategoryComboBoxManager.Text];
SubCategoryIds.Clear();
SubCategoryPropertyIds.Clear();
using var connection = new SqlConnection(ConnectionString);
using var command = new SqlCommand("SELECT * FROM dbo.GetCategorySubCategories(@ID)", connection);
@@ -138,6 +186,7 @@ namespace InventoryTest
private void LoadCategoryProperties()
{
CategoryPropertiesListBox.Items.Clear();
CategoryPropertyIds.Clear();
using var connection = new SqlConnection(ConnectionString);
using var command = new SqlCommand("SELECT * FROM GetCategoryProperties(@CategoryID)", connection);
@@ -148,12 +197,23 @@ namespace InventoryTest
var reader = command.ExecuteReader();
while (reader.Read())
{
CategoryPropertyIds.Add(reader["Name"].ToString(), new Property
{
PropertyName = reader["Name"].ToString(),
PropertyId = Convert.ToInt32(reader["Property ID"]),
CategoryPropertyId = Convert.ToInt32(reader["Category Property ID"]),
PropertyType = reader["Property Type"].ToString(),
PropertyTypeId = Convert.ToInt32(reader["Property Type ID"])
});
CategoryPropertiesListBox.Items.Add(reader["Name"]);
}
}
private void LoadSubCategoryProperties()
{
SubCategoryPropertiesListBox.Items.Clear();
SubCategoryPropertyIds.Clear();
DeleteSubCategoryPropertyButton.Enabled = false;
using var connection = new SqlConnection(ConnectionString);
using var command = new SqlCommand("SELECT * FROM GetSubCategoryProperties(@ID)", connection);
@@ -164,25 +224,38 @@ namespace InventoryTest
var reader = command.ExecuteReader();
while (reader.Read())
{
SubCategoryPropertyIds.Add(reader["Name"].ToString(), new Property
{
PropertyName = reader["Name"].ToString(),
PropertyId = Convert.ToInt32(reader["Property ID"]),
CategoryPropertyId = Convert.ToInt32(reader["Sub Category Property ID"]),
PropertyType = reader["Property Type"].ToString(),
PropertyTypeId = Convert.ToInt32(reader["Property Type ID"])
});
SubCategoryPropertiesListBox.Items.Add(reader["Name"]);
}
}
private void ToggleSubCategoryProperty(bool enabled)
{
SubCategoryPropertyNameTextBox.Enabled = enabled;
SubCategoryPropertyTypeComboBox.Enabled = enabled;
DeleteSubCategoryButton.Enabled = enabled;
}
private void ToggleCategoryProperty(bool enabled)
{
CategoryPropertyNameTextBox.Enabled = enabled;
CategoryPropertyTypeComboBox.Enabled = enabled;
}
private void CreateCategoryPropertyButton_Click(object sender, EventArgs e)
{
using var connection = new SqlConnection(ConnectionString);
using var command = new SqlCommand("CreateCategoryPropertyByName", connection) { CommandType = CommandType.StoredProcedure };
using var command = new SqlCommand("CreateCategoryProperty", connection) { CommandType = CommandType.StoredProcedure };
command.Parameters.AddWithValue("PropertyName", CategoryPropertyNameTextBox.Text);
command.Parameters.AddWithValue("PropertyTypeID", PropertyTypeIds[CategoryPropertyTypeComboBox.Text]);
command.Parameters.AddWithValue("CategoryID", CategoryIds[CategoryComboBoxManager.Text]);
connection.Open();
@@ -195,11 +268,12 @@ namespace InventoryTest
CategoryPropertyNameTextBox.Focus();
}
private void CreateSubCategoryButton_Click(object sender, EventArgs e)
private void CreateSubCategoryPropertyButton_Click(object sender, EventArgs e)
{
using var connection = new SqlConnection(ConnectionString);
using var command = new SqlCommand("CreateSubCategoryPropertyByName", connection) { CommandType = CommandType.StoredProcedure };
using var command = new SqlCommand("CreateSubCategoryProperty", connection) { CommandType = CommandType.StoredProcedure };
command.Parameters.AddWithValue("PropertyName", SubCategoryPropertyNameTextBox.Text);
command.Parameters.AddWithValue("PropertyTypeID", PropertyTypeIds[SubCategoryPropertyTypeComboBox.Text]);
command.Parameters.AddWithValue("SubCategoryID", SubCategoryIds[SubCategoryComboBoxManager.Text]);
connection.Open();
@@ -216,6 +290,7 @@ namespace InventoryTest
{
using var connection = new SqlConnection(ConnectionString);
using var command = new SqlCommand("CreateCategory", connection) { CommandType = CommandType.StoredProcedure };
command.Parameters.AddWithValue("PropertyTypeID", PropertyTypeIds[CategoryPropertyTypeComboBox.Text]);
command.Parameters.AddWithValue("Category", NewCategoryNameTextBox.Text);
connection.Open();
@@ -283,7 +358,75 @@ namespace InventoryTest
private void DeleteCategoryPropertyButton_Click(object sender, EventArgs e)
{
using var connection = new SqlConnection(ConnectionString);
using var command = new SqlCommand("DeleteCategoryProperty", connection) { CommandType = CommandType.StoredProcedure };
command.Parameters.AddWithValue("CategoryPropertyID", CategoryPropertyIds[CategoryPropertiesListBox.SelectedItem.ToString()].CategoryPropertyId);
connection.Open();
command.ExecuteNonQuery();
CategoryPropertyIds.Remove(CategoryPropertiesListBox.SelectedItem.ToString());
CategoryPropertiesListBox.Items.RemoveAt(CategoryPropertiesListBox.SelectedIndex);
}
private void DeleteSubCategoryPropertyButton_Click(object sender, EventArgs e)
{
using var connection = new SqlConnection(ConnectionString);
using var command = new SqlCommand("DeleteSubCategoryProperty", connection) { CommandType = CommandType.StoredProcedure };
command.Parameters.AddWithValue("SubCategoryPropertyID", SubCategoryPropertyIds[SubCategoryPropertiesListBox.SelectedItem.ToString()].CategoryPropertyId);
connection.Open();
command.ExecuteNonQuery();
SubCategoryPropertyIds.Remove(SubCategoryPropertiesListBox.SelectedItem.ToString());
SubCategoryPropertiesListBox.Items.RemoveAt(SubCategoryPropertiesListBox.SelectedIndex);
}
private struct Property
{
public string PropertyName;
public int PropertyId;
public int CategoryPropertyId;
public int PropertyTypeId;
public string PropertyType;
}
private void CategoryPropertyInfoUpdateButton_Click(object sender, EventArgs e)
{
using var connection = new SqlConnection(ConnectionString);
using var command = new SqlCommand("UpdateProperty", connection) { CommandType = CommandType.StoredProcedure };
command.Parameters.AddWithValue("PropertyID", CategoryPropertyIds[CategoryPropertiesListBox.SelectedItem.ToString()].PropertyId);
command.Parameters.AddWithValue("PropertyName", CategoryPropertyInfoNameTextBox.Text);
command.Parameters.AddWithValue("PropertyTypeID", PropertyTypeIds[CategoryPropertyInfoTypeComboBox.Text]);
connection.Open();
command.ExecuteNonQuery();
LoadCategoryProperties();
CategoryPropertyInfoNameTextBox.Text = string.Empty;
}
private void SubCategoryPropertyInfoUpdateButton_Click(object sender, EventArgs e)
{
using var connection = new SqlConnection(ConnectionString);
using var command = new SqlCommand("UpdateProperty", connection) { CommandType = CommandType.StoredProcedure };
command.Parameters.AddWithValue("PropertyID", SubCategoryPropertyIds[SubCategoryPropertiesListBox.SelectedItem.ToString()].PropertyId);
command.Parameters.AddWithValue("PropertyName", SubCategoryPropertyInfoNameTextBox.Text);
command.Parameters.AddWithValue("PropertyTypeID", PropertyTypeIds[SubCategoryTypeInfoComboBox.Text]);
connection.Open();
command.ExecuteNonQuery();
LoadSubCategoryProperties();
SubCategoryPropertyInfoNameTextBox.Text = string.Empty;
}
}
}