Fixed duplicated key issue

Closes #18
This commit is contained in:
Thorsten Sommer 2022-07-09 21:14:24 +02:00
parent 1d7e1db8e5
commit aa34f541cf
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108

View File

@ -35,6 +35,17 @@ public static class SectionProcessor
// 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();
// Check, if this key already exists:
if (await db.Sections.AnyAsync(n => n.DataKey == key))
{
var rng = new Random();
while (await db.Sections.AnyAsync(n => n.DataKey == key))
{
// Add a random number to the end of the key:
key += $"_{rng.Next(1, 10_000)}";
}
}
// In the case, when the user adds a section to the root, handle the insert differently: // In the case, when the user adds a section to the root, handle the insert differently:
if (string.IsNullOrEmpty(parentKey)) if (string.IsNullOrEmpty(parentKey))
{ {