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

158 lines
No EOL
5 KiB
Text

@page "/Metadata/Collections/{id:guid}"
@page "/Metadata/Collections/Add"
@using LANCommander.Server.Extensions
@attribute [Authorize(Roles = RoleService.AdministratorRoleName)]
@inject DatabaseServiceFactory DatabaseServiceFactory
@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="" Style="text-align: right">
<ChildContent>
<Space Direction="DirectionVHType.Horizontal">
<SpaceItem>
<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>
</SpaceItem>
</Space>
</ChildContent>
</ActionColumn>
</Table>
@code {
[Parameter] public Guid Id { get; set; }
Collection Collection = new Collection();
protected override async Task OnInitializedAsync()
{
using (var collectionService = DatabaseServiceFactory.Create<CollectionService>())
{
if (Id == Guid.Empty)
Collection = new Collection();
else
Collection = await collectionService
.Include(c => c.Games)
.GetAsync(Id);
}
}
private async Task Save()
{
try
{
using (var collectionService = DatabaseServiceFactory.Create<CollectionService>())
{
if (Collection.Id != Guid.Empty)
{
Collection = await collectionService.UpdateAsync(Collection);
await MessageService.Success("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);
using (var collectionService = DatabaseServiceFactory.Create<CollectionService>())
{
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";
}
}