2022-07-17 10:56:06 +00:00
|
|
|
|
using DataModel.Database;
|
2022-07-16 20:40:06 +00:00
|
|
|
|
using DataModel.Database.Common;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2022-07-17 13:15:55 +00:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2022-07-16 20:40:06 +00:00
|
|
|
|
|
|
|
|
|
namespace Processor;
|
|
|
|
|
|
|
|
|
|
public static class TextElementProcessor
|
|
|
|
|
{
|
2022-07-20 18:51:12 +00:00
|
|
|
|
// Load all text elements for one particular section:
|
2022-07-21 19:14:32 +00:00
|
|
|
|
public static async Task<List<TextElement>> GetTextElements(Section section, string filterTerm)
|
2022-07-16 20:40:06 +00:00
|
|
|
|
{
|
2022-07-17 13:15:55 +00:00
|
|
|
|
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
|
2022-07-21 19:14:32 +00:00
|
|
|
|
|
|
|
|
|
if(string.IsNullOrWhiteSpace(filterTerm))
|
|
|
|
|
return await db.TextElements.Where(n => n.Section == section).OrderBy(n => n.Name).ThenBy(n => n.Id).ToListAsync();
|
|
|
|
|
else
|
|
|
|
|
return await db.TextElements.Where(n => n.Section == section && n.Name.Contains(filterTerm)).OrderBy(n => n.Name).ThenBy(n => n.Id).ToListAsync();
|
2022-07-16 20:40:06 +00:00
|
|
|
|
}
|
2022-07-20 18:51:12 +00:00
|
|
|
|
|
|
|
|
|
// Load one text element by id:
|
|
|
|
|
public static async Task<TextElement> LoadTextElement(int id)
|
|
|
|
|
{
|
|
|
|
|
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
|
|
|
|
|
return await db.TextElements.FirstAsync(n => n.Id == id);
|
|
|
|
|
}
|
2022-07-17 10:56:06 +00:00
|
|
|
|
|
2022-07-20 18:51:12 +00:00
|
|
|
|
// Adds a new text element:
|
2022-09-17 17:23:11 +00:00
|
|
|
|
public static async Task<ProcessorResult<TextElement>> AddTextElement(string? currentSectionKey, string elementName, bool isMultiLine)
|
2022-07-17 10:56:06 +00:00
|
|
|
|
{
|
2022-07-17 13:15:55 +00:00
|
|
|
|
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
|
|
|
|
|
|
2022-07-17 10:56:06 +00:00
|
|
|
|
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));
|
|
|
|
|
|
2022-07-18 19:32:39 +00:00
|
|
|
|
// Generate a code:
|
|
|
|
|
var code = await Utils.GenerateCode(elementName, db.TextElements, (n, code) => n.Section == currentSection && n.Code == code);
|
2022-07-17 10:56:06 +00:00
|
|
|
|
|
|
|
|
|
var textElement = new TextElement
|
|
|
|
|
{
|
|
|
|
|
Name = elementName,
|
|
|
|
|
Code = code,
|
|
|
|
|
Section = currentSection,
|
2022-09-17 17:23:11 +00:00
|
|
|
|
IsMultiLine = isMultiLine,
|
2022-07-17 10:56:06 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 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>();
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-21 18:35:22 +00:00
|
|
|
|
|
2022-09-17 17:23:11 +00:00
|
|
|
|
// Updates the multi-line flag of a text element:
|
|
|
|
|
public static async Task<ProcessorResult<TextElement>> UpdateTextElementState(int id, bool isMultiLine)
|
2022-07-21 18:35:22 +00:00
|
|
|
|
{
|
|
|
|
|
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
|
|
|
|
|
var textElement = await db.TextElements.FirstAsync(n => n.Id == id);
|
2022-09-17 17:23:11 +00:00
|
|
|
|
textElement.IsMultiLine = isMultiLine;
|
2022-07-21 18:35:22 +00:00
|
|
|
|
|
2022-09-17 17:23:11 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Save the changes:
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
return new ProcessorResult<TextElement>(textElement);
|
|
|
|
|
}
|
|
|
|
|
catch (DbUpdateException updateException)
|
|
|
|
|
{
|
|
|
|
|
return updateException.ToProcessorResult<TextElement>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Renames a text element:
|
|
|
|
|
public static async Task<ProcessorResult<TextElement>> RenameTextElement(int id, string newName, bool isMultiLine)
|
|
|
|
|
{
|
|
|
|
|
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
|
|
|
|
|
var textElement = await db.TextElements.FirstAsync(n => n.Id == id);
|
|
|
|
|
|
2022-07-21 18:35:22 +00:00
|
|
|
|
// Get the corresponding section:
|
|
|
|
|
var section = (await db.TextElements.FirstAsync(n => n.Id == id)).Section;
|
|
|
|
|
|
|
|
|
|
// Generate a code:
|
|
|
|
|
var code = await Utils.GenerateCode(newName, db.TextElements, (n, code) => n.Section == section && n.Code == code);
|
|
|
|
|
|
|
|
|
|
textElement.Name = newName;
|
|
|
|
|
textElement.Code = code;
|
2022-09-17 17:23:11 +00:00
|
|
|
|
textElement.IsMultiLine = isMultiLine;
|
2022-07-21 18:35:22 +00:00
|
|
|
|
|
|
|
|
|
// Save the changes:
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
return new ProcessorResult<TextElement>(textElement);
|
|
|
|
|
}
|
|
|
|
|
catch (DbUpdateException updateException)
|
|
|
|
|
{
|
|
|
|
|
return updateException.ToProcessorResult<TextElement>();
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-23 19:21:48 +00:00
|
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
}
|
2022-10-30 14:49:22 +00:00
|
|
|
|
catch (DbUpdateException)
|
2022-07-23 19:21:48 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-15 19:32:47 +00:00
|
|
|
|
|
|
|
|
|
public static async Task<string> GetKey(int id)
|
|
|
|
|
{
|
|
|
|
|
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
|
|
|
|
|
var textElement = await db.TextElements.FirstAsync(n => n.Id == id);
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Build the path to the text element:
|
|
|
|
|
//
|
|
|
|
|
var textElementPath = new List<string>(textElement.Section.Depth + 2)
|
|
|
|
|
{
|
|
|
|
|
textElement.Code
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var parent = textElement.Section;
|
|
|
|
|
while (parent is not null)
|
|
|
|
|
{
|
|
|
|
|
// Load the parent's parent section:
|
|
|
|
|
await db.Entry(parent).Reference(n => n.Parent).LoadAsync();
|
|
|
|
|
|
|
|
|
|
// Add the parent's key to the path:
|
|
|
|
|
textElementPath.Add(parent.DataKey);
|
|
|
|
|
|
|
|
|
|
// Go to parent's parent:
|
|
|
|
|
parent = parent.Parent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
textElementPath.Add("I18N");
|
|
|
|
|
|
|
|
|
|
// Write the path to the text element as key:
|
|
|
|
|
return $"{string.Join(".", textElementPath.ReverseIt())}";
|
|
|
|
|
}
|
2022-07-16 20:40:06 +00:00
|
|
|
|
}
|