1
0
mirror of synced 2024-12-18 01:15:55 +01:00

Add delete api

Add delete dialog
This commit is contained in:
asesidaa 2022-09-06 22:32:36 +08:00
parent 6814715ddb
commit 1097c3cd24
8 changed files with 181 additions and 66 deletions

View File

@ -1,4 +1,5 @@
namespace SharedProject.Enums; // ReSharper disable UnusedMember.Global
namespace SharedProject.Enums;
public enum RandomType public enum RandomType
{ {

View File

@ -0,0 +1,30 @@
namespace TaikoLocalServer.Controllers.Api;
[ApiController]
[Route("api/[controller]")]
public class CardsController : BaseController<CardsController>
{
private readonly TaikoDbContext context;
public CardsController(TaikoDbContext context)
{
this.context = context;
}
[HttpDelete("{accessCode}")]
public async Task<IActionResult> DeleteUser(string accessCode)
{
var card = await context.Cards.FindAsync(accessCode);
if (card is null)
{
return NotFound();
}
context.Cards.Remove(card);
await context.SaveChangesAsync();
return NoContent();
}
}

View File

@ -45,7 +45,7 @@ public class UserSettingsController : BaseController<UserSettingsController>
[HttpPost] [HttpPost]
public async Task<IActionResult> SaveUserSetting(uint baid, UserSetting userSetting) public async Task<IActionResult> SaveUserSetting(uint baid, UserSetting userSetting)
{ {
var user = await context.UserData.FirstOrDefaultAsync(datum => datum.Baid == baid); var user = await context.UserData.FindAsync(baid);
if (user is null) if (user is null)
{ {

View File

@ -22,7 +22,7 @@
<PackageReference Include="SharpZipLib" Version="1.3.3" /> <PackageReference Include="SharpZipLib" Version="1.3.3" />
<PackageReference Include="Swan.Core" Version="6.0.2-beta.90" /> <PackageReference Include="Swan.Core" Version="6.0.2-beta.90" />
<PackageReference Include="Swan.Logging" Version="6.0.2-beta.69" /> <PackageReference Include="Swan.Logging" Version="6.0.2-beta.69" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Throw" Version="1.3.0" /> <PackageReference Include="Throw" Version="1.3.0" />
<PackageReference Include="Yoh.Text.Json.NamingPolicies" Version="0.2.1" /> <PackageReference Include="Yoh.Text.Json.NamingPolicies" Version="0.2.1" />
</ItemGroup> </ItemGroup>

View File

@ -4,7 +4,7 @@
@using SharedProject.Models @using SharedProject.Models
@inject HttpClient Client @inject HttpClient Client
<MudBreadcrumbs Items="_breadcrumbs" Class="px-0"></MudBreadcrumbs> <MudBreadcrumbs Items="breadcrumbs" Class="px-0"></MudBreadcrumbs>
<h1>Card: @Baid</h1> <h1>Card: @Baid</h1>
@ -21,21 +21,21 @@
<MudTextField @bind-Value="@response.Title" Label="Title" Variant="Variant.Outlined"></MudTextField> <MudTextField @bind-Value="@response.Title" Label="Title" Variant="Variant.Outlined"></MudTextField>
<MudSelect @bind-Value="@response.IsDisplayAchievement" <MudSelect @bind-Value="@response.IsDisplayAchievement"
Label="Display Achievement Panel"> Label="Display Achievement Panel">
<MudSelectItem Value="@true">On</MudSelectItem> <MudSelectItem Value="@true">On</MudSelectItem>
<MudSelectItem Value="@false">Off</MudSelectItem> <MudSelectItem Value="@false">Off</MudSelectItem>
</MudSelect> </MudSelect>
<MudSelect @bind-Value="@response.IsDisplayDanOnNamePlate" <MudSelect @bind-Value="@response.IsDisplayDanOnNamePlate"
Label="Display Dan Rank on Name Plate"> Label="Display Dan Rank on Name Plate">
<MudSelectItem Value="@true">On</MudSelectItem> <MudSelectItem Value="@true">On</MudSelectItem>
<MudSelectItem Value="@false">Off</MudSelectItem> <MudSelectItem Value="@false">Off</MudSelectItem>
</MudSelect> </MudSelect>
<MudSelect @bind-Value="@response.AchievementDisplayDifficulty" <MudSelect @bind-Value="@response.AchievementDisplayDifficulty"
Label="Achievement Panel Difficulty"> Label="Achievement Panel Difficulty">
@foreach (var item in Enum.GetValues<Difficulty>()) @foreach (var item in Enum.GetValues<Difficulty>())
{ {
<MudSelectItem Value="@item" /> <MudSelectItem Value="@item"/>
} }
</MudSelect> </MudSelect>
</MudStack> </MudStack>
@ -47,13 +47,13 @@
<MudGrid> <MudGrid>
<MudItem xs="12" md="4"> <MudItem xs="12" md="4">
<MudStack Spacing="6"> <MudStack Spacing="6">
<MudSwitch @bind-Checked="@response.PlaySetting.IsVanishOn" Label="Vanish" Color="Color.Primary" /> <MudSwitch @bind-Checked="@response.PlaySetting.IsVanishOn" Label="Vanish" Color="Color.Primary"/>
<MudSwitch @bind-Checked="@response.PlaySetting.IsInverseOn" Label="Inverse" Color="Color.Primary" /> <MudSwitch @bind-Checked="@response.PlaySetting.IsInverseOn" Label="Inverse" Color="Color.Primary"/>
<MudSwitch @bind-Checked="@response.IsSkipOn" Label="Give Up" Color="Color.Primary" /> <MudSwitch @bind-Checked="@response.IsSkipOn" Label="Give Up" Color="Color.Primary"/>
<MudSwitch @bind-Checked="@response.IsVoiceOn" Label="Voice" Color="Color.Primary" /> <MudSwitch @bind-Checked="@response.IsVoiceOn" Label="Voice" Color="Color.Primary"/>
</MudStack> </MudStack>
</MudItem> </MudItem>
<MudItem xs="12" md="8"> <MudItem xs="12" md="8">
@ -67,10 +67,10 @@
</MudSelect> </MudSelect>
<MudSelect @bind-Value="@response.PlaySetting.RandomType" <MudSelect @bind-Value="@response.PlaySetting.RandomType"
Label="Random"> Label="Random">
@foreach (var item in Enum.GetValues<RandomType>()) @foreach (var item in Enum.GetValues<RandomType>())
{ {
<MudSelectItem Value="@item" /> <MudSelectItem Value="@item"/>
} }
</MudSelect> </MudSelect>
@ -86,12 +86,12 @@
<MudItem md="4" xs="12" Class="py-8 px-8 my-4 pt-8"> <MudItem md="4" xs="12" Class="py-8 px-8 my-4 pt-8">
<MudStack Spacing="6" Style="top:100px" Class="sticky"> <MudStack Spacing="6" Style="top:100px" Class="sticky">
<MudButton Disabled="@isSavingOptions" <MudButton Disabled="@isSavingOptions"
OnClick="SaveOptions" OnClick="SaveOptions"
Variant="Variant.Filled" Variant="Variant.Filled"
Color="Color.Primary"> Color="Color.Primary">
@if (isSavingOptions) @if (isSavingOptions)
{ {
<MudProgressCircular Class="ms-n1" Size="Size.Small" Indeterminate="true" /> <MudProgressCircular Class="ms-n1" Size="Size.Small" Indeterminate="true"/>
<MudText Class="ms-2">Saving...</MudText> <MudText Class="ms-2">Saving...</MudText>
} }
else else
@ -105,9 +105,8 @@
</MudGrid> </MudGrid>
<MudScrollToTop> <MudScrollToTop>
<MudFab Color="Color.Secondary" Icon="@Icons.Filled.ArrowCircleUp" /> <MudFab Color="Color.Secondary" Icon="@Icons.Filled.ArrowCircleUp"/>
</MudScrollToTop> </MudScrollToTop>
} }
@code { @code {
@ -119,12 +118,16 @@
private bool isSavingOptions; private bool isSavingOptions;
private readonly string[] speedStrings = { "1.0", "1.1", "1.2", "1.3", "1.4", "1.5", "1.6", "1.7", "1.8", "1.9", private readonly string[] speedStrings =
"2.0", "2.5", "3.0", "3.5", "4.0"}; {
"1.0", "1.1", "1.2", "1.3", "1.4",
"1.5", "1.6", "1.7", "1.8", "1.9",
"2.0", "2.5", "3.0", "3.5", "4.0"
};
private readonly string[] notePositionStrings = new string[] { "-5", "-4", "-3", "-2", "-1", "0", "+1", "+2", "+3", "+4", "+5"}; private readonly string[] notePositionStrings = { "-5", "-4", "-3", "-2", "-1", "0", "+1", "+2", "+3", "+4", "+5" };
private List<BreadcrumbItem> _breadcrumbs = new List<BreadcrumbItem> private List<BreadcrumbItem> breadcrumbs = new()
{ {
new BreadcrumbItem("Dashboard", href: "Dashboard"), new BreadcrumbItem("Dashboard", href: "Dashboard"),
}; };
@ -135,7 +138,7 @@
isSavingOptions = false; isSavingOptions = false;
response = await Client.GetFromJsonAsync<UserSetting>($"api/UserSettings/{Baid}"); response = await Client.GetFromJsonAsync<UserSetting>($"api/UserSettings/{Baid}");
_breadcrumbs.Add(new BreadcrumbItem($"Card: {Baid}", href: null, disabled: true)); breadcrumbs.Add(new BreadcrumbItem($"Card: {Baid}", href: null, disabled: true));
} }
private async Task SaveOptions() private async Task SaveOptions()
@ -143,5 +146,6 @@
isSavingOptions = true; isSavingOptions = true;
await Client.PostAsJsonAsync($"api/UserSettings/{Baid}", response); await Client.PostAsJsonAsync($"api/UserSettings/{Baid}", response);
isSavingOptions = false; isSavingOptions = false;
} }
} }

