added RaidDetails.razor + needed services

This commit is contained in:
t.ruspekhofer 2022-02-24 23:29:31 +01:00
parent 6da9b06e8a
commit b18cc58665
15 changed files with 597 additions and 18 deletions

View file

@ -1,4 +1,6 @@
using Lieb.Models;
using Lieb.Models.GuildWars2;
using Lieb.Models.GuildWars2.Raid;
using Microsoft.EntityFrameworkCore;
namespace Lieb.Data
@ -29,12 +31,16 @@ namespace Lieb.Data
return; // DB has been seeded
}
GuildWars2Account linaith = new GuildWars2Account() { AccountName = "Linaith.2375" };
GuildWars2Account sarah = new GuildWars2Account() { AccountName = "Sarah.3984" };
GuildWars2Account hierpiepts = new GuildWars2Account() { AccountName = "hierpiepts.5241" };
GuildWars2Account bloodseeker = new GuildWars2Account() { AccountName = "Bloodseeker.2043" };
var users = new LiebUser[]
{
new LiebUser{DiscordUserId=194863625477816321, Name="Sarah", Birthday=DateTime.Parse("1992-01-15")},
new LiebUser{DiscordUserId=1, Name="Lisa"},
new LiebUser{DiscordUserId=2, Name="Simon"}
new LiebUser{DiscordUserId=194863625477816321, Name="Sarah", Birthday=DateTime.Parse("1992-01-15"), GuildWars2Accounts = new List<GuildWars2Account>(){ linaith} },
new LiebUser{DiscordUserId=1, Name="Lisa", GuildWars2Accounts = new List<GuildWars2Account>(){ hierpiepts}},
new LiebUser{DiscordUserId=2, Name="Simon", GuildWars2Accounts = new List<GuildWars2Account>(){ bloodseeker}}
};
context.LiebUsers.AddRange(users);
@ -63,6 +69,45 @@ namespace Lieb.Data
context.RoleAssignments.AddRange(assignments);
context.SaveChanges();
PlannedRaidRole ele = new PlannedRaidRole()
{
Description = "Beste",
Name = "Heal Ele",
Spots = 2
};
PlannedRaidRole scourge = new PlannedRaidRole()
{
Description = "WupWup",
Name = "Scourge",
Spots = 8
};
Raid raid = new Raid()
{
Title = "Testraid",
Description = "This is a test raid\nwith multiple lines?",
Guild = "LIEB",
Organizer = "Sarah",
RaidDuration = 2,
RaidType = RaidType.RandomClasses,
StartTime = DateTime.Now,
VoiceChat = "ts.lieb.games",
Roles = new [] { ele, scourge}
};
context.Raids.Add(raid);
context.SaveChanges();
var signUps = new RaidSignUp[]
{
new RaidSignUp{GuildWars2AccountId = linaith.GuildWars2AccountId, LiebUserId = users[0].LiebUserId, PlannedRaidRoleId = ele.PlannedRaidRoleId, RaidId = raid.RaidId, SignUpType = SignUpType.SignedUp },
new RaidSignUp{GuildWars2AccountId = hierpiepts.GuildWars2AccountId, LiebUserId = users[1].LiebUserId, PlannedRaidRoleId = scourge.PlannedRaidRoleId, RaidId = raid.RaidId, SignUpType = SignUpType.SignedUp },
new RaidSignUp{GuildWars2AccountId = bloodseeker.GuildWars2AccountId, LiebUserId = users[2].LiebUserId, PlannedRaidRoleId = scourge.PlannedRaidRoleId, RaidId = raid.RaidId, SignUpType = SignUpType.Maybe }
};
context.RaidSignUps.AddRange(signUps);
context.SaveChanges();
}
}
}

View file

