Added Polls and locking raids if not enough users are signed up
needs testing
This commit is contained in:
parent
32af72a262
commit
ccb276a265
14 changed files with 289 additions and 8 deletions
113
Lieb/Data/PollService.cs
Normal file
113
Lieb/Data/PollService.cs
Normal file
|
@ -0,0 +1,113 @@
|
|||
using Lieb.Models.GuildWars2.Raid;
|
||||
using Lieb.Models.Poll;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Internal;
|
||||
|
||||
namespace Lieb.Data
|
||||
{
|
||||
public class PollService
|
||||
{
|
||||
private readonly IDbContextFactory<LiebContext> _contextFactory;
|
||||
private readonly DiscordService _discordService;
|
||||
public PollService(IDbContextFactory<LiebContext> contextFactory, DiscordService discordService)
|
||||
{
|
||||
_contextFactory = contextFactory;
|
||||
_discordService = discordService;
|
||||
}
|
||||
|
||||
public Poll GetPoll(int pollId)
|
||||
{
|
||||
using var context = _contextFactory.CreateDbContext();
|
||||
Poll? poll = context.Polls
|
||||
.Include(p => p.Options)
|
||||
.Include(p => p.Answers)
|
||||
.FirstOrDefault(p => p.PollId == pollId);
|
||||
|
||||
if (poll != null) return poll;
|
||||
|
||||
return new Poll();
|
||||
}
|
||||
|
||||
public List<Poll> GetPollsByRaidId(int raidId)
|
||||
{
|
||||
using var context = _contextFactory.CreateDbContext();
|
||||
return context.Polls
|
||||
.Include(p => p.Options)
|
||||
.Include(p => p.Answers)
|
||||
.Where(p => p.RaidId == raidId).ToList();
|
||||
}
|
||||
|
||||
public async Task<int> CreatePoll(string question, List<string> options, int raidId)
|
||||
{
|
||||
using var context = _contextFactory.CreateDbContext();
|
||||
Raid? raid = context.Raids
|
||||
.Include(r => r.SignUps)
|
||||
.FirstOrDefault(r => r.RaidId == raidId);
|
||||
|
||||
if (raid == null) return 0;
|
||||
List<ulong> users = raid.SignUps.Where(s => s.LiebUserId != null).Select(s => (ulong)s.LiebUserId).ToList();
|
||||
return await CreatePoll(question, options, users, raidId);
|
||||
}
|
||||
|
||||
public async Task<int> CreatePoll(string question, List<string> options, List<ulong> users, int? raidId = null)
|
||||
{
|
||||
Poll poll = new Poll()
|
||||
{
|
||||
Question = question,
|
||||
RaidId = raidId
|
||||
};
|
||||
foreach(string option in options)
|
||||
{
|
||||
poll.Options.Add(new PollOption()
|
||||
{
|
||||
Name = option
|
||||
});
|
||||
}
|
||||
foreach(ulong user in users)
|
||||
{
|
||||
poll.Answers.Add(new PollAnswer()
|
||||
{
|
||||
UserId = user
|
||||
});
|
||||
}
|
||||
|
||||
using var context = _contextFactory.CreateDbContext();
|
||||
context.Polls.Add(poll);
|
||||
await context.SaveChangesAsync();
|
||||
return poll.PollId;
|
||||
}
|
||||
|
||||
public async Task DeletePoll(int pollId)
|
||||
{
|
||||
using var context = _contextFactory.CreateDbContext();
|
||||
Poll? poll = context.Polls
|
||||
.Include(p => p.Options)
|
||||
.Include(p => p.Answers)
|
||||
.FirstOrDefault(p => p.PollId == pollId);
|
||||
|
||||
if(poll == null) return;
|
||||
|
||||
context.PollOptions.RemoveRange(poll.Options);
|
||||
context.PollAnswers.RemoveRange(poll.Answers);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
poll.Options.Clear();
|
||||
poll.Answers.Clear();
|
||||
context.Polls.Remove(poll);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task UpdateAnswer(int pollId, int pollOptionId, ulong userId)
|
||||
{
|
||||
using var context = _contextFactory.CreateDbContext();
|
||||
Poll? poll = context.Polls
|
||||
.Include(p => p.Answers)
|
||||
.FirstOrDefault(p => p.PollId == pollId && p.Answers.Where(a => a.UserId == userId).Any());
|
||||
|
||||
if (poll == null) return;
|
||||
|
||||
PollAnswer answer = poll.Answers.First(a => a.UserId == userId);
|
||||
answer.PollOptionId = pollOptionId;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue