Implemented importing and exporting of stars
This commit is contained in:
@@ -6,20 +6,32 @@
|
||||
@using MoonCore.Blazor.Tailwind.DataTable
|
||||
@using MoonlightServers.Shared.Http.Responses.Admin.Stars
|
||||
@using MoonCore.Blazor.Tailwind.Components
|
||||
@using MoonCore.Blazor.Tailwind.Toasts
|
||||
@using MoonCore.Exceptions
|
||||
@using Moonlight.Client.Services
|
||||
|
||||
@inject HttpApiClient ApiClient
|
||||
@inject DownloadService DownloadService
|
||||
@inject ToastService ToastService
|
||||
|
||||
<div class="mb-3">
|
||||
<NavTabs Index="3" Names="@UiConstants.AdminNavNames" Links="@UiConstants.AdminNavLinks"/>
|
||||
</div>
|
||||
|
||||
<MinimalCrud TItem="StarDetailResponse" OnConfigure="OnConfigure">
|
||||
<MinimalCrud @ref="Crud" TItem="StarDetailResponse" OnConfigure="OnConfigure">
|
||||
<ChildContent>
|
||||
<DataColumn TItem="StarDetailResponse" Field="@(x => x.Id)" Title="Id" IsSortable="true"/>
|
||||
<DataColumn TItem="StarDetailResponse" Field="@(x => x.Name)" Title="Name" IsSortable="true"/>
|
||||
<DataColumn TItem="StarDetailResponse" Field="@(x => x.Version)" Title="Version" IsSortable="true"/>
|
||||
<DataColumn TItem="StarDetailResponse" Field="@(x => x.Author)" Title="Author"/>
|
||||
</ChildContent>
|
||||
<Actions>
|
||||
<InputFile id="import-file" hidden="" multiple OnChange="OnImportFiles"/>
|
||||
<label for="import-file" class="btn btn-tertiary cursor-pointer">
|
||||
<i class="icon-file-up mr-2"></i>
|
||||
Import
|
||||
</label>
|
||||
</Actions>
|
||||
<ItemActions>
|
||||
@if (!string.IsNullOrEmpty(context.DonateUrl))
|
||||
{
|
||||
@@ -36,11 +48,18 @@
|
||||
<span class="align-middle">Update</span>
|
||||
</a>
|
||||
}
|
||||
|
||||
<a href="#" @onclick="() => Export(context)" @onclick:preventDefault class="text-success-500 mr-3">
|
||||
<i class="icon-download align-middle"></i>
|
||||
<span class="align-middle">Export</span>
|
||||
</a>
|
||||
</ItemActions>
|
||||
</MinimalCrud>
|
||||
|
||||
@code
|
||||
{
|
||||
private MinimalCrud<StarDetailResponse> Crud;
|
||||
|
||||
private void OnConfigure(MinimalCrudOptions<StarDetailResponse> options)
|
||||
{
|
||||
options.Title = "Stars";
|
||||
@@ -51,4 +70,51 @@
|
||||
options.UpdateUrl = item => ComponentHelper.GetRouteOfComponent<Update>(item.Id)!;
|
||||
options.DeleteFunction = async item => await ApiClient.Delete($"api/admin/servers/stars/{item.Id}");
|
||||
}
|
||||
|
||||
private async Task Export(StarDetailResponse star)
|
||||
{
|
||||
var json = await ApiClient.GetString($"api/admin/servers/stars/{star.Id}/export");
|
||||
|
||||
var formattedFileName = star.Name.Replace(" ", "_") + ".json";
|
||||
|
||||
await DownloadService.DownloadString(formattedFileName, json);
|
||||
await ToastService.Success($"Successfully exported '{star.Name}'");
|
||||
}
|
||||
|
||||
private async Task OnImportFiles(InputFileChangeEventArgs eventArgs)
|
||||
{
|
||||
IBrowserFile[] files;
|
||||
|
||||
if(eventArgs.FileCount == 0)
|
||||
return;
|
||||
else if (eventArgs.FileCount > 1)
|
||||
files = eventArgs.GetMultipleFiles().ToArray();
|
||||
else
|
||||
files = [eventArgs.File];
|
||||
|
||||
foreach (var file in files)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!file.Name.EndsWith(".json"))
|
||||
{
|
||||
await ToastService.Danger($"Failed to import '{file.Name}': Only json files are supported");
|
||||
continue;
|
||||
}
|
||||
|
||||
await using var stream = file.OpenReadStream();
|
||||
var content = new MultipartFormDataContent();
|
||||
content.Add(new StreamContent(stream), "file", file.Name);
|
||||
|
||||
var star = await ApiClient.PostJson<StarDetailResponse>("api/admin/servers/stars/import", content);
|
||||
|
||||
await ToastService.Success($"Successfully imported '{star.Name}'");
|
||||
await Crud.Refresh(isSilent: false, bypassCache: true);
|
||||
}
|
||||
catch (HttpApiException e)
|
||||
{
|
||||
await ToastService.Danger($"Failed to import '{file.Name}': {e.Title}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user