@ -0,0 +1,84 @@
using Lieb.Models;
using Lieb.Models.GuildWars2;
using Microsoft.EntityFrameworkCore;
namespace Lieb.Data
{
public class GuildWars2AccountService
{
private readonly IDbContextFactory<LiebContext> _contextFactory;
public GuildWars2AccountService(IDbContextFactory<LiebContext> contextFactory)
{
_contextFactory = contextFactory;
}
public async Task AddAccount(GuildWars2Account guildWars2Account, ulong discordId)
{
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();
}
}
public async Task UpdateAccount(int guildWars2AccountId, string accountName, string apiKey)
{
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))
{
account.AccountName = accountName;
}
await context.SaveChangesAsync();
}
}
public async Task RemoveAccount()
{
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();
}
}
}

View file

@ -16,9 +16,9 @@ namespace Lieb.Data
public DbSet<LiebUser> LiebUsers { get; set; }
public DbSet<RoleAssignment> RoleAssignments { get; set; }
public DbSet<LiebRole> LiebRoles { get; set; }
public DbSet<GuildWars2Account> GuildWars2Account { get; set; }
public DbSet<GuildWars2Account> GuildWars2Accounts { get; set; }
public DbSet<Equipped> Equipped { get; set; }
public DbSet<GuildWars2Build> RaidRoles { get; set; }
public DbSet<GuildWars2Build> GuildWars2Builds { get; set; }
public DbSet<PlannedRaidRole> PlannedRaidRoles { get; set; }
public DbSet<Raid> Raids { get; set; }
public DbSet<RaidReminder> RaidReminders { get; set; }

143
Lieb/Data/RaidService.cs Normal file
View file

@ -0,0 +1,143 @@
using Lieb.Models.GuildWars2.Raid;
using Microsoft.EntityFrameworkCore;
namespace Lieb.Data
{
public class RaidService
{
private readonly IDbContextFactory<LiebContext> _contextFactory;
public RaidService(IDbContextFactory<LiebContext> contextFactory)
{
_contextFactory = contextFactory;
}
public List<Raid> GetRaids()
{
using var context = _contextFactory.CreateDbContext();
return context.Raids
.Include(r => r.Roles)
.Include(r => r.SignUpHistory)
.Include(r => r.Reminders)
.Include(r => r.SignUps)
.ThenInclude(s => s.LiebUser)
.Include(r => r.SignUps)
.ThenInclude(s => s.GuildWars2Account)
.Include(r => r.SignUps)
.ThenInclude(s => s.PlannedRaidRole)
.ToList();
}
public Raid GetRaid(int raidId)
{
using var context = _contextFactory.CreateDbContext();
return context.Raids
.Include(r => r.Roles)
.Include(r => r.SignUpHistory)
.Include(r => r.Reminders)
.Include(r => r.SignUps)
.ThenInclude(s => s.LiebUser)
.Include(r => r.SignUps)
.ThenInclude(s => s.GuildWars2Account)
.Include(r => r.SignUps)
.ThenInclude(s => s.PlannedRaidRole)
.FirstOrDefault(r => r.RaidId == raidId);
}
public async Task CreateRaid(Raid raid)
{
if (raid == null)
{
using var context = _contextFactory.CreateDbContext();
context.Raids.Add(raid);
await context.SaveChangesAsync();
}
}
public async Task<List<PlannedRaidRole>> GetFreeRoles(int raidId)
{
using var context = _contextFactory.CreateDbContext();
Raid? raid = await context.Raids
.Include(r => r.Roles)
.Include(r => r.SignUps)
.FirstOrDefaultAsync(r => r.RaidId == raidId);
List<PlannedRaidRole> freeRoles = new List<PlannedRaidRole>();
if (raid != null)
{
List<int> plannedRolesIds = raid.SignUps.Select(s => s.PlannedRaidRoleId).ToList();
Dictionary<int, int> addedIds = new Dictionary<int, int>();
foreach (RaidSignUp signUp in raid.SignUps)
{
if (signUp.SignUpType == SignUpType.SignedUp || signUp.SignUpType == SignUpType.Maybe)
{
int id = signUp.PlannedRaidRoleId;
if (addedIds.ContainsKey(id))
{
addedIds[id] += 1;
}
else
{
addedIds.Add(id, 1);
}
}
}
foreach(PlannedRaidRole role in raid.Roles)
{
if(!addedIds.ContainsKey(role.PlannedRaidRoleId) || role.Spots > addedIds[role.PlannedRaidRoleId])
{
freeRoles.Add(role);
}
}
}
return freeRoles;
}
public async Task SignUp(int raidId, int liebUserId, int guildWars2AccountId, int plannedRoleId, SignUpType signUpType)
{
if ((await GetFreeRoles(raidId)).Where(r => r.PlannedRaidRoleId == plannedRoleId).Any())
{
using var context = _contextFactory.CreateDbContext();
context.RaidSignUps.Add(new RaidSignUp()
{
GuildWars2AccountId = guildWars2AccountId,
RaidId = raidId,
LiebUserId = liebUserId,
PlannedRaidRoleId = plannedRoleId,
SignUpType = signUpType
});
await context.SaveChangesAsync();
}
}
public async Task SignOff(int raidId, int liebUserId)
{
using var context = _contextFactory.CreateDbContext();
List<RaidSignUp> signUps = context.RaidSignUps.Where(x => x.RaidId == raidId && x.LiebUserId == liebUserId).ToList();
context.RaidSignUps.RemoveRange(signUps);
await context.SaveChangesAsync();
}
public async Task ChangeAccount(int raidId, int liebUserId, int guildWars2AccountId)
{
using var context = _contextFactory.CreateDbContext();
List<RaidSignUp> signUps = context.RaidSignUps.Where(x => x.RaidId == raidId && x.LiebUserId == liebUserId).ToList();
foreach(RaidSignUp signUp in signUps)
{
signUp.GuildWars2AccountId = guildWars2AccountId;
}
await context.SaveChangesAsync();
}
public async Task ChangeSignUpType(int raidId, int liebUserId, int plannedRoleId, SignUpType signUpType)
{
using var context = _contextFactory.CreateDbContext();
RaidSignUp signUp = await context.RaidSignUps.FirstOrDefaultAsync(x => x.RaidId == raidId && x.LiebUserId == liebUserId && x.SignUpType != SignUpType.SignedOff && x.SignUpType != SignUpType.Flex);
signUp.PlannedRaidRoleId = plannedRoleId;
signUp.SignUpType = signUpType;
await context.SaveChangesAsync();
}
}
}

