Implemented the delete function for text elements

This commit is contained in:
Thorsten Sommer 2022-07-23 21:21:48 +02:00
parent e762cfe21e
commit 71936e5dcf
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
2 changed files with 31 additions and 1 deletions

View File

@ -91,4 +91,23 @@ public static class TextElementProcessor
return updateException.ToProcessorResult<TextElement>();
}
}
// Deletes a text element:
public static async Task DeleteTextElement(int id)
{
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
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)
{
}
}
}

View File

@ -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)