From aa34f541cf48a7ed738a291c079c7f3948068b6b Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sat, 9 Jul 2022 21:14:24 +0200 Subject: [PATCH] Fixed duplicated key issue Closes #18 --- I18N Commander/Processor/SectionProcessor.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/I18N Commander/Processor/SectionProcessor.cs b/I18N Commander/Processor/SectionProcessor.cs index dfd064e..6f9b197 100644 --- a/I18N Commander/Processor/SectionProcessor.cs +++ b/I18N Commander/Processor/SectionProcessor.cs @@ -34,6 +34,17 @@ public static class SectionProcessor { // 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(); + + // 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: if (string.IsNullOrEmpty(parentKey))