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