Added some error handling to the section tree
This commit is contained in:
parent
2b4d613347
commit
bbefeb97a8
@ -30,7 +30,7 @@ public static class SectionProcessor
|
||||
/// <summary>
|
||||
/// Compute the new sections key and its depth, then store the section in the database.
|
||||
/// </summary>
|
||||
public static async Task<Section> AddSection(DataContext db, string text, string? parentKey)
|
||||
public static async Task<ProcessorResult<Section>> 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<Section>(rootSection);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.ToProcessorResult<Section>();
|
||||
}
|
||||
}
|
||||
|
||||
// Read the parent from the database:
|
||||
@ -78,9 +85,16 @@ public static class SectionProcessor
|
||||
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>(section);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return e.ToProcessorResult<Section>();
|
||||
}
|
||||
}
|
||||
|
||||
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<Section> RenameSection(DataContext db, string selectedNodeKey, string alteredName)
|
||||
public static async Task<ProcessorResult<Section>> 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>(section);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return e.ToProcessorResult<Section>();
|
||||
}
|
||||
}
|
||||
|
||||
public static Section GetSection(DataContext db, string sectionKey) => db.Sections.First(n => n.DataKey == sectionKey);
|
||||
|
@ -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
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user