LANCommander/LANCommander.Server/UI/Components/ScriptEditorDialog.razor

80 lines
2.3 KiB
Text

@using LANCommander.SDK.Enums
@using LANCommander.Server.Extensions;
@using LANCommander.Server.Models;
@using LANCommander.Server.UI.Components;
@inherits FeedbackComponent<ScriptEditorOptions, Script>
@inject DatabaseServiceFactory DatabaseServiceFactory
@inject ModalService ModalService
@inject IMessageService MessageService
@inject ILogger<ScriptEditorDialog> Logger
<ScriptTextEditor
@bind-Script="@Script"
ArchiveId="@Options.ArchiveId"
AllowedTypes="@Options.AllowedTypes"
OnSave="Save" />
@code {
Guid Id = Guid.NewGuid();
Script Script;
protected override async Task OnInitializedAsync()
{
using (var scriptService = DatabaseServiceFactory.Create<ScriptService>())
{
if (Options.ScriptId != Guid.Empty)
Script = await scriptService.GetAsync(Options.ScriptId);
else if (Options.GameId != Guid.Empty)
Script = new Script()
{
GameId = Options.GameId
};
else if (Options.RedistributableId != Guid.Empty)
Script = new Script()
{
RedistributableId = Options.RedistributableId
};
else if (Options.ServerId != Guid.Empty)
Script = new Script()
{
ServerId = Options.ServerId
};
}
}
public override async Task OnFeedbackOkAsync(ModalClosingEventArgs args)
{
var success = await Save();
if (success)
await base.OkCancelRefWithResult!.OnOk(Script);
else
args.Reject();
}
async Task<bool> Save()
{
try
{
using (var scriptService = DatabaseServiceFactory.Create<ScriptService>())
{
if (Script.Id == Guid.Empty)
Script = await scriptService.AddAsync(Script);
else
Script = await scriptService.UpdateAsync(Script);
}
MessageService.Success("Script saved!");
return true;
}
catch (Exception ex)
{
MessageService.Error("Script could not be saved!");
Logger.LogError(ex, "Script could not be saved!");
return false;
}
}
}