56
Lieb/Data/UserService.cs Normal file
View file

@ -0,0 +1,56 @@
using Lieb.Models;
using Microsoft.EntityFrameworkCore;
namespace Lieb.Data
{
public class UserService
{
private readonly IDbContextFactory<LiebContext> _contextFactory;
public UserService(IDbContextFactory<LiebContext> contextFactory)
{
_contextFactory = contextFactory;
}
public async Task<LiebUser> GetLiebUser(ulong discordId)
{
if (discordId > 0)
{
using var context = _contextFactory.CreateDbContext();
return await context.LiebUsers
.Include(u => u.GuildWars2Accounts)
.ThenInclude(a => a.EquippedBuilds)
.ThenInclude(b => b.GuildWars2Build)
.Include(u => u.RoleAssignments)
.ThenInclude(r => r.LiebRole)
.FirstOrDefaultAsync(u => u.DiscordUserId == discordId);
}
else
return new LiebUser();
}
public LiebUser GetLiebUserSmall(ulong discordId)
{
if (discordId > 0)
{
using var context = _contextFactory.CreateDbContext();
return context.LiebUsers
.Include(u => u.GuildWars2Accounts)
.FirstOrDefault(u => u.DiscordUserId == discordId);
}
else
return new LiebUser();
}
public async Task<int> GetLiebUserId(ulong discordId)
{
if (discordId > 0)
{
using var context = _contextFactory.CreateDbContext();
return (await context.LiebUsers.FirstOrDefaultAsync(u => u.DiscordUserId == discordId)).LiebUserId;
}
else
return -1;
}
}
}