From 1158aff6df10364f5f4bc1bb0bed840696861131 Mon Sep 17 00:00:00 2001 From: Sarah Faey Date: Sat, 14 Jan 2023 21:04:01 +0100 Subject: [PATCH] added poll pages --- Lieb/Data/PollService.cs | 44 +++++-- Lieb/Data/UserService.cs | 10 ++ Lieb/Models/Poll/PollOption.cs | 2 +- Lieb/Pages/Poll/PollCreate.razor | 179 ++++++++++++++++++++++++++ Lieb/Pages/Poll/PollDetails.razor | 51 ++++++++ Lieb/Pages/Poll/PollDetails.razor.css | 12 ++ Lieb/Pages/Poll/PollOverview.razor | 47 +++++++ Lieb/Shared/NavMenu.razor | 9 ++ 8 files changed, 340 insertions(+), 14 deletions(-) create mode 100644 Lieb/Pages/Poll/PollCreate.razor create mode 100644 Lieb/Pages/Poll/PollDetails.razor create mode 100644 Lieb/Pages/Poll/PollDetails.razor.css create mode 100644 Lieb/Pages/Poll/PollOverview.razor diff --git a/Lieb/Data/PollService.cs b/Lieb/Data/PollService.cs index db9bc5c..aa5af9f 100644 --- a/Lieb/Data/PollService.cs +++ b/Lieb/Data/PollService.cs @@ -15,6 +15,15 @@ namespace Lieb.Data _discordService = discordService; } + public List 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 CreatePoll(string question, List 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 users = raid.SignUps.Where(s => s.LiebUserId != null).Select(s => (ulong)s.LiebUserId).ToList(); - return await CreatePoll(question, options, users, raidId); - } - - public async Task CreatePoll(string question, List options, List 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 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 users = raid.SignUps.Where(s => s.LiebUserId != null).Select(s => (ulong)s.LiebUserId).ToList(); + return await CreatePoll(poll, users, raidId); + } + + public async Task CreatePoll(Poll poll, List users, int? raidId = null) + { + poll.RaidId = raidId; + foreach(ulong user in users) { poll.Answers.Add(new PollAnswer() diff --git a/Lieb/Data/UserService.cs b/Lieb/Data/UserService.cs index 06b0760..cd3b61f 100644 --- a/Lieb/Data/UserService.cs +++ b/Lieb/Data/UserService.cs @@ -357,5 +357,15 @@ namespace Lieb.Data await context.SaveChangesAsync(); return true; } + + public List 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(); + } } } diff --git a/Lieb/Models/Poll/PollOption.cs b/Lieb/Models/Poll/PollOption.cs index 2d5161b..5402f4d 100644 --- a/Lieb/Models/Poll/PollOption.cs +++ b/Lieb/Models/Poll/PollOption.cs @@ -3,6 +3,6 @@ public class PollOption { public int PollOptionId { get; set; } - public string Name { get; set;} + public string Name { get; set;} = string.Empty; } } diff --git a/Lieb/Pages/Poll/PollCreate.razor b/Lieb/Pages/Poll/PollCreate.razor new file mode 100644 index 0000000..c483d81 --- /dev/null +++ b/Lieb/Pages/Poll/PollCreate.razor @@ -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 + + +

Create Poll

+ + + + +

+ +

+

+ +

+ +

+ +

+ + @if(_pollType == POLL_TYPE_RAID) + { +

+ +

+ } + else if(_pollType == POLL_TYPE_GROUP) + { +

+ +

+ } + +

+ +

+ +

+ + + + + + + @foreach(PollOption option in _poll.Options) + { + + + + + } +
Option
+

+ + + + + + +
+
+ +@code { + + public Poll _poll = new Poll(); + private LiebUser _user; + + private string _errorMessage = string.Empty; + + private List _raids = new List(); + private List _availabeGroups = new List(); + + 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"); + } +} \ No newline at end of file diff --git a/Lieb/Pages/Poll/PollDetails.razor b/Lieb/Pages/Poll/PollDetails.razor new file mode 100644 index 0000000..e85f3a8 --- /dev/null +++ b/Lieb/Pages/Poll/PollDetails.razor @@ -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 + + + +
+ +
@_poll.Question
+ @if(_raid != null) + { +

@_raid.Title - @_raid.StartTimeUTC.DateTime.ToLongDateString()

+ } +
+ + @if (!_isCollapsed) + { + @foreach(var answer in _poll.Answers.GroupBy(a => a.Answer)) + { +

@answer.Key - @answer.Count()

+ } + } + + +@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); + } + } + +} diff --git a/Lieb/Pages/Poll/PollDetails.razor.css b/Lieb/Pages/Poll/PollDetails.razor.css new file mode 100644 index 0000000..29b5c56 --- /dev/null +++ b/Lieb/Pages/Poll/PollDetails.razor.css @@ -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; +} \ No newline at end of file diff --git a/Lieb/Pages/Poll/PollOverview.razor b/Lieb/Pages/Poll/PollOverview.razor new file mode 100644 index 0000000..253c47f --- /dev/null +++ b/Lieb/Pages/Poll/PollOverview.razor @@ -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 + + +

Event Overview

+ + + + +
+ @foreach (var poll in _polls.OrderByDescending(p => p.PollId)) + { +
+ + } +
+ +@code +{ + private List _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(); + } +} diff --git a/Lieb/Shared/NavMenu.razor b/Lieb/Shared/NavMenu.razor index fae9be3..f9c05eb 100644 --- a/Lieb/Shared/NavMenu.razor +++ b/Lieb/Shared/NavMenu.razor @@ -59,6 +59,15 @@ + + + + +