LANCommander/LANCommander.Server/UI/Pages/Games/Components/AddToCollectionDialog.razor
2024-12-31 18:21:41 -06:00

134 lines
4.4 KiB
Text

@using LANCommander.Server.Models
@using Microsoft.EntityFrameworkCore
@inherits FeedbackComponent<AddToCollectionOptions, IEnumerable<Collection>>
@inject DatabaseServiceFactory DatabaseServiceFactory
@inject IMessageService MessageService
@inject ILogger<AddToCollectionDialog> Logger
<Select
AllowClear
Mode="SelectMode.Multiple"
TItem="Collection"
TItemValue="Guid"
DataSource="@Collections"
@bind-Values="SelectedCollections"
LabelName="@nameof(Collection.Name)"
ValueName="@nameof(Collection.Id)"
Placeholder="Select a Collection"
DropdownRender="@DropdownRender" />
@code {
ICollection<Collection> Collections = new List<Collection>();
IEnumerable<Guid> SelectedCollections;
string NewCollectionName;
protected override async Task OnInitializedAsync()
{
await LoadData();
}
async Task LoadData()
{
using (var collectionService = DatabaseServiceFactory.Create<CollectionService>())
{
Collections = await collectionService.SortBy(c => c.Name).GetAsync();
}
}
RenderFragment DropdownRender(RenderFragment originNode)
{
RenderFragment customDropdownRender =
@<Template>
<div>
@originNode
<Divider Style="margin: 4px 0"></Divider>
<Space Direction="SpaceDirection.Horizontal" Style="padding: 4px 8px; width: 100%;">
<SpaceItem Style="flex-grow: 1;">
<Input Style="flex: auto;" @bind-Value="@NewCollectionName" BindOnInput="true" />
</SpaceItem>
<SpaceItem>
<Button Type="ButtonType.Primary" Disabled="@(String.IsNullOrWhiteSpace(NewCollectionName))" OnClick="AddCollection">Add New Collection</Button>
</SpaceItem>
</Space>
</div>
</Template>
;
return customDropdownRender;
}
async Task AddCollection(MouseEventArgs args)
{
try
{
if (!String.IsNullOrWhiteSpace(NewCollectionName))
{
using (var collectionService = DatabaseServiceFactory.Create<CollectionService>())
{
await collectionService.AddAsync(new Collection()
{
Name = NewCollectionName
});
}
await LoadData();
MessageService.Success("Collection added!");
}
}
catch (Exception ex)
{
MessageService.Error("Could not add a new collection!");
Logger.LogError(ex, "Could not add a new collection!");
}
}
public override async Task OnFeedbackOkAsync(ModalClosingEventArgs args)
{
ICollection<Collection> collections;
using (var collectionService = DatabaseServiceFactory.Create<CollectionService>())
{
collections = await collectionService.GetAsync(c => SelectedCollections.Contains(c.Id));
try
{
using (var gameService = DatabaseServiceFactory.Create<GameService>())
{
foreach (var collection in collections)
{
if (collection.Games == null)
collection.Games = new List<Game>();
foreach (var gameId in Options.GameIds.Where(gid => !collection.Games.Any(g => g.Id == gid)))
{
var game = await gameService.GetAsync(gameId);
collection.Games.Add(game);
}
await collectionService.UpdateAsync(collection);
}
}
if (collections.Count > 1)
MessageService.Success("Added to collections!");
else
MessageService.Success("Added to collection!");
}
catch (Exception ex)
{
if (collections.Count > 1)
MessageService.Error("Could not add to collections!");
else
MessageService.Error("Could not add to collection!");
Logger.LogError(ex, "Could not add to collection(s)!");
}
}
await base.OkCancelRefWithResult!.OnOk(collections);
}
}