LANCommander/LANCommander.Server/UI/Pages/Metadata/Collections/Edit.razor

155 lines
No EOL
4.9 KiB
Text

@page "/Metadata/Collections/{id:guid}"
@page "/Metadata/Collections/Add"
@using Microsoft.EntityFrameworkCore
@attribute [Authorize(Roles = RoleService.AdministratorRoleName)]
@inject CollectionService CollectionService
@inject IMessageService MessageService
@inject NavigationManager NavigationManager
@inject ILogger<Edit> Logger
<PageHeader>
<PageHeaderTitle>
<Input Size="InputSize.Large" @bind-Value="@Collection.Name" />
</PageHeaderTitle>
<PageHeaderExtra>
<Button Type="ButtonType.Primary" OnClick="Save">Save</Button>
</PageHeaderExtra>
</PageHeader>
<Table TItem="Game" DataSource="@Collection.Games" Responsive>
<Column TData="string" Title="Icon">
<Image Src="@GetIcon(context)" Height="32" Width="32" Preview="false"></Image>
</Column>
<PropertyColumn Property="g => g.Title" Sortable Filterable DefaultSortOrder="SortDirection.Ascending" />
<PropertyColumn Property="g => g.ReleasedOn" Format="MM/dd/yyyy" Sortable Filterable />
<PropertyColumn Property="g => g.Singleplayer" Sortable Filterable>
<Checkbox Disabled="true" Checked="context.Singleplayer" />
</PropertyColumn>
<Column TData="bool" Title="Multiplayer">
<Checkbox Disabled="true" Checked="context.MultiplayerModes?.Count > 0" />
</Column>
<Column TData="string[]" Title="Developers">
@foreach (var dev in context.Developers ?? [])
{
<Tag>@dev.Name</Tag>
}
</Column>
<Column TData="string[]" Title="Publishers">
@foreach (var pub in context.Publishers ?? [])
{
<Tag>@pub.Name</Tag>
}
</Column>
<Column TData="string[]" Title="Genres">
@foreach (var genre in context.Genres ?? [])
{
<Tag>@genre.Name</Tag>
}
</Column>
<Column TData="SDK.Enums.MultiplayerType[]" Title="Multiplayer Modes">
@foreach (var mode in context.MultiplayerModes?.Select(mm => mm.Type).Distinct() ?? [])
{
<Tag>@mode.GetDisplayName()</Tag>
}
</Column>
<ActionColumn Title="">
<Flex Gap="FlexGap.Small" Justify="FlexJustify.End">
<Popconfirm OnConfirm="() => RemoveGame(context)" Title="Are you sure you want to remove this game from the collection?">
<Button Icon="@IconType.Outline.Close" Type="ButtonType.Text" Danger/>
</Popconfirm>
</Flex>
</ActionColumn>
</Table>
@code {
[Parameter] public Guid Id { get; set; }
Collection Collection = new Collection();
protected override async Task OnInitializedAsync()
{
if (Id == Guid.Empty)
Collection = new Collection();
else
Collection = await CollectionService
.Query(q =>
{
return q
.Include(c => c.Games).ThenInclude(g => g.Media)
.Include(c => c.Games).ThenInclude(g => g.Developers)
.Include(c => c.Games).ThenInclude(g => g.Publishers)
.Include(c => c.Games).ThenInclude(g => g.Genres)
.Include(c => c.Games).ThenInclude(g => g.MultiplayerModes)
;
})
.AsSplitQuery()
.GetAsync(Id);
}
private async Task Save()
{
try
{
if (Collection.Id != Guid.Empty)
{
Collection = await CollectionService.UpdateAsync(Collection);
await MessageService.SuccessAsync("Collection updated!");
}
else
{
Collection = await CollectionService.AddAsync(Collection);
NavigationManager.LocationChanged += NotifyCollectionAdded;
NavigationManager.NavigateTo($"/Collections/{Collection.Id}");
}
}
catch (Exception ex)
{
MessageService.Error("Could not save!");
Logger.LogError(ex, "Could not save!");
}
}
private void NotifyCollectionAdded(object? sender, LocationChangedEventArgs e)
{
NavigationManager.LocationChanged -= NotifyCollectionAdded;
MessageService.Success("Collection added!");
}
private async Task RemoveGame(Game game)
{
try
{
Collection.Games.Remove(game);
await CollectionService.UpdateAsync(Collection);
}
catch (Exception ex)
{
MessageService.Error("Game could not be removed from the collection!");
Logger.LogError(ex, "Game could not be removed from the collection!");
}
}
private string GetIcon(Game game)
{
var media = game?.Media?.FirstOrDefault(m => m.Type == SDK.Enums.MediaType.Icon);
if (media != null)
return $"/api/Media/{media.Id}/Download?fileId={media.FileId}";
else
return "/favicon.ico";
}
}