diff --git a/Lieb/Data/GuildWars2AccountService.cs b/Lieb/Data/GuildWars2AccountService.cs index 4985eb7..bab7dd3 100644 --- a/Lieb/Data/GuildWars2AccountService.cs +++ b/Lieb/Data/GuildWars2AccountService.cs @@ -13,72 +13,80 @@ namespace Lieb.Data _contextFactory = contextFactory; } - public async Task AddAccount(GuildWars2Account guildWars2Account, ulong discordId) + public GuildWars2Account GetAccount(int gw2AccountId) { using var context = _contextFactory.CreateDbContext(); - LiebUser liebUser = await context.LiebUsers.FirstOrDefaultAsync(u => u.DiscordUserId == discordId); - if (liebUser != null) - { - liebUser.GuildWars2Accounts.Add(guildWars2Account); - await context.SaveChangesAsync(); - } + return context.GuildWars2Accounts + .Include(a => a.EquippedBuilds) + .ThenInclude(e => e.GuildWars2Build) + .FirstOrDefault(a => a.GuildWars2AccountId == gw2AccountId); } - public async Task UpdateAccount(int guildWars2AccountId, string accountName, string apiKey) + public async Task AddOrEditAccount(GuildWars2Account account, int userId) { - using var context = _contextFactory.CreateDbContext(); - GuildWars2Account account = await context.GuildWars2Accounts.FirstOrDefaultAsync(u => u.GuildWars2AccountId == guildWars2AccountId); if (account != null) { - account.ApiKey = apiKey; - if (!string.IsNullOrEmpty(accountName)) + using var context = _contextFactory.CreateDbContext(); + if (account.GuildWars2AccountId == 0) { - account.AccountName = accountName; + //context.GuildWars2Accounts.Add(account); + LiebUser user = context.LiebUsers.FirstOrDefault(u => u.LiebUserId == userId); + if(user != null) + { + user.GuildWars2Accounts.Add(account); + } + await context.SaveChangesAsync(); + } + else + { + GuildWars2Account accountToChange = context.GuildWars2Accounts + .Include(a => a.EquippedBuilds) + .Include(e => e.EquippedBuilds) + .FirstOrDefault(a => a.GuildWars2AccountId == account.GuildWars2AccountId); + + accountToChange.AccountName = account.AccountName; + accountToChange.ApiKey = account.ApiKey; + + List toDelete = new List(); + foreach (Equipped equipped in accountToChange.EquippedBuilds) + { + Equipped? newEquipped = account.EquippedBuilds.FirstOrDefault(r => r.EquippedId == equipped.EquippedId); + if (newEquipped != null) + { + equipped.CanTank = newEquipped.CanTank; + } + else + { + toDelete.Add(equipped); + } + } + foreach(Equipped equipped in toDelete) + { + accountToChange.EquippedBuilds.Remove(equipped); + context.Equipped.Remove(equipped); + } + foreach (Equipped equipped in account.EquippedBuilds.Where(r => r.EquippedId == 0)) + { + accountToChange.EquippedBuilds.Add(equipped); + } + + await context.SaveChangesAsync(); } - await context.SaveChangesAsync(); } } - public async Task RemoveAccount() + + public async Task DeleteAccount(int accountId) { using var context = _contextFactory.CreateDbContext(); - - } - - public async Task AddBuildToAccount() - { - using var context = _contextFactory.CreateDbContext(); - - } - - public async Task RemoveBuildFromAccount() - { - using var context = _contextFactory.CreateDbContext(); - - } - - public async Task> GetBuilds() - { - using var context = _contextFactory.CreateDbContext(); - return context.GuildWars2Builds.ToList(); - } - - public async Task CreateBuild() - { - using var context = _contextFactory.CreateDbContext(); - await context.SaveChangesAsync(); - } - - public async Task UpdateBuild() - { - using var context = _contextFactory.CreateDbContext(); - await context.SaveChangesAsync(); - } - - public async Task DeleteBuild() - { - using var context = _contextFactory.CreateDbContext(); - await context.SaveChangesAsync(); + GuildWars2Account? account = await context.GuildWars2Accounts.FirstOrDefaultAsync(b => b.GuildWars2AccountId == accountId); + if (account != null) + { + context.Equipped.RemoveRange(account.EquippedBuilds); + await context.SaveChangesAsync(); + context.GuildWars2Accounts.Remove(account); + await context.SaveChangesAsync(); + } } } } diff --git a/Lieb/Data/RaidService.cs b/Lieb/Data/RaidService.cs index eb239d5..e222b13 100644 --- a/Lieb/Data/RaidService.cs +++ b/Lieb/Data/RaidService.cs @@ -76,7 +76,8 @@ namespace Lieb.Data raidToChange.DiscordChannelId = raid.DiscordChannelId; raidToChange.DiscordGuildId = raid.DiscordGuildId; - foreach(PlannedRaidRole role in raidToChange.Roles) + List rolesToRemove = new List(); + foreach (PlannedRaidRole role in raidToChange.Roles) { PlannedRaidRole? newRole = raid.Roles.FirstOrDefault(r => r.PlannedRaidRoleId == role.PlannedRaidRoleId); if(newRole != null) @@ -87,15 +88,20 @@ namespace Lieb.Data } else { - raidToChange.Roles.Remove(role); - context.PlannedRaidRoles.Remove(role); + rolesToRemove.Add(role); } } + foreach(PlannedRaidRole role in rolesToRemove) + { + raidToChange.Roles.Remove(role); + context.PlannedRaidRoles.Remove(role); + } foreach (PlannedRaidRole role in raid.Roles.Where(r => r.PlannedRaidRoleId == 0)) { raidToChange.Roles.Add(role); } + List reminderToRemove = new List(); foreach (RaidReminder reminder in raidToChange.Reminders) { RaidReminder? newReminder = raid.Reminders.FirstOrDefault(r => r.RaidReminderId == reminder.RaidReminderId); @@ -109,10 +115,14 @@ namespace Lieb.Data } else { - raidToChange.Reminders.Remove(reminder); - context.RaidReminders.Remove(reminder); + reminderToRemove.Add(reminder); } } + foreach(RaidReminder reminder in reminderToRemove) + { + raidToChange.Reminders.Remove(reminder); + context.RaidReminders.Remove(reminder); + } foreach (PlannedRaidRole role in raid.Roles.Where(r => r.PlannedRaidRoleId == 0)) { raidToChange.Roles.Add(role); diff --git a/Lieb/Data/UserService.cs b/Lieb/Data/UserService.cs index 1a595a8..bd4b8d5 100644 --- a/Lieb/Data/UserService.cs +++ b/Lieb/Data/UserService.cs @@ -1,4 +1,5 @@ using Lieb.Models; +using Lieb.Models.GuildWars2; using Microsoft.EntityFrameworkCore; namespace Lieb.Data @@ -52,5 +53,21 @@ namespace Lieb.Data else return -1; } + + public async Task EditUser(LiebUser user) + { + using var context = _contextFactory.CreateDbContext(); + LiebUser? userToChange = context.LiebUsers + .Include(u => u.GuildWars2Accounts) + .FirstOrDefault(u => u.LiebUserId == user.LiebUserId); + + if(userToChange != null) + { + userToChange.Name = user.Name; + userToChange.Pronouns = user.Pronouns; + userToChange.Birthday = user.Birthday; + } + await context.SaveChangesAsync(); + } } } diff --git a/Lieb/Models/GuildWars2/GuildWars2Account.cs b/Lieb/Models/GuildWars2/GuildWars2Account.cs index ff8e0a1..7db88e4 100644 --- a/Lieb/Models/GuildWars2/GuildWars2Account.cs +++ b/Lieb/Models/GuildWars2/GuildWars2Account.cs @@ -1,9 +1,13 @@ -namespace Lieb.Models.GuildWars2 +using System.ComponentModel.DataAnnotations; + +namespace Lieb.Models.GuildWars2 { public class GuildWars2Account { public int GuildWars2AccountId { get; set; } public string ApiKey { get; set; } = string.Empty; + [Required] + [RegularExpression("^[a-zA-z ]{3,27}\\.[0-9]{4}$", ErrorMessage = "Invalid Account Name")] public string AccountName { get; set; } = string.Empty; public ICollection EquippedBuilds { get; set; } = new List(); diff --git a/Lieb/Pages/GuildWars2/BuildEdit.razor b/Lieb/Pages/GuildWars2/BuildEdit.razor index a15a0ae..77c2301 100644 --- a/Lieb/Pages/GuildWars2/BuildEdit.razor +++ b/Lieb/Pages/GuildWars2/BuildEdit.razor @@ -98,7 +98,7 @@ async Task DeleteBuildClicked() { - bool confirmed = await JsRuntime.InvokeAsync("confirm", "Are you sure?"); + bool confirmed = await JsRuntime.InvokeAsync("confirm", "Are you sure you want to delete this Build?"); if (confirmed) { await GuildWars2BuildService.DeleteBuild(_build.GuildWars2BuildId); diff --git a/Lieb/Pages/Raids/RaidDetails.razor b/Lieb/Pages/Raids/RaidDetails.razor index 3dd96b7..3451775 100644 --- a/Lieb/Pages/Raids/RaidDetails.razor +++ b/Lieb/Pages/Raids/RaidDetails.razor @@ -140,10 +140,10 @@ + + Edit + + diff --git a/Lieb/Pages/Raids/RaidEdit.razor b/Lieb/Pages/Raids/RaidEdit.razor index a1896db..264be1f 100644 --- a/Lieb/Pages/Raids/RaidEdit.razor +++ b/Lieb/Pages/Raids/RaidEdit.razor @@ -150,7 +150,7 @@ async Task DeleteRaidClicked() { - bool confirmed = await JsRuntime.InvokeAsync("confirm", "Are you sure?"); + bool confirmed = await JsRuntime.InvokeAsync("confirm", "Are you sure you want to delete the raid?"); if (confirmed) { await RaidService.DeleteRaid(_raid.RaidId); @@ -171,5 +171,6 @@ } await RaidService.AddOrEditRaid(_raid); + NavigationManager.NavigateTo("raidoverview"); } } \ No newline at end of file diff --git a/Lieb/Pages/User/ManageAccount.razor b/Lieb/Pages/User/ManageAccount.razor new file mode 100644 index 0000000..757d33a --- /dev/null +++ b/Lieb/Pages/User/ManageAccount.razor @@ -0,0 +1,78 @@ +@page "/accountedit" +@using Lieb.Data +@using Lieb.Models +@using Lieb.Models.GuildWars2 +@using System.ComponentModel.DataAnnotations +@using System.Security.Claims +@inject UserService UserService +@inject NavigationManager NavigationManager +@inject AuthenticationStateProvider AuthenticationStateProvider + +

ManageAccount

+ + + + + + + +

+ +

+

+ +

+ +

+ +

+ + + + @foreach(GuildWars2Account account in _user.GuildWars2Accounts) + { + + } + +
+ + +
+
+
+ + +@code { + + public LiebUser _user; + + protected override async Task OnInitializedAsync() + { + var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); + + ulong discordId = ulong.Parse(authState.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value); + _user = UserService.GetLiebUserSmall(discordId); + } + + + private async Task HandleValidSubmit() + { + await UserService.EditUser(_user); + } +} \ No newline at end of file diff --git a/Lieb/Pages/User/ManageGuildWars2Account.razor b/Lieb/Pages/User/ManageGuildWars2Account.razor new file mode 100644 index 0000000..e61005b --- /dev/null +++ b/Lieb/Pages/User/ManageGuildWars2Account.razor @@ -0,0 +1,145 @@ +@page "/gw2accountedit" +@page "/gw2accountedit/{gw2Id}" +@using Lieb.Data +@using Lieb.Models +@using Lieb.Models.GuildWars2 +@using System.ComponentModel.DataAnnotations +@using System.Security.Claims +@inject GuildWars2AccountService GuildWars2AccountService +@inject GuildWars2BuildService GuildWars2BuildService +@inject UserService UserService +@inject NavigationManager NavigationManager +@inject AuthenticationStateProvider AuthenticationStateProvider +@inject IJSRuntime JsRuntime + +

ManageGuildWars2Account

+ + + + + + + +

+ +

+

+ +

+ + + + + + + + + + + + @foreach (Equipped equippedBuild in _account.EquippedBuilds) + { + + + + + + } +
can TankBuild
@equippedBuild.GuildWars2Build.BuildName
+ +

+ + @foreach (GuildWars2Build build in GuildWars2BuildService.GetBuilds()) + { + if(!_account.EquippedBuilds.Where(e => e.GuildWars2BuildId == build.GuildWars2BuildId).Any()) + { +

@build.BuildName
+ } + } +

+ + +
+ + +
+
+ +
+
+ + +@code { + + [Parameter] + public string gw2Id { get; set; } + + public GuildWars2Account _account; + + private LiebUser _user; + + protected override async Task OnInitializedAsync() + { + var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); + ulong discordId = ulong.Parse(authState.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value); + _user = UserService.GetLiebUserSmall(discordId); + + if(!string.IsNullOrEmpty(gw2Id) && int.TryParse(gw2Id, out int parsedId) && _user.GuildWars2Accounts.Where(a => a.GuildWars2AccountId == parsedId).Any()) + { + _account = GuildWars2AccountService.GetAccount(parsedId); + } + else + { + _account = new GuildWars2Account(); + } + } + + async Task AddBuildlicked(GuildWars2Build build) + { + Equipped equipped = new Equipped() + { + GuildWars2AccountId = _account.GuildWars2AccountId, + GuildWars2Account = _account, + GuildWars2BuildId = build.GuildWars2BuildId, + GuildWars2Build = build + }; + _account.EquippedBuilds.Add(equipped); + } + + + async Task RemoveBuildClicked(int buildId) + { + Equipped equipped = _account.EquippedBuilds.FirstOrDefault(e => e.GuildWars2BuildId == buildId); + if (equipped != null) + { + _account.EquippedBuilds.Remove(equipped); + } + } + + async Task TankingStatusChanged(Equipped equipped, ChangeEventArgs args) + { + equipped.CanTank =bool.Parse(args.Value.ToString()); + } + + async Task DeleteAccountClicked() + { + bool confirmed = await JsRuntime.InvokeAsync("confirm", "Are you sure you want to delete this Account?\nThis will sign you off in every raid in which you are signed up with this account."); + if (confirmed) + { + await GuildWars2AccountService.DeleteAccount(_account.GuildWars2AccountId); + NavigationManager.NavigateTo("accountedit"); + } + } + + private async Task HandleValidSubmit() + { + await GuildWars2AccountService.AddOrEditAccount(_account, _user.LiebUserId); + NavigationManager.NavigateTo("accountedit"); + } +} \ No newline at end of file diff --git a/Lieb/Shared/NavMenu.razor b/Lieb/Shared/NavMenu.razor index 7937a6c..db34bb5 100644 --- a/Lieb/Shared/NavMenu.razor +++ b/Lieb/Shared/NavMenu.razor @@ -24,6 +24,11 @@ Build Overview +