Added some error handling to the section tree
This commit is contained in:
2 files changed
+45
-15
No files matched your search
@@ -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:
|
||||
@@ -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>(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);
|
||||
|
||||
Reference in new issue
Block a user