Implemented the deletion function

Closes #17
This commit is contained in:
Thorsten Sommer 2022-07-09 21:24:09 +02:00
parent e8711de0d6
commit 271ae2a1cf
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
3 changed files with 63 additions and 2 deletions

View File

@ -82,4 +82,30 @@ public static class SectionProcessor
await db.SaveChangesAsync(); await db.SaveChangesAsync();
return section; return section;
} }
public static async Task RemoveSection(DataContext db, string selectedKey)
{
// Remove the section from the database:
var section2Delete = await db.Sections.FirstOrDefaultAsync(n => n.DataKey == selectedKey);
if (section2Delete is null)
throw new ArgumentException($"The section with key {selectedKey} does not exist in the database.");
// Next, remove all children of the section, and the children's children, etc.:
var children = await db.Sections.Where(n => n.Parent == section2Delete).ToListAsync();
foreach (var child in children)
await RemoveSection(db, child.DataKey);
db.Sections.Remove(section2Delete);
await db.SaveChangesAsync();
}
public static async Task<int> NumberChildren(DataContext db, string selectedKey)
{
// Read the section from the database:
var section = await db.Sections.FirstOrDefaultAsync(n => n.DataKey == selectedKey);
if (section is null)
throw new ArgumentException($"The section with key {selectedKey} does not exist in the database.");
return await db.Sections.CountAsync(n => n.Parent == section);
}
} }

View File

@ -91,6 +91,7 @@
this.buttonRemove.Text = "Remove"; this.buttonRemove.Text = "Remove";
this.buttonRemove.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText; this.buttonRemove.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText;
this.buttonRemove.UseVisualStyleBackColor = true; this.buttonRemove.UseVisualStyleBackColor = true;
this.buttonRemove.Click += new System.EventHandler(this.buttonRemove_Click);
// //
// treeView // treeView
// //
@ -102,6 +103,7 @@
this.treeView.Name = "treeView"; this.treeView.Name = "treeView";
this.treeView.Size = new System.Drawing.Size(290, 439); this.treeView.Size = new System.Drawing.Size(290, 439);
this.treeView.TabIndex = 1; this.treeView.TabIndex = 1;
this.treeView.NodeMouseClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(this.treeView_NodeMouseClick);
// //
// SectionTree // SectionTree
// //

View File

@ -31,10 +31,10 @@ public partial class SectionTree : UserControl
this.treeView.ImageList = imgList; this.treeView.ImageList = imgList;
// Subscribe to the load event: // Subscribe to the load event:
this.Load += this.OnLoad; this.Load += this.LoadNodes;
} }
private async void OnLoad(object? sender, EventArgs e) private async void LoadNodes(object? sender, EventArgs e)
{ {
// A dictionary to cache all known tree nodes: // A dictionary to cache all known tree nodes:
var treeNodes = new Dictionary<string, TreeNode>(); var treeNodes = new Dictionary<string, TreeNode>();
@ -146,4 +146,37 @@ public partial class SectionTree : UserControl
node.EnsureVisible(); node.EnsureVisible();
this.treeView.SelectedNode = node; this.treeView.SelectedNode = node;
} }
private async void buttonRemove_Click(object sender, EventArgs e)
{
// Get the currently selected section, which will be removed:
var selectedNode = this.treeView.SelectedNode;
// Get the number of children:
// (notice, that the node's name is its key)
var numberChildren = await SectionProcessor.NumberChildren(this.db, selectedNode.Name);
// Ask the user, if he really wants to remove the section:
if(MessageBox.Show(numberChildren > 0 ? $"Are you sure, you want to remove the section '{selectedNode.Text}', its {numberChildren} children and so on?" : $"Are you sure, you want to remove the section '{selectedNode.Text}'?", "Remove section", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
return;
// Remove the section from the database:
// (notice, that the node's name is its key)
await SectionProcessor.RemoveSection(this.db, selectedNode.Name);
// Remove all nodes from the tree control:
this.treeView.Nodes.Clear();
// Reload the tree:
this.LoadNodes(this, EventArgs.Empty);
}
private void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
// Get the currently selected section:
var selectedNode = this.treeView.SelectedNode;
// If the selected node is not null, enable the remove button:
this.buttonRemove.Enabled = selectedNode is not null;
}
} }