Added delete user button

This commit is contained in:
Sarah Faey 2022-12-01 21:42:43 +01:00
parent ce405a3e36
commit 26dd595057
2 changed files with 38 additions and 1 deletions

View file

@ -9,11 +9,13 @@ namespace Lieb.Data
{
private readonly IDbContextFactory<LiebContext> _contextFactory;
private readonly DiscordService _discordService;
private readonly GuildWars2AccountService _guildWars2AccountService;
public UserService(IDbContextFactory<LiebContext> contextFactory, DiscordService discordService)
public UserService(IDbContextFactory<LiebContext> contextFactory, DiscordService discordService, GuildWars2AccountService guildWars2AccountService)
{
_contextFactory = contextFactory;
_discordService = discordService;
_guildWars2AccountService = guildWars2AccountService;
}
public List<LiebUser> GetLiebUsers()
@ -101,6 +103,21 @@ namespace Lieb.Data
await _discordService.RenameUser(user.Id, user.Name, GetMainAccount(user.Id).AccountName);
}
public async Task DeleteUser(LiebUser user)
{
using var context = _contextFactory.CreateDbContext();
foreach(GuildWars2Account account in user.GuildWars2Accounts)
{
await _guildWars2AccountService.DeleteAccount(account.GuildWars2AccountId);
}
context.RaidLogs.RemoveRange(context.RaidLogs.Where(r => r.UserId == user.Id).ToList());
context.RaidSignUps.RemoveRange(context.RaidSignUps.Where(r => r.LiebUserId == user.Id));
context.RoleAssignments.RemoveRange(context.RoleAssignments.Where(r => r.LiebUserId == user.Id));
context.Remove(user);
await context.SaveChangesAsync();
}
public async Task UpdateBannedUntil(ulong userId, DateTime? date)
{
using var context = _contextFactory.CreateDbContext();

View file

@ -7,6 +7,7 @@
@inject UserService UserService
@inject NavigationManager NavigationManager
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject IJSRuntime JsRuntime
<h3>Manage Account</h3>
@ -71,6 +72,12 @@
<br />
<button type="submit">Save</button>
<ValidationSummary />
<br/>
<br/>
<br/>
<br/>
<br/>
<a href="Account/Logout" class="loginText" @onclick="() => DeleteAccountClicked()">Delete Account</a>
</EditForm>
</Authorized>
</AuthorizeView>
@ -99,4 +106,17 @@
await UserService.EditUser(_user);
_saveMessage = "changes saved successfully";
}
async Task DeleteAccountClicked()
{
bool confirmed = await JsRuntime.InvokeAsync<bool>("confirm", "Are you sure you want to delete this Website Account?\nThis will sign you off in every raid in which you are signed up.");
if (confirmed)
{
await UserService.DeleteUser(_user);
}
else
{
NavigationManager.NavigateTo("accountedit");
}
}
}