120 lines
3.8 KiB
Text
120 lines
3.8 KiB
Text
@page "/Metadata/Companies"
|
|
@attribute [Authorize(Roles = RoleService.AdministratorRoleName)]
|
|
@inject CompanyService CompanyService
|
|
@inject IMessageService MessageService
|
|
@inject ILogger<Index> Logger
|
|
|
|
<PageHeader Title="Companies" />
|
|
|
|
<DataTable
|
|
TItem="Company"
|
|
@ref="Table"
|
|
Size="TableSize.Small"
|
|
Responsive
|
|
Searchable
|
|
SearchProperty="c => c.Name">
|
|
<RightToolbar>
|
|
<Button OnClick="() => OpenEdit(null)" Type="@ButtonType.Primary">Add Company</Button>
|
|
</RightToolbar>
|
|
<Columns>
|
|
<BoundDataColumn Property="c => c.Name" Sortable DefaultSortOrder="SortDirection.Ascending" />
|
|
<BoundDataColumn Property="c => c.CreatedOn" Title="Created On" Sortable>
|
|
<LocalTime Value="context.UpdatedOn" />
|
|
</BoundDataColumn>
|
|
<BoundDataColumn Property="c => c.CreatedBy != null ? c.CreatedBy.UserName : String.Empty" Title="Created By" Sortable Include="CreatedBy" />
|
|
<BoundDataColumn Property="c => c.UpdatedOn" Title="Updated On" Sortable>
|
|
<LocalTime Value="context.UpdatedOn" />
|
|
</BoundDataColumn>
|
|
<BoundDataColumn Property="c => c.UpdatedBy != null ? c.UpdatedBy.UserName : String.Empty" Title="Updated By" Sortable Include="UpdatedBy" />
|
|
<BoundDataColumn Property="c => c.DevelopedGames.Count" Sortable Title="Developed Games" Include="DevelopedGames" />
|
|
<BoundDataColumn Property="c => c.PublishedGames.Count" Sortable Title="Published Games" Include="PublishedGames" />
|
|
<DataActions>
|
|
<Popconfirm OnConfirm="() => Delete(context)" Title="Are you sure you want to delete this company?">
|
|
<Button Type="@ButtonType.Text" Icon="@IconType.Outline.Close" Danger />
|
|
</Popconfirm>
|
|
</DataActions>
|
|
</Columns>
|
|
</DataTable>
|
|
|
|
<Modal Title="@(CompanyContext.Id == Guid.Empty ? "New Company" : "Edit Company")"
|
|
@bind-Visible="@EditCompanyVisible"
|
|
OnOk="UpdateOrAdd"
|
|
OnCancel="CloseEdit">
|
|
<Form Model="@CompanyContext">
|
|
<FormItem Label="Name">
|
|
<Input @bind-Value="@context.Name" AutoFocus />
|
|
</FormItem>
|
|
</Form>
|
|
</Modal>
|
|
|
|
@code {
|
|
DataTable<Company> Table;
|
|
bool EditCompanyVisible = false;
|
|
Company CompanyContext = new();
|
|
|
|
async Task UpdateOrAdd()
|
|
{
|
|
try
|
|
{
|
|
if (CompanyContext.Id == Guid.Empty)
|
|
{
|
|
await CompanyService.AddMissingAsync(x => x.Name == CompanyContext.Name, CompanyContext);
|
|
|
|
MessageService.Success($"{CompanyContext.Name} was added!");
|
|
}
|
|
else
|
|
{
|
|
await CompanyService.UpdateAsync(CompanyContext);
|
|
|
|
MessageService.Success($"{CompanyContext.Name} was updated!");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (CompanyContext.Id == Guid.Empty)
|
|
MessageService.Error($"Could not add {CompanyContext.Name}!");
|
|
else
|
|
MessageService.Error($"Could not update {CompanyContext.Name}!");
|
|
|
|
Logger.LogError(ex, $"Could not update/add {CompanyContext.Name}!");
|
|
}
|
|
|
|
Table.Reload();
|
|
|
|
await CloseEdit();
|
|
}
|
|
|
|
async Task OpenEdit(Company company)
|
|
{
|
|
if (company != null)
|
|
CompanyContext = company;
|
|
|
|
EditCompanyVisible = true;
|
|
|
|
await InvokeAsync(StateHasChanged);
|
|
}
|
|
|
|
async Task CloseEdit()
|
|
{
|
|
EditCompanyVisible = false;
|
|
|
|
await InvokeAsync(StateHasChanged);
|
|
}
|
|
|
|
async Task Delete(Company company)
|
|
{
|
|
try
|
|
{
|
|
await CompanyService.DeleteAsync(company);
|
|
|
|
MessageService.Success("Company deleted!");
|
|
|
|
Table.Reload();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageService.Error("Could not delete company!");
|
|
Logger?.LogError(ex, "Could not delete company");
|
|
}
|
|
}
|
|
}
|