protected IEnumerable <string> CalculateWinners() { return(UserVotes.Select(votes => votes.Value.Select(reaction => reaction.Message.Value.Content).Distinct()) .SelectMany(votes => votes) .GroupBy(vote => vote) .MaxBy(grouping => grouping.Count()) .Select(grouping => grouping.Key)); }
public VotingItemResponse VoteForItem(VoteRequest request, Guid UserID) { Users user = dbContext.Users.Find(UserID); if (user == null) { throw new BusinessLogicException(HttpStatusCode.BadRequest, ResponseCode.USER_NOT_EXIST.ToString()); } VotingItems votingItem = dbContext.VotingItems .Where(v => v.VotingItemId.Equals(request.VotingItemID) && v.DueDate > DateTime.Now).FirstOrDefault(); if (votingItem == null) { throw new BusinessLogicException(HttpStatusCode.BadRequest, ResponseCode.VOTING_ITEM_NOT_EXIST_OR_EXPIRED.ToString()); } UserVotes userVote = dbContext.UserVotes .Where(us => us.UserId.Equals(UserID) && us.VotingItemId.Equals(request.VotingItemID)).FirstOrDefault(); if (userVote != null) { throw new BusinessLogicException(HttpStatusCode.BadRequest, ResponseCode.USER_ALREADY_VOTED.ToString()); } using (IDbContextTransaction transaction = dbContext.Database.BeginTransaction()) { try { UserVotes newVotes = new UserVotes(); newVotes.UserId = UserID; newVotes.VotingItemId = request.VotingItemID; newVotes.CreatedBy = user.Email; newVotes.UpdatedBy = user.Email; votingItem.SupportersCount += 1; dbContext.UserVotes.Add(newVotes); dbContext.VotingItems.Update(votingItem); dbContext.SaveChanges(); transaction.Commit(); } catch { transaction.Rollback(); throw new BusinessLogicException(HttpStatusCode.InternalServerError, ResponseCode.FAILED_TO_VOTE.ToString()); } } List <Guid> listCategories = constructListCategoryID(votingItem.VotingCategories.ToList()); List <Categories> categories = dbContext.Categories.Where(cat => listCategories.Contains(cat.CategoryId)).ToList(); return(constructResponse(votingItem, categories)); }
public IActionResult CastVoteDecision(UserVotes decision) { try { var updatedVote = _investConnect.CastUserVote(decision); return(Accepted(updatedVote)); } catch { return(BadRequest()); } }
private async Task LoadVotes(IEnumerable <SimpleUser> users) { _activeVote = await _signalHelperFacade.VoteSignalHelper.GetActiveVote().ConfigureAwait(false); var onlineUsers = users as SimpleUser[] ?? users.ToArray(); _onlineUsers = onlineUsers; UserVotes.Clear(); foreach (var user in onlineUsers) { bool voted = _activeVote?.UserAnswerCache.ContainsKey(user.Username) == true; UserVotes.Add(new UsersVotes { Username = user.Username, Voted = voted }); } OnPropertyChanged(nameof(UserVotes)); }
//internal IEnumerable<IUserVotes> GetPendingVotesForClub(int clubId) //{ // using (SqlConnection db = new SqlConnection(_connectionString)) // { // string querystring = @" // SELECT pci.id, p.id PartnerId, pci.Vote, pci.Abstain, pc.ClubId, // i.ReceivingEntity, c.ClubName, it.InvestmentType, i.Id InvestmentId // FROM Partner as p // join PartnerClub as pc on pc.PartnerId = p.Id // join PartnerClubInvestment as pci on pci.PartnerClubId = pc.Id // join Investment as i on i.Id = pci.InvestmentId // join Club as c on c.Id = pc.ClubId // join InvestmentType as it on it.Id = i.AssetType // WHERE p.Id=@ClubId and i.Pending=1 and pc.ApprovedMember=1"; // var parameters = new { ClubId = clubId }; // var pendingInvestments = db.Query<PendingVotes>(querystring, parameters); // if (pendingInvestments != null) // { // return pendingInvestments; // } // } // throw new Exception("trouble getting votes for user"); //} internal IUserVotes CastUserVote(UserVotes submittedVote) { using (SqlConnection db = new SqlConnection(_connectionString)) { var UserVotes = GetVotesForUser(submittedVote.PartnerId, db).ToList(); if (UserVotes.FirstOrDefault(Vote => Vote.Id.Equals(submittedVote.Id)) != null) { string querystring = @" UPDATE PartnerClubInvestment SET Abstain = @Abstain if (SELECT Abstain FROM PartnerClubInvestment WHERE Id=@VoteId) = 1 BEGIN UPDATE PartnerClubInvestment SET Vote = NULL OUTPUT inserted.* WHERE Id=@VoteId END else BEGIN UPDATE PartnerClubInvestment SET Vote = @Vote OUTPUT inserted.* WHERE Id=@VoteId END" ; var parameters = new { VoteId = submittedVote.Id, Vote = submittedVote.Vote, Abstain = submittedVote.Abstain }; var pendingInvestments = db.QueryFirstOrDefault <PartnerClubInvestment>(querystring, parameters); if (pendingInvestments != null) { return(pendingInvestments); } } } throw new Exception("trouble getting votes for user"); }
public void AddVote(SocketReaction reaction) { if (MultipleVotesAllowed) { if (!UserVotes.ContainsKey(reaction.UserId)) { UserVotes.Add(reaction.UserId, new List <SocketReaction>()); } UserVotes[reaction.UserId].Add(reaction); } else { if (UserVotes.ContainsKey(reaction.UserId) && _client.CurrentUser.Id != reaction.UserId) { SocketReaction oldReaction = UserVotes[reaction.UserId].Single(); RemoveVote(oldReaction); } else { UserVotes.Add(reaction.UserId, new List <SocketReaction>()); } UserVotes[reaction.UserId].Add(reaction); } }
public IActionResult OnGet() { string username = HttpContext.Session.GetString("username"); if (username == null) { return(RedirectToPage("Privacy")); } //since in the beginning the vote table will be empty for all user so we just use VoteStatus = "Vote"; User loginUser = _context.User.FirstOrDefault(u => u.UserName.Equals(username)); AppCondition app = _context.AppCondition.FirstOrDefault(app => app.AppConditionId.Equals(1)); AllEvents = _context.Event.ToList(); AllUsers = _context.User.ToList(); //Now we need to check all the votes of the logged-in user for the given event UserVotes = _context.Vote.Where(v => v.UserID.Equals(loginUser.UserId) && v.EventID.Equals(app.EventID)).ToList(); if (UserVotes.Count() >= app.VotesAllowed) { //That means user has reached his/her to limit to vote for the event Message = "You have reached max vote!! You need to unvote a Voted team"; } else { Message = "You can vote any team except your own!!"; } //Here we need to make sure if a judge tries to vote for a team //He/she can only vote a team with the same theme in the same event bool isJudge = false; Team team = _context.Team.FirstOrDefault(t => t.UserID.Equals(loginUser.UserId) && t.EventID.Equals(app.EventID)); //Firstly we find users team and omit that team from the list if (team != null) { //User does have a team for the event Team = _context.Team.Where(t => (t.TeamId != team.TeamId) && t.EventID.Equals(app.EventID)).ToList(); return(Page()); } Member member = _context.Member.FirstOrDefault(m => m.UserID.Equals(loginUser.UserId) && m.EventID.Equals(app.EventID)); if (member != null) { Team = _context.Team.Where(t => (t.TeamId != member.TeamID) && t.EventID.Equals(app.EventID)).ToList(); return(Page()); } Team = _context.Team.Where(t => t.EventID.Equals(app.EventID)).ToList(); return(Page()); }
public async Task <IActionResult> OnGetAsync() { string username = HttpContext.Session.GetString("username"); if (username == null) { return(RedirectToPage("Privacy")); } VoteStatus = "Vote"; User loginUser = _context.User.FirstOrDefault(u => u.UserName.Equals(username)); AppCondition app = _context.AppCondition.FirstOrDefault(app => app.AppConditionId.Equals(1)); AllEvents = _context.Event.ToList(); AllUsers = _context.User.ToList(); //Now we need to check all the votes of the logged-in user for the given event UserVotes = _context.Vote.Where(v => v.UserID.Equals(loginUser.UserId) && v.EventID.Equals(app.EventID)).ToList(); if (UserVotes.Count() >= app.VotesAllowed) { //That means user has reached his/her to limit to vote for the event Message = "You have reached max vote!! You need to unovte a Voted team"; } else { Message = "You can vote any team except your own!!"; } //Here we need to make sure if a judge tries to vote for a team //He/she can only vote a team with the same theme in the same event //We need to check if the login user is a judge Judge judge = await _context.Judge.FirstOrDefaultAsync(j => j.UserID.Equals(loginUser.UserId)); //If the user is not a judge we just send it back if (judge == null) { return(RedirectToPage("Privacy")); } Team team = _context.Team.FirstOrDefault(t => t.UserID.Equals(loginUser.UserId) && t.EventID.Equals(app.EventID)); //Firstly we find users team and omit that team from the list if (team != null) { //User does have a team for the event Team = _context.Team.Where(t => (t.TeamId != team.TeamId) && t.EventID.Equals(app.EventID) && t.ThemeID.Equals(judge.ThemeID)).ToList(); theme = await _context.Theme.FirstOrDefaultAsync(t => t.ThemeId.Equals(judge.ThemeID)); ThemeName = theme.ThemeName; return(Page()); } Member member = _context.Member.FirstOrDefault(m => m.UserID.Equals(loginUser.UserId) && m.EventID.Equals(app.EventID)); if (member != null) { Team = _context.Team.Where(t => (t.TeamId != member.TeamID) && t.EventID.Equals(app.EventID) && t.ThemeID.Equals(judge.ThemeID)).ToList(); theme = await _context.Theme.FirstOrDefaultAsync(t => t.ThemeId.Equals(judge.ThemeID)); ThemeName = theme.ThemeName; return(Page()); } //We just load all the teams with same theme AS THE JUDGE Team = _context.Team.Where(t => t.EventID.Equals(app.EventID) && t.ThemeID.Equals(judge.ThemeID)).ToList(); theme = await _context.Theme.FirstOrDefaultAsync(t => t.ThemeId.Equals(judge.ThemeID)); ThemeName = theme.ThemeName; return(Page()); }