added poll pages

This commit is contained in:
Sarah Faey 2023-01-14 21:04:01 +01:00
parent 6d6e720695
commit 1158aff6df
8 changed files with 340 additions and 14 deletions

View file

@ -15,6 +15,15 @@ namespace Lieb.Data
_discordService = discordService;
}
public List<Poll> GetPolls()
{
using var context = _contextFactory.CreateDbContext();
return context.Polls
.Include(p => p.Options)
.Include(p => p.Answers)
.ToList();
}
public Poll GetPoll(int pollId)
{
using var context = _contextFactory.CreateDbContext();
@ -38,23 +47,13 @@ namespace Lieb.Data
}
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
RaidId = raidId,
AllowCustomAnswer = false,
AnswerType = AnswerType.Buttons
};
foreach(string option in options)
{
@ -63,6 +62,25 @@ namespace Lieb.Data
Name = option
});
}
return await CreatePoll(poll, raidId);
}
public async Task<int> CreatePoll(Poll poll, 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(poll, users, raidId);
}
public async Task<int> CreatePoll(Poll poll, List<ulong> users, int? raidId = null)
{
poll.RaidId = raidId;
foreach(ulong user in users)
{
poll.Answers.Add(new PollAnswer()

View file

@ -357,5 +357,15 @@ namespace Lieb.Data
await context.SaveChangesAsync();
return true;
}
public List<LiebUser> GetGroupMembers(int roleId)
{
using var context = _contextFactory.CreateDbContext();
return context.LiebUsers
.Include(u => u.RoleAssignments)
.ThenInclude(r => r.LiebRole)
.Where(u => u.RoleAssignments.Where(r => r.LiebRole.LiebRoleId == roleId).Any())
.ToList();
}
}
}

View file

@ -3,6 +3,6 @@
public class PollOption
{
public int PollOptionId { get; set; }
public string Name { get; set;}
public string Name { get; set;} = string.Empty;
}
}

View file

@ -0,0 +1,179 @@
@page "/pollcreate"
@using Lieb.Data
@using Lieb.Models
@using Lieb.Models.Poll
@using Lieb.Models.GuildWars2.Raid
@using System.ComponentModel.DataAnnotations
@using System.Security.Claims
@using SharedClasses.SharedModels
@inject RaidService RaidService
@inject UserService UserService
@inject PollService PollService
@inject NavigationManager NavigationManager
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject IJSRuntime JsRuntime
<h3>Create Poll</h3>
<AuthorizeView Policy="@Constants.Roles.RaidLead.Name" Context="authorizationContext">
<EditForm Model="@_poll" OnValidSubmit="@HandleValidSubmit">
<DataAnnotationsValidator />
<p>
<label>
Question:
<InputText @bind-Value="_poll.Question" />
</label>
</p>
<p>
<label>
Answer Type:
<InputSelect @bind-Value="_poll.AnswerType">
<option value="@AnswerType.Buttons">Buttons</option>
<option value="@AnswerType.Dropdown">Dropdown</option>
</InputSelect>
</label>
</p>
<p>
<label>
Poll Type:
<InputSelect @bind-Value="_pollType">
<option value="@POLL_TYPE_RAID">Raid</option>
<option value="@POLL_TYPE_GROUP">Group</option>
</InputSelect>
</label>
</p>
@if(_pollType == POLL_TYPE_RAID)
{
<p>
<label>
Raid:
<InputSelect @bind-Value="_chosenRaid">
@foreach (Raid raid in _raids.OrderByDescending(r => r.StartTimeUTC))
{
string name = $"{raid.Title} - {raid.StartTimeUTC.DateTime.ToLongDateString()}";
<option value="@raid.RaidId">@name</option>
}
</InputSelect>
</label>
</p>
}
else if(_pollType == POLL_TYPE_GROUP)
{
<p>
<label>
Raid:
<InputSelect @bind-Value="_chosenRole">
@foreach (LiebRole group in _availabeGroups)
{
<option value="@group.LiebRoleId">@group.RoleName</option>
}
</InputSelect>
</label>
</p>
}
<p>
<label>
<InputCheckbox @bind-Value="_poll.AllowCustomAnswer" />
Allow custom answers
</label>
</p>
<p>
<label>
Options:
</label>
<button type=button @onclick="() => AddOptionClicked()">Add option</button>
<table>
<tr>
<th>Option</th>
</tr>
@foreach(PollOption option in _poll.Options)
{
<tr>
<td><InputText @bind-Value="option.Name" /></td>
<td><button type=button @onclick="() => DeleteOptionClicked(option)">Delete</button></td>
</tr>
}
</table>
</p>
<ValidationSummary />
<label class="validation-message" >@_errorMessage</label>
<button type="submit">Submit</button>
</EditForm>
</AuthorizeView>
@code {
public Poll _poll = new Poll();
private LiebUser _user;
private string _errorMessage = string.Empty;
private List<Raid> _raids = new List<Raid>();
private List<LiebRole> _availabeGroups = new List<LiebRole>();
private const string POLL_TYPE_RAID = "raid";
private const string POLL_TYPE_GROUP = "group";
private string _pollType = POLL_TYPE_RAID;
private int _chosenRaid;
private int _chosenRole;
protected override async Task OnInitializedAsync()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
if (authState != null)
{
ulong discordId = ulong.Parse(authState.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value);
_user = UserService.GetLiebUser(discordId);
}
if(_user == null)
{
NavigationManager.NavigateTo("");
}
_raids = RaidService.GetRaids();
_chosenRaid = _raids.OrderByDescending(r => r.StartTimeUTC).First().RaidId;
if(_user.RoleAssignments.Where(r => r.LiebRole.Level >= Constants.Roles.Admin.PowerLevel).Any())
{
_availabeGroups = UserService.GetLiebRoles().Where(u => u.Type == RoleType.UserDefinedRole).ToList();
}
else
{
_availabeGroups = UserService.GetUserRoles(_user.Id).Where(u => u.Type == RoleType.UserDefinedRole).ToList();
}
_chosenRole = _availabeGroups.First().LiebRoleId;
}
async Task AddOptionClicked()
{
_poll.Options.Add(new PollOption());
}
async Task DeleteOptionClicked(PollOption option)
{
_poll.Options.Remove(option);
}
private async Task HandleValidSubmit()
{
if(_pollType == POLL_TYPE_RAID)
{
await PollService.CreatePoll(_poll, _chosenRaid);
}
else if(_pollType == POLL_TYPE_GROUP)
{
await PollService.CreatePoll(_poll, UserService.GetGroupMembers(_chosenRole).Select(u => u.Id).ToList());
}
NavigationManager.NavigateTo("raidoverview");
}
}

