LANCommander/LANCommander.Server/UI/Pages/Metadata/Platforms/Index.razor
2025-03-17 02:02:17 -05:00

129 lines
4 KiB
Text

@page "/Metadata/Platforms"
@attribute [Authorize(Roles = RoleService.AdministratorRoleName)]
@inject PlatformService PlatformService
@inject IMessageService MessageService
@inject ILogger<Index> Logger
<PageHeader Title="Platforms" />
<DataTable
TItem="Platform"
@ref="Table"
Size="@TableSize.Small"
Searchable
SearchProperty="p => p.Name">
<RightToolbar>
<Button OnClick="() => OpenEdit(null)" Type="@ButtonType.Primary">Add Platform</Button>
</RightToolbar>
<Columns>
<BoundDataColumn Property="p => p.Name" Sortable DefaultSortOrder="SortDirection.Ascending" />
<BoundDataColumn Property="p => p.Games.Count" Sortable Title="Games" Include="Games" />
<BoundDataColumn Property="p => p.CreatedOn" Title="Created On" Sortable>
<LocalTime Value="context.UpdatedOn" />
</BoundDataColumn>
<BoundDataColumn Property="p => p.CreatedBy != null ? p.CreatedBy.UserName : String.Empty" Title="Created By" Sortable Include="CreatedBy" />
<BoundDataColumn Property="p => p.UpdatedOn" Title="Updated On" Sortable>
<LocalTime Value="context.UpdatedOn" />
</BoundDataColumn>
<BoundDataColumn Property="p => p.UpdatedBy != null ? p.UpdatedBy.UserName : String.Empty" Title="Updated By" Sortable Include="UpdatedBy" />
<DataActions>
<a href="@($"/Metadata/Platforms/{context.Id}")" class="ant-btn ant-btn-primary">Edit</a>
<Popconfirm OnConfirm="() => Delete(context)" Title="Are you sure you want to delete this platform?">
<Button Icon="@IconType.Outline.Close" Type="@ButtonType.Text" Danger />
</Popconfirm>
</DataActions>
</Columns>
</DataTable>
<Modal Title="@(PlatformContext.Id == Guid.Empty ? "New Platform" : "Edit Platform")"
@bind-Visible="@EditPlatformVisible"
OnOk="UpdateOrAdd"
OnCancel="CloseEdit">
<Form Model="@PlatformContext">
<FormItem Label="Name">
<Input @bind-Value="@context.Name" AutoFocus />
</FormItem>
</Form>
</Modal>
@code {
IEnumerable<Platform> Platforms { get; set; } = new List<Platform>();
bool Loading = true;
bool EditPlatformVisible = false;
Platform PlatformContext = new();
DataTable<Platform> Table;
protected override async Task OnInitializedAsync()
{
Loading = false;
}
async Task UpdateOrAdd()
{
try
{
if (PlatformContext.Id == Guid.Empty)
{
await PlatformService.AddMissingAsync(x => x.Name == PlatformContext.Name, PlatformContext);
MessageService.Success($"{PlatformContext.Name} was added!");
}
else
{
await PlatformService.UpdateAsync(PlatformContext);
MessageService.Success($"{PlatformContext.Name} was updated!");
}
}
catch (Exception ex)
{
if (PlatformContext.Id == Guid.Empty)
MessageService.Error($"Could not add {PlatformContext.Name}!");
else
MessageService.Error($"Could not update {PlatformContext.Name}!");
Logger.LogError(ex, $"Could not add/update {PlatformContext.Name}!");
}
Table.Reload();
await CloseEdit();
}
async Task OpenEdit(Platform platform)
{
if (platform != null)
PlatformContext = platform;
EditPlatformVisible = true;
await InvokeAsync(StateHasChanged);
}
async Task CloseEdit()
{
EditPlatformVisible = false;
await InvokeAsync(StateHasChanged);
}
async Task Delete(Platform platform)
{
try
{
await PlatformService.DeleteAsync(platform);
MessageService.Success("Platform deleted!");
Table.Reload();
}
catch (Exception ex)
{
MessageService.Error("Could not delete platform!");
Logger?.LogError(ex, "Could not delete platform");
}
}
}