Added the selection processor for adding & loading data
This commit is contained in:
parent
9015e08518
commit
c90dfa1ac0
74
I18N Commander/Processor/SectionProcessor.cs
Normal file
74
I18N Commander/Processor/SectionProcessor.cs
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
using DataModel.Database;
|
||||||
|
using DataModel.Database.Common;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace Processor;
|
||||||
|
|
||||||
|
public static class SectionProcessor
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Load one layer of the tree by using the specified depth:
|
||||||
|
/// </summary>
|
||||||
|
public static IAsyncEnumerable<Section> LoadLayer(DataContext db, int depth)
|
||||||
|
{
|
||||||
|
return db.Sections.Where(n => n.Depth == depth).OrderBy(n => n.DataKey).AsAsyncEnumerable();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Determine how deep the tree is.
|
||||||
|
/// </summary>
|
||||||
|
public static async ValueTask<int> GetDepth(DataContext db)
|
||||||
|
{
|
||||||
|
if(!await db.Sections.AnyAsync())
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return await db.Sections.MaxAsync(s => s.Depth);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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)
|
||||||
|
{
|
||||||
|
// 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();
|
||||||
|
|
||||||
|
// In the case, when the user adds a section to the root, handle the insert differently:
|
||||||
|
if (string.IsNullOrEmpty(parentKey))
|
||||||
|
{
|
||||||
|
var rootSection = new Section
|
||||||
|
{
|
||||||
|
Depth = 0,
|
||||||
|
DataKey = key,
|
||||||
|
Parent = null,
|
||||||
|
Name = text.Trim(),
|
||||||
|
TextElements = new(),
|
||||||
|
};
|
||||||
|
|
||||||
|
db.Sections.Add(rootSection);
|
||||||
|
await db.SaveChangesAsync();
|
||||||
|
return rootSection;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read the parent from the database:
|
||||||
|
var parent = await db.Sections.FirstOrDefaultAsync(n => n.DataKey == parentKey);
|
||||||
|
if (parent is null)
|
||||||
|
throw new ArgumentException($"The section's parent with key {parentKey} does not exist in the database.");
|
||||||
|
|
||||||
|
// Add the new section to the database:
|
||||||
|
var section = new Section
|
||||||
|
{
|
||||||
|
Name = text.Trim(),
|
||||||
|
DataKey = key,
|
||||||
|
Parent = parent,
|
||||||
|
TextElements = new(),
|
||||||
|
Depth = parent.Depth + 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
db.Sections.Add(section);
|
||||||
|
await db.SaveChangesAsync();
|
||||||
|
return section;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user