Implemented the add text element function
This commit is contained in:
parent
33fe8f5dac
commit
2b4d613347
@ -1,5 +1,4 @@
|
||||
using System.Collections;
|
||||
using DataModel.Database;
|
||||
using DataModel.Database;
|
||||
using DataModel.Database.Common;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
@ -11,4 +10,49 @@ public static class TextElementProcessor
|
||||
{
|
||||
return Task.FromResult(db.TextElements.Where(n => n.Section == section).AsAsyncEnumerable());
|
||||
}
|
||||
|
||||
public static async Task<ProcessorResult<TextElement>> AddTextElement(DataContext db, string? currentSectionKey, string elementName)
|
||||
{
|
||||
if(string.IsNullOrWhiteSpace(currentSectionKey))
|
||||
throw new ArgumentOutOfRangeException(nameof(currentSectionKey));
|
||||
|
||||
var currentSection = await db.Sections.FirstOrDefaultAsync(n => n.DataKey == currentSectionKey);
|
||||
if (currentSection is null)
|
||||
throw new ArgumentOutOfRangeException(nameof(currentSectionKey));
|
||||
|
||||
// Remove any whitespaces from the element name, regardless of how many e.g. spaces the user typed:
|
||||
var code = string.Join('_', elementName.Split(' ', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)).ToUpperInvariant();
|
||||
|
||||
// Check, if this key already exists:
|
||||
if (await db.TextElements.AnyAsync(n => n.Section == currentSection && n.Code == code))
|
||||
{
|
||||
var rng = new Random();
|
||||
while (await db.TextElements.AnyAsync(n => n.Section == currentSection && n.Code == code))
|
||||
{
|
||||
// Add a random number to the end of the key:
|
||||
code += $"_{rng.Next(1, 10_000)}";
|
||||
}
|
||||
}
|
||||
|
||||
var textElement = new TextElement
|
||||
{
|
||||
Name = elementName,
|
||||
Code = code,
|
||||
Section = currentSection,
|
||||
};
|
||||
|
||||
// Add the new element to the database:
|
||||
await db.TextElements.AddAsync(textElement);
|
||||
|
||||
try
|
||||
{
|
||||
// Save the changes:
|
||||
await db.SaveChangesAsync();
|
||||
return new ProcessorResult<TextElement>(textElement);
|
||||
}
|
||||
catch (DbUpdateException updateException)
|
||||
{
|
||||
return updateException.ToProcessorResult<TextElement>();
|
||||
}
|
||||
}
|
||||
}
|
@ -37,8 +37,8 @@
|
||||
this.listTextElements = new System.Windows.Forms.ListBox();
|
||||
this.textBoxFilter = new System.Windows.Forms.TextBox();
|
||||
this.labelFilter = new System.Windows.Forms.Label();
|
||||
this.toolTip = new System.Windows.Forms.ToolTip(this.components);
|
||||
this.labelSectionPath = new System.Windows.Forms.Label();
|
||||
this.toolTip = new System.Windows.Forms.ToolTip(this.components);
|
||||
this.tableLayout.SuspendLayout();
|
||||
this.flowLayoutToolbar.SuspendLayout();
|
||||
this.SuspendLayout();
|
||||
@ -90,6 +90,7 @@
|
||||
this.buttonAdd.TabIndex = 0;
|
||||
this.toolTip.SetToolTip(this.buttonAdd, "Add text element to selected section");
|
||||
this.buttonAdd.UseVisualStyleBackColor = true;
|
||||
this.buttonAdd.Click += new System.EventHandler(this.buttonAdd_Click);
|
||||
//
|
||||
// buttonRemove
|
||||
//
|
||||
@ -103,6 +104,7 @@
|
||||
this.buttonRemove.TabIndex = 2;
|
||||
this.toolTip.SetToolTip(this.buttonRemove, "Delete this text element");
|
||||
this.buttonRemove.UseVisualStyleBackColor = true;
|
||||
this.buttonRemove.Click += new System.EventHandler(this.buttonRemove_Click);
|
||||
//
|
||||
// buttonRename
|
||||
//
|
||||
@ -116,6 +118,7 @@
|
||||
this.buttonRename.TabIndex = 1;
|
||||
this.toolTip.SetToolTip(this.buttonRename, "Rename this text element");
|
||||
this.buttonRename.UseVisualStyleBackColor = true;
|
||||
this.buttonRename.Click += new System.EventHandler(this.buttonRename_Click);
|
||||
//
|
||||
// listTextElements
|
||||
//
|
||||
@ -150,14 +153,6 @@
|
||||
this.labelFilter.Text = "Filter:";
|
||||
this.labelFilter.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
|
||||
//
|
||||
// toolTip
|
||||
//
|
||||
this.toolTip.AutoPopDelay = 30000;
|
||||
this.toolTip.InitialDelay = 500;
|
||||
this.toolTip.ReshowDelay = 100;
|
||||
this.toolTip.ToolTipIcon = System.Windows.Forms.ToolTipIcon.Info;
|
||||
this.toolTip.ToolTipTitle = "Help";
|
||||
//
|
||||
// labelSectionPath
|
||||
//
|
||||
this.labelSectionPath.AutoSize = true;
|
||||
@ -171,6 +166,14 @@
|
||||
this.labelSectionPath.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
|
||||
this.toolTip.SetToolTip(this.labelSectionPath, "The path of the currently selected section");
|
||||
//
|
||||
// toolTip
|
||||
//
|
||||
this.toolTip.AutoPopDelay = 30000;
|
||||
this.toolTip.InitialDelay = 500;
|
||||
this.toolTip.ReshowDelay = 100;
|
||||
this.toolTip.ToolTipIcon = System.Windows.Forms.ToolTipIcon.Info;
|
||||
this.toolTip.ToolTipTitle = "Help";
|
||||
//
|
||||
// TextElements
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(120F, 120F);
|
||||
|
@ -2,6 +2,7 @@
|
||||
using DataModel.Database.Common;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Processor;
|
||||
using UI_WinForms.Dialogs;
|
||||
|
||||
namespace UI_WinForms.Components;
|
||||
|
||||
@ -55,10 +56,51 @@ public partial class TextElements : UserControl
|
||||
{
|
||||
var item = new ListViewItem(textElement.Name)
|
||||
{
|
||||
Tag = textElement
|
||||
Tag = textElement.Code,
|
||||
};
|
||||
|
||||
this.listTextElements.Items.Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
private async void buttonAdd_Click(object sender, EventArgs e)
|
||||
{
|
||||
if(this.DesignMode)
|
||||
return;
|
||||
|
||||
var result = InputDialog.Show(new InputDialog.Options(
|
||||
Message: "Please type the desired text element's name.",
|
||||
Title: "Add a text element",
|
||||
Placeholder: "My text element",
|
||||
ShowQuestionCheckbox: false
|
||||
));
|
||||
|
||||
if(result.DialogResult == DialogResult.Cancel)
|
||||
return;
|
||||
|
||||
// Add the text element to the database into the current section:
|
||||
var newTextElement = await TextElementProcessor.AddTextElement(this.db, this.currentSection?.DataKey, result.Text);
|
||||
newTextElement.ProcessError();
|
||||
|
||||
if(!newTextElement.Successful)
|
||||
return;
|
||||
|
||||
// Add the text element to the list:
|
||||
var item = new ListViewItem(newTextElement.Result!.Name)
|
||||
{
|
||||
Tag = newTextElement.Result.Code,
|
||||
};
|
||||
|
||||
this.listTextElements.Items.Add(item);
|
||||
}
|
||||
|
||||
private void buttonRemove_Click(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void buttonRename_Click(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
@ -60,4 +60,10 @@
|
||||
<metadata name="toolTip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>17, 17</value>
|
||||
</metadata>
|
||||
<metadata name="toolTip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>17, 17</value>
|
||||
</metadata>
|
||||
<metadata name="toolTip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>17, 17</value>
|
||||
</metadata>
|
||||
</root>
|
Loading…
Reference in New Issue
Block a user