View file

@ -0,0 +1,51 @@
@using System.Security.Claims
@using Lieb.Data
@using Lieb.Models
@using Lieb.Models.Poll
@using Lieb.Models.GuildWars2.Raid
@inject UserService UserService
@inject RaidService RaidService
@inject TimeZoneService TimeZoneService
@inject RaidRandomizerService RaidRandomizerService
@inject NavigationManager NavigationManager
<body>
<div @onclick="() => _isCollapsed = !_isCollapsed">
<h5>@_poll.Question</h5>
@if(_raid != null)
{
<p>@_raid.Title - @_raid.StartTimeUTC.DateTime.ToLongDateString()</p>
}
</div>
@if (!_isCollapsed)
{
@foreach(var answer in _poll.Answers.GroupBy(a => a.Answer))
{
<p>@answer.Key - @answer.Count() </p>
}
}
</body>
@code {
[Parameter]
public Poll _poll { get; set; }
[Parameter]
public LiebUser? _user { get; set; }
public Raid _raid { get; set; }
bool _isCollapsed = true;
protected override async Task OnParametersSetAsync()
{
if(_poll.RaidId.HasValue)
{
_raid = RaidService.GetRaid(_poll.RaidId.Value);
}
}
}

View file

@ -0,0 +1,12 @@
body {
background-color: rgb(38 38 38);
border-radius: 25px;
padding: 25px;
/*width: 900px;*/
width: stretch;
color: lightgrey;
}
h5 {
color: lightgrey;
}

View file

@ -0,0 +1,47 @@
@page "/polloverview"
@using Lieb.Data
@using System.Security.Claims
@using Lieb.Models
@using Lieb.Models.Poll
@using Lieb.Models.GuildWars2.Raid
@inject PollService PollService
@inject UserService UserService
@inject AuthenticationStateProvider AuthenticationStateProvider
<h3>Event Overview</h3>
<AuthorizeView Policy="@Constants.Roles.RaidLead.Name">
<div class="nav-item px-3">
<NavLink class="nav-link" href="pollcreate">
<span class="oi oi-plus" aria-hidden="true"></span> Add Event
</NavLink>
</div>
<br />
@foreach (var poll in _polls.OrderByDescending(p => p.PollId))
{
<br />
<PollDetails _poll=@poll _user=@_user/>
}
</AuthorizeView>
@code
{
private List<Poll> _polls;
private LiebUser? _user;
protected override async Task OnInitializedAsync()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
if (authState.User.Identity.IsAuthenticated)
{
ulong discordId = ulong.Parse(authState.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value);
_user = UserService.GetLiebUser(discordId);
}
_polls = PollService.GetPolls();
}
}

View file

@ -59,6 +59,15 @@
</div>
</Authorized>
</AuthorizeView>
<AuthorizeView Policy="@Constants.Roles.RaidLead.Name">
<Authorized>
<div class="nav-item px-3">
<NavLink class="nav-link" href="polloverview">
<span class="oi oi-pie-chart" aria-hidden="true"></span> Polls
</NavLink>
</div>
</Authorized>
</AuthorizeView>
<AuthorizeView Policy="@Constants.Roles.Admin.Name">
<Authorized>
<div class="nav-item px-3">