Fixed path construction of sections

- Fixed missed loading of parents
- Made processing async
This commit is contained in:
Thorsten Sommer 2022-07-13 20:07:06 +02:00
parent 2b5de0aa1e
commit 0adc95b016
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
2 changed files with 13 additions and 5 deletions

View File

@ -138,13 +138,21 @@ public static class SectionProcessor
public static Section GetSection(DataContext db, string sectionKey) => db.Sections.First(n => n.DataKey == sectionKey);
public static string GetSectionPath(DataContext db, string sectionKey)
public static async Task<string> GetSectionPath(DataContext db, string sectionKey)
{
var section = db.Sections.First(n => n.DataKey == sectionKey);
var section = await db.Sections.FirstAsync(n => n.DataKey == sectionKey);
// Ensure, that the database loaded the section's parent:
await db.Entry(section).Reference(n => n.Parent).LoadAsync();
var path = section.Name;
while (section.Parent != null)
{
section = section.Parent;
section = await db.Sections.FirstAsync(n => n.DataKey == section.Parent.DataKey);
// Ensure, that the database loaded the section's parent:
await db.Entry(section).Reference(n => n.Parent).LoadAsync();
path = $"{section.Name}/{path}";
}

View File

@ -26,14 +26,14 @@ public partial class TextElements : UserControl
this.Disposed += (_, _) => this.db.Dispose();
// When the section is changed, update this component:
AppEvents.WhenSectionChanged += (sender, section) =>
AppEvents.WhenSectionChanged += async (sender, section) =>
{
this.currentSection = section;
this.buttonAdd.Enabled = this.currentSection is not null;
// Update the path:
if (this.currentSection is not null)
this.labelSectionPath.Text = SectionProcessor.GetSectionPath(this.db, this.currentSection.DataKey);
this.labelSectionPath.Text = await SectionProcessor.GetSectionPath(this.db, this.currentSection.DataKey);
};
}
}