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>
 | 
					    /// <summary>
 | 
				
			||||||
    /// Compute the new sections key and its depth, then store the section in the database.
 | 
					    /// Compute the new sections key and its depth, then store the section in the database.
 | 
				
			||||||
    /// </summary>
 | 
					    /// </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:
 | 
					        // 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();
 | 
					        var key = string.Join('_', text.Split(' ', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)).ToUpperInvariant();
 | 
				
			||||||
@ -58,9 +58,16 @@ public static class SectionProcessor
 | 
				
			|||||||
                TextElements = new(),
 | 
					                TextElements = new(),
 | 
				
			||||||
            };
 | 
					            };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            try
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
                db.Sections.Add(rootSection);
 | 
					                db.Sections.Add(rootSection);
 | 
				
			||||||
                await db.SaveChangesAsync();
 | 
					                await db.SaveChangesAsync();
 | 
				
			||||||
            return rootSection;
 | 
					                return new ProcessorResult<Section>(rootSection);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            catch (Exception e)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                e.ToProcessorResult<Section>();
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Read the parent from the database:
 | 
					        // Read the parent from the database:
 | 
				
			||||||
@ -78,9 +85,16 @@ public static class SectionProcessor
 | 
				
			|||||||
            Depth = parent.Depth + 1,
 | 
					            Depth = parent.Depth + 1,
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        try
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
            await db.Sections.AddAsync(section);
 | 
					            await db.Sections.AddAsync(section);
 | 
				
			||||||
            await db.SaveChangesAsync();
 | 
					            await db.SaveChangesAsync();
 | 
				
			||||||
        return section;
 | 
					            return new ProcessorResult<Section>(section);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        catch (Exception e)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            return e.ToProcessorResult<Section>();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public static async Task RemoveSection(DataContext db, string selectedKey)
 | 
					    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);
 | 
					        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:
 | 
					        // Read the section from the database:
 | 
				
			||||||
        var section = await db.Sections.FirstOrDefaultAsync(n => n.DataKey == selectedNodeKey);
 | 
					        var section = await db.Sections.FirstOrDefaultAsync(n => n.DataKey == selectedNodeKey);
 | 
				
			||||||
@ -132,8 +146,16 @@ public static class SectionProcessor
 | 
				
			|||||||
        
 | 
					        
 | 
				
			||||||
        section.Name = alteredName;
 | 
					        section.Name = alteredName;
 | 
				
			||||||
        section.DataKey = newKey;
 | 
					        section.DataKey = newKey;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        try
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
            await db.SaveChangesAsync();
 | 
					            await db.SaveChangesAsync();
 | 
				
			||||||
        return section;
 | 
					            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);
 | 
					    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:
 | 
					        // Add the new section to the database:
 | 
				
			||||||
        var addedSection = await SectionProcessor.AddSection(this.db, result.Text,  addRootNode ? null : selectedNode?.Name);
 | 
					        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:
 | 
					        // Add the new section to the tree control:
 | 
				
			||||||
        var node = new TreeNode
 | 
					        var node = new TreeNode
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            Name = addedSection.DataKey, // [sic] name is the key
 | 
					            Name = addedSection.Result!.DataKey, // [sic] name is the key
 | 
				
			||||||
            Text = addedSection.Name,
 | 
					            Text = addedSection.Result.Name,
 | 
				
			||||||
            StateImageIndex = 1,
 | 
					            StateImageIndex = 1,
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -233,8 +237,12 @@ public partial class SectionTree : UserControl
 | 
				
			|||||||
        // Rename the section:
 | 
					        // Rename the section:
 | 
				
			||||||
        var alteredSection = await SectionProcessor.RenameSection(this.db, selectedNode.Name, result.Text);
 | 
					        var alteredSection = await SectionProcessor.RenameSection(this.db, selectedNode.Name, result.Text);
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
 | 
					        alteredSection.ProcessError();
 | 
				
			||||||
 | 
					        if(!alteredSection.Successful)
 | 
				
			||||||
 | 
					            return;
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
        // Rename the selected node:
 | 
					        // Rename the selected node:
 | 
				
			||||||
        selectedNode.Text = alteredSection.Name;
 | 
					        selectedNode.Text = alteredSection.Result!.Name;
 | 
				
			||||||
        selectedNode.Name = alteredSection.DataKey; // [sic] name is the key
 | 
					        selectedNode.Name = alteredSection.Result.DataKey; // [sic] name is the key
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user