From bbefeb97a848d75b552572fb51f8c3dc06d9c7c2 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sun, 17 Jul 2022 13:26:25 +0200 Subject: [PATCH] Added some error handling to the section tree --- I18N Commander/Processor/SectionProcessor.cs | 44 ++++++++++++++----- .../UI WinForms/Components/SectionTree.cs | 16 +++++-- 2 files changed, 45 insertions(+), 15 deletions(-) diff --git a/I18N Commander/Processor/SectionProcessor.cs b/I18N Commander/Processor/SectionProcessor.cs index 2242d94..a1a7844 100644 --- a/I18N Commander/Processor/SectionProcessor.cs +++ b/I18N Commander/Processor/SectionProcessor.cs @@ -30,7 +30,7 @@ public static class SectionProcessor /// /// Compute the new sections key and its depth, then store the section in the database. /// - public static async Task
AddSection(DataContext db, string text, string? parentKey) + public static async Task> AddSection(DataContext db, string text, string? parentKey) { // Remove any whitespaces from the section name, regardless of how many e.g. spaces the user typed: var key = string.Join('_', text.Split(' ', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)).ToUpperInvariant(); @@ -58,9 +58,16 @@ public static class SectionProcessor TextElements = new(), }; - db.Sections.Add(rootSection); - await db.SaveChangesAsync(); - return rootSection; + try + { + db.Sections.Add(rootSection); + await db.SaveChangesAsync(); + return new ProcessorResult
(rootSection); + } + catch (Exception e) + { + e.ToProcessorResult
(); + } } // Read the parent from the database: @@ -77,10 +84,17 @@ public static class SectionProcessor TextElements = new(), Depth = parent.Depth + 1, }; - - await db.Sections.AddAsync(section); - await db.SaveChangesAsync(); - return section; + + try + { + await db.Sections.AddAsync(section); + await db.SaveChangesAsync(); + return new ProcessorResult
(section); + } + catch (Exception e) + { + return e.ToProcessorResult
(); + } } public static async Task RemoveSection(DataContext db, string selectedKey) @@ -109,7 +123,7 @@ public static class SectionProcessor return await db.Sections.CountAsync(n => n.Parent == section); } - public static async Task
RenameSection(DataContext db, string selectedNodeKey, string alteredName) + public static async Task> RenameSection(DataContext db, string selectedNodeKey, string alteredName) { // Read the section from the database: var section = await db.Sections.FirstOrDefaultAsync(n => n.DataKey == selectedNodeKey); @@ -132,8 +146,16 @@ public static class SectionProcessor section.Name = alteredName; section.DataKey = newKey; - await db.SaveChangesAsync(); - return section; + + try + { + await db.SaveChangesAsync(); + return new ProcessorResult
(section); + } + catch (Exception e) + { + return e.ToProcessorResult
(); + } } public static Section GetSection(DataContext db, string sectionKey) => db.Sections.First(n => n.DataKey == sectionKey); diff --git a/I18N Commander/UI WinForms/Components/SectionTree.cs b/I18N Commander/UI WinForms/Components/SectionTree.cs index 2ebbc55..41fc3c3 100644 --- a/I18N Commander/UI WinForms/Components/SectionTree.cs +++ b/I18N Commander/UI WinForms/Components/SectionTree.cs @@ -141,11 +141,15 @@ public partial class SectionTree : UserControl // Add the new section to the database: var addedSection = await SectionProcessor.AddSection(this.db, result.Text, addRootNode ? null : selectedNode?.Name); + addedSection.ProcessError(); + if(!addedSection.Successful) + return; + // Add the new section to the tree control: var node = new TreeNode { - Name = addedSection.DataKey, // [sic] name is the key - Text = addedSection.Name, + Name = addedSection.Result!.DataKey, // [sic] name is the key + Text = addedSection.Result.Name, StateImageIndex = 1, }; @@ -233,8 +237,12 @@ public partial class SectionTree : UserControl // Rename the section: var alteredSection = await SectionProcessor.RenameSection(this.db, selectedNode.Name, result.Text); + alteredSection.ProcessError(); + if(!alteredSection.Successful) + return; + // Rename the selected node: - selectedNode.Text = alteredSection.Name; - selectedNode.Name = alteredSection.DataKey; // [sic] name is the key + selectedNode.Text = alteredSection.Result!.Name; + selectedNode.Name = alteredSection.Result.DataKey; // [sic] name is the key } } \ No newline at end of file