View File

@ -1,77 +1,87 @@
@using SharedProject.Models.Responses @using SharedProject.Models.Responses
@using TaikoWebUI.Pages.Dialogs
@using SharedProject.Models
@inject HttpClient Client @inject HttpClient Client
@inject NavigationManager UriHelper @inject NavigationManager UriHelper
@inject IDialogService DialogService
@page "/Dashboard" @page "/Dashboard"
<h1>Dashboard</h1> <h1>Dashboard</h1>
<MudGrid Class="my-8"> <MudGrid Class="my-8">
@if (response is null) @if (response is null)
{ {
@if (isLoading is true) { @for (uint i = 0; i < 3; i++)
@for (uint i = 0; i < 3; i++) { {
<MudItem xs="12" md="6" lg="4"> <MudItem xs="12" md="6" lg="4">
<MudCard Outlined="true"> <MudCard Outlined="true">
<MudCardContent> <MudCardContent>
<MudSkeleton Width="30%" Height="42px;" Class="mb-5" /> <MudSkeleton Width="30%" Height="42px;" Class="mb-5"/>
<MudSkeleton Width="80%" /> <MudSkeleton Width="80%"/>
<MudSkeleton Width="100%" /> <MudSkeleton Width="100%"/>
</MudCardContent> </MudCardContent>
<MudCardActions> <MudCardActions>
<MudStack Row="true" Style="width:100%" Spacing="4" Justify="Justify.FlexEnd"> <MudStack Row="true" Style="width:100%" Spacing="4" Justify="Justify.FlexEnd">
<MudSkeleton Width="64px" Height="40px" /> <MudSkeleton Width="64px" Height="40px"/>
<MudSkeleton Width="64px" Height="40px" /> <MudSkeleton Width="64px" Height="40px"/>
</MudStack> </MudStack>
</MudCardActions> </MudCardActions>
</MudCard> </MudCard>
</MudItem> </MudItem>
} }
} else { }
else if(response.Users.Count != 0)
{
@foreach (var user in response.Users)
{
<MudItem xs="12" md="6" lg="4">
<MudCard Outlined="true">
<MudCardHeader>
<CardHeaderContent>
<MudText Typo="Typo.h6" Style="font-weight:bold">@user.Baid</MudText>
</CardHeaderContent>
</MudCardHeader>
<MudCardContent>
<MudText Style="font-weight:bold">Access Code</MudText>
<MudText Style="font-family:monospace">@user.AccessCode</MudText>
</MudCardContent>
<MudCardActions>
<MudStack Row="true" Style="width:100%" Spacing="4" Justify="Justify.FlexEnd">
<MudButton Size="Size.Small" Variant="Variant.Text" StartIcon="@Icons.Material.Filled.Delete"
Color="Color.Error"
OnClick="_ => DeleteCard(user)">
Delete
</MudButton>
<MudButton OnClick="() => NavigateToProfile(user.Baid)"
Size="Size.Small" Variant="Variant.Text" StartIcon="@Icons.Material.Filled.Edit"
Color="Color.Primary">
Edit
</MudButton>
</MudStack>
</MudCardActions>
</MudCard>
</MudItem>
}
}
else
{
<MudItem xs="12"> <MudItem xs="12">
<MudText Align="Align.Center" Class="my-8"> <MudText Align="Align.Center" Class="my-8">
No data. No data.
</MudText> </MudText>
</MudItem> </MudItem>
} }
}
else
{
@foreach (var user in response.Users)
{
<MudItem xs="12" md="6" lg="4">
<MudCard Outlined="true">
<MudCardHeader>
<CardHeaderContent>
<MudText Typo="Typo.h6" Style="font-weight:bold">@user.Baid</MudText>
</CardHeaderContent>
</MudCardHeader>
<MudCardContent>
<MudText Style="font-weight:bold">Access Code</MudText>
<MudText Style="font-family:monospace">@user.AccessCode</MudText>
</MudCardContent>
<MudCardActions>
<MudStack Row="true" Style="width:100%" Spacing="4" Justify="Justify.FlexEnd">
<MudButton Size="Size.Small" Variant="Variant.Text" StartIcon="@Icons.Material.Filled.Delete" Color="Color.Error">Delete</MudButton>
<MudButton OnClick="() => NavigateToProfile(user.Baid)" Size="Size.Small" Variant="Variant.Text" StartIcon="@Icons.Material.Filled.Edit" Color="Color.Primary">Edit</MudButton>
</MudStack>
</MudCardActions>
</MudCard>
</MudItem>
}
}
</MudGrid> </MudGrid>
@code { @code {
private DashboardResponse? response; private DashboardResponse? response;
private bool isLoading = true;
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
await base.OnInitializedAsync(); await base.OnInitializedAsync();
response = await Client.GetFromJsonAsync<DashboardResponse>("api/Dashboard"); response = await Client.GetFromJsonAsync<DashboardResponse>("api/Dashboard");
isLoading = false;
} }
@ -79,4 +89,23 @@ else
{ {
UriHelper.NavigateTo($"/Card/{baid}"); UriHelper.NavigateTo($"/Card/{baid}");
} }
private async Task DeleteCard(User user)
{
var parameters = new DialogParameters
{
["user"] = user
};
var dialog = DialogService.Show<CardDeleteConfirmDialog>("Delete Card", parameters);
var result = await dialog.Result;
if (result.Cancelled)
{
return;
}
response = await Client.GetFromJsonAsync<DashboardResponse>("api/Dashboard");
}
} }

