From 71936e5dcf139ec0e8069192de693835e5b7138e Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sat, 23 Jul 2022 21:21:48 +0200 Subject: [PATCH] Implemented the delete function for text elements --- .../Processor/TextElementProcessor.cs | 19 +++++++++++++++++++ .../UI WinForms/Components/TextElements.cs | 13 ++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/I18N Commander/Processor/TextElementProcessor.cs b/I18N Commander/Processor/TextElementProcessor.cs index 8c63e17..7f30adc 100644 --- a/I18N Commander/Processor/TextElementProcessor.cs +++ b/I18N Commander/Processor/TextElementProcessor.cs @@ -91,4 +91,23 @@ public static class TextElementProcessor return updateException.ToProcessorResult(); } } + + // Deletes a text element: + public static async Task DeleteTextElement(int id) + { + await using var db = ProcessorMeta.ServiceProvider.GetRequiredService(); + var textElement = await db.TextElements.FirstAsync(n => n.Id == id); + + // Remove the element from the database: + db.TextElements.Remove(textElement); + + try + { + // Save the changes: + await db.SaveChangesAsync(); + } + catch (DbUpdateException updateException) + { + } + } } \ No newline at end of file diff --git a/I18N Commander/UI WinForms/Components/TextElements.cs b/I18N Commander/UI WinForms/Components/TextElements.cs index aa6e354..abed227 100644 --- a/I18N Commander/UI WinForms/Components/TextElements.cs +++ b/I18N Commander/UI WinForms/Components/TextElements.cs @@ -102,9 +102,20 @@ public partial class TextElements : UserControl this.column.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent); } - private void buttonRemove_Click(object sender, EventArgs e) + private async void buttonRemove_Click(object sender, EventArgs e) { + if(this.DesignMode || this.currentTextElement is null) + return; + // Ask the user, if he really wants to remove the text element: + if(MessageBox.Show(this.currentTextElement.Translations.Count > 0 ? $"Are you sure, you want to remove the text element '{this.currentTextElement.Name}', its {this.currentTextElement.Translations.Count} translations and so on?" : $"Are you sure, you want to remove the text element '{this.currentTextElement.Name}'?", "Remove text element", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No) + return; + + // Remove the text element1 from the database: + await TextElementProcessor.DeleteTextElement(this.currentTextElement.Id); + + // Reload the data: + await this.LoadTextElements(); } private async void buttonRename_Click(object sender, EventArgs e)