Added account management sites
This commit is contained in:
parent
c8252daf88
commit
7113e3abee
10 changed files with 332 additions and 64 deletions
|
@ -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<Equipped> toDelete = new List<Equipped>();
|
||||
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<List<GuildWars2Build>> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -76,7 +76,8 @@ namespace Lieb.Data
|
|||
raidToChange.DiscordChannelId = raid.DiscordChannelId;
|
||||
raidToChange.DiscordGuildId = raid.DiscordGuildId;
|
||||
|
||||
foreach(PlannedRaidRole role in raidToChange.Roles)
|
||||
List<PlannedRaidRole> rolesToRemove = new List<PlannedRaidRole>();
|
||||
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<RaidReminder> reminderToRemove = new List<RaidReminder>();
|
||||
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);
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue