Implemented the text element rename function
This commit is contained in:
2 files changed
+58
-1
No files matched your search
@@ -57,4 +57,34 @@ public static class TextElementProcessor
|
||||
return updateException.ToProcessorResult<TextElement>();
|
||||
}
|
||||
}
|
||||
|
||||
// Renames a text element:
|
||||
public static async Task<ProcessorResult<TextElement>> RenameTextElement(int id, string newName)
|
||||
{
|
||||
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
|
||||
|
||||
var textElement = await db.TextElements.FirstAsync(n => n.Id == id);
|
||||
if (textElement is null)
|
||||
throw new ArgumentOutOfRangeException(nameof(id));
|
||||
|
||||
// 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;
|
||||
|
||||
// Save the changes:
|
||||
try
|
||||
{
|
||||
await db.SaveChangesAsync();
|
||||
return new ProcessorResult<TextElement>(textElement);
|
||||
}
|
||||
catch (DbUpdateException updateException)
|
||||
{
|
||||
return updateException.ToProcessorResult<TextElement>();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user