@using System.Collections.ObjectModel @using Action = LANCommander.Server.Data.Models.Action @inject ActionService ActionService @inject ArchiveService ArchiveService @inject MessageService MessageService @inject ILogger Logger
@code { [CascadingParameter(Name = "GameId")] public Guid? GameId { get; set; } [CascadingParameter(Name = "ServerId")] public Guid? ServerId { get; set; } Archive? _archive; Game? _game; Server? _server; ObservableCollection _sortedActions = new(); protected override async Task OnInitializedAsync() => await LoadActions(); async Task LoadActions() { if (GameId.HasValue) { _sortedActions = new ObservableCollection(await ActionService.SortBy(a => a.SortOrder).GetAsync(a => a.GameId == GameId.Value)); _archive = await ArchiveService.GetLatestArchiveAsync(a => a.GameId == GameId); } if (ServerId.HasValue) _sortedActions = new ObservableCollection(await ActionService.SortBy(a => a.SortOrder).GetAsync(a => a.ServerId == ServerId.Value)); } void AddAction() { var newAction = new Action { PrimaryAction = _sortedActions.Count == 0, SortOrder = _sortedActions.Count }; if (GameId.HasValue) newAction.GameId = GameId; if (ServerId.HasValue) newAction.ServerId = ServerId; _sortedActions.Add(newAction); UpdateOrder(); } async Task RemoveAction(Action action) { await ActionService.DeleteAsync(action); _sortedActions.Remove(action); UpdateOrder(); } async Task MoveUp(Action action) { var index = _sortedActions.IndexOf(action); if (index > 0) _sortedActions.Move(index, index - 1); UpdateOrder(); } async Task MoveDown(Action action) { var index = _sortedActions.IndexOf(action); if (index < _sortedActions.Count - 1) _sortedActions.Move(index, index + 1); UpdateOrder(); } void UpdateOrder() { foreach (var action in _sortedActions) action.SortOrder = _sortedActions.IndexOf(action); if (GameId.HasValue && _game != null) _game.Actions = _sortedActions; if (ServerId.HasValue && _server != null) _server.Actions = _sortedActions; } public async Task Save() { try { foreach (var action in _sortedActions) { if (action.Id == Guid.Empty) await ActionService.AddAsync(action); else await ActionService.UpdateAsync(action); } await LoadActions(); MessageService.Success("Actions updated!"); } catch (Exception ex) { MessageService.Error("Could not update actions!"); Logger.LogError(ex, "Could not update actions!"); } } void OnPathSelected(string path, Action action) { action.Path = path; action.WorkingDirectory = "{InstallDir}"; } }