View File

@ -0,0 +1,51 @@
@using SharedProject.Models
@inject HttpClient Client
@inject ISnackbar Snackbar
<MudDialog>
<TitleContent>
<MudText Typo="Typo.h6">
<MudIcon Icon="@Icons.Material.Filled.DeleteForever" Class="mr-3 mb-n1"/>
Delete card?
</MudText>
</TitleContent>
<DialogContent>
<MudText>
Do you really want to delete the card?
All the related data will also be deleted and this process cannot be undone!
</MudText>
</DialogContent>
<DialogActions>
<MudButton OnClick="Cancel">Cancel</MudButton>
<MudButton Color="Color.Error" OnClick="DeleteCard">
<MudText>Delete Card</MudText>
</MudButton>
</DialogActions>
</MudDialog>
@code {
[CascadingParameter]
MudDialogInstance MudDialog { get; set; } = null!;
[Parameter]
public User User { get; set; } = new();
private void Cancel() => MudDialog.Cancel();
private async Task DeleteCard()
{
var responseMessage = await Client.DeleteAsync($"api/Cards/{User.AccessCode}");
if (!responseMessage.IsSuccessStatusCode)
{
Snackbar.Add("Something went wrong when deleting card!", Severity.Error);
MudDialog.Close(DialogResult.Ok(false));
return;
}
Snackbar.Add("Delete success!", Severity.Success);
MudDialog.Close(DialogResult.Ok(true));
}
}

View File

@ -9,7 +9,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.7" /> <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.7" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.7" PrivateAssets="all" /> <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.7" PrivateAssets="all" />
<PackageReference Include="MudBlazor" Version="6.0.13" /> <PackageReference Include="MudBlazor" Version="6.0.15" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>