LANCommander/LANCommander.Server/UI/Pages/Metadata/Collections/Index.razor
2025-01-21 18:48:01 -06:00

125 lines
4 KiB
Text

@page "/Metadata/Collections"
@using LANCommander.Server.Data
@using Microsoft.EntityFrameworkCore;
@using System.Web
@attribute [Authorize(Roles = RoleService.AdministratorRoleName)]
@inject DatabaseServiceFactory DatabaseServiceFactory
@inject NavigationManager NavigationManager
@inject IMessageService MessageService
@inject ILogger<Index> Logger
<PageHeader Title="Collections" />
<DataTable
TItem="Collection"
Size="@TableSize.Small"
Searchable
SearchProperty="c => c.Name">
<RightToolbar>
<Button OnClick="OpenAdd" Type="@ButtonType.Primary">Add Collection</Button>
</RightToolbar>
<Columns>
<BoundDataColumn Property="c => c.Name" Sortable DefaultSortOrder="SortDirection.Ascending" />
<BoundDataColumn Property="c => c.Games.Count" Sortable Title="Games" Include="Games" />
<BoundDataColumn Property="c => c.CreatedOn" Format="MM/dd/yyyy hh:mm tt" Sortable />
<BoundDataColumn Property="c => c.CreatedBy != null ? c.CreatedBy.UserName : String.Empty" Title="Created By" Sortable Include="CreatedBy" />
<BoundDataColumn Property="c => c.UpdatedOn" Format="MM/dd/yyyy hh:mm tt" Sortable />
<BoundDataColumn Property="c => c.UpdatedBy != null ? c.UpdatedBy.UserName : String.Empty" Title="Updated By" Sortable Include="UpdatedBy" />
<DataActions>
<a href="@($"/Metadata/Collections/{context.Id}")" class="ant-btn ant-btn-primary">Edit</a>
<Popconfirm OnConfirm="() => Delete(context)" Title="Are you sure you want to delete this collection?">
<Button Icon="@IconType.Outline.Close" Type="@ButtonType.Text" Danger />
</Popconfirm>
</DataActions>
</Columns>
</DataTable>
<Modal Title="Add Collection" OnOk="Add" OnCancel="CloseAdd" @bind-Visible="AddCollectionVisible">
<Form Model="@CollectionContext">
<FormItem Label="Name">
<Input @bind-Value="@context.Name" />
</FormItem>
</Form>
</Modal>
@code {
IEnumerable<Collection> Collections { get; set; } = new List<Collection>();
bool Visibility = false;
IEnumerable<Game> Selected;
DataTable<Game> Table;
string Url;
bool AddCollectionVisible = false;
Collection CollectionContext = new();
async Task<PaginatedResults<Collection>> LoadData(int pageIndex, int pageSize, string search)
{
var fuzzySearch = search.ToLower().Trim();
using (var collectionService = DatabaseServiceFactory.Create<CollectionService>())
{
return await collectionService
.Include(c => c.Games)
.Include(c => c.CreatedBy)
.Include(c => c.UpdatedBy)
.SortBy(c => c.Name)
.PaginateAsync(c => c.Name.ToLower().Contains(fuzzySearch),
pageIndex,
pageSize);
}
}
async Task OpenAdd()
{
CollectionContext = new Collection();
AddCollectionVisible = true;
await InvokeAsync(StateHasChanged);
}
async Task CloseAdd()
{
AddCollectionVisible = false;
await InvokeAsync(StateHasChanged);
}
async Task Add()
{
try
{
using (var collectionService = DatabaseServiceFactory.Create<CollectionService>())
{
await collectionService.AddMissingAsync(x => x.Name == CollectionContext.Name, CollectionContext);
}
MessageService.Success($"{CollectionContext.Name} was added!");
// await LoadData();
await CloseAdd();
}
catch (Exception ex)
{
MessageService.Error($"Could not add {CollectionContext.Name}!");
Logger.LogError(ex, $"Could not add {CollectionContext.Name}!");
}
}
async Task Delete(Collection Collection)
{
Collections = new List<Collection>();
using (var collectionService = DatabaseServiceFactory.Create<CollectionService>())
{
await collectionService.DeleteAsync(Collection);
}
// await LoadData();
}
}