public async Task <IActionResult> OnGetAsync(int?id) { if (id == null) { return(NotFound()); } PeopleWinner = await _context.PeopleWinner.FirstOrDefaultAsync(m => m.PeopleWinnerId == id); if (PeopleWinner == null) { return(NotFound()); } return(Page()); }
public async Task <IActionResult> OnPostAsync(int?id) { //Here id refers to tiebreaker id as due to design flaws if (id == null) { return(RedirectToPage("PeopleWinnerList")); } AppCondition app = await _context.AppCondition.FirstOrDefaultAsync(app => app.AppConditionId.Equals(1)); TieBreaker tie = await _context.TieBreaker.FirstOrDefaultAsync(tie => tie.TieBreakerId.Equals(id) && tie.EventID.Equals(app.EventID)); if (tie == null) { //Something obviosuly went wrong return(RedirectToPage("PeopleVotes")); } Team = await _context.Team.FirstOrDefaultAsync(t => t.TeamId.Equals(tie.TeamID) && t.EventID.Equals(tie.EventID)); if (Team.TeamId != tie.TeamID) { //Which shouldn't be the case considering the OnGet function return(RedirectToPage("PeopleVotes")); } PeopleWinner peopleWinner = new PeopleWinner() { EventID = Team.EventID, TeamID = Team.TeamId, TeamName = Team.TeamName, UserID = Team.UserID }; _context.PeopleWinner.Add(peopleWinner); await _context.SaveChangesAsync(); //Now that winner has been found we need to remove the tie breaker teams IList <TieBreaker> tieBreakers = await _context.TieBreaker.ToListAsync(); foreach (var ties in tieBreakers) { _context.TieBreaker.Remove(ties); await _context.SaveChangesAsync(); } return(RedirectToPage("./PeopleWinnerList")); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } PeopleWinner = await _context.PeopleWinner.FindAsync(id); if (PeopleWinner != null) { _context.PeopleWinner.Remove(PeopleWinner); await _context.SaveChangesAsync(); } return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync() { string username = HttpContext.Session.GetString("username"); AppCondition app = await _context.AppCondition.FirstOrDefaultAsync(app => app.AppConditionId.Equals(1)); IList <TieBreaker> tiees = await _context.TieBreaker.Where(tie => tie.EventID.Equals(app.EventID)).ToListAsync(); if (tiees != null) { foreach (var tie in tiees) { _context.TieBreaker.Remove(tie); await _context.SaveChangesAsync(); } } if (username.Equals(app.AdminName)) { isAdmin = true; } else { //Since only admin is supposed to use this functionality we send the other user back isAdmin = false; return(RedirectToPage("Privacy")); } int highVotes = 0; if (Team == null) { Team = await _context.Team.Where(t => t.EventID.Equals(app.EventID)).ToListAsync(); IList <User> Users = await _context.User.ToListAsync(); foreach (var team in Team) { //WE will be using Userid as a place to store the total votes for a team team.UserID = 0; } User loginUser = await _context.User.FirstOrDefaultAsync(u => u.UserName.Equals(username)); if (app.AdminName.Equals(username)) { isAdmin = true; } else { isAdmin = false; } foreach (var user in Users) { //Firstly we need to fetch all the vote users has for this session IList <Vote> userVotes = await _context.Vote.Where(v => v.UserID.Equals(user.UserId) && v.EventID.Equals(app.EventID)).ToListAsync(); //Next based on the vote we assign or increment each vote by one based on the respective user votes foreach (var vote in userVotes) { foreach (var team in Team) { if (vote.TeamID.Equals(team.TeamId)) { //That is the user has voted for this team in the event //So we just increament the team vote by one team.UserID += 1; } } } } } foreach (var team in Team) { if (team.UserID >= highVotes) { highVotes = team.UserID; } } PeopleWinner peopleWinner = await _context.PeopleWinner.FirstOrDefaultAsync(peopleWin => peopleWin.EventID.Equals(app.EventID)); if (peopleWinner != null) { Message = "People Winner has already been decided before!! If you wish to change remove that team first"; isAdmin = true; return(Page()); } //Now that we have to the highest votes we need to find the teams which have recieve high votes foreach (var team in Team) { //We store the team details of each team in a dummy team object for later use Team newTeam = await _context.Team.FirstOrDefaultAsync(t => t.TeamId.Equals(team.TeamId) && t.EventID.Equals(app.EventID)); if (team.UserID.Equals(highVotes)) { TieBreaker tie = new TieBreaker() { UserID = newTeam.UserID, EventID = newTeam.EventID, TeamID = newTeam.TeamId, }; //Now that we know that a team has got highest votes we need to store it within tie breaker _context.TieBreaker.Add(tie); await _context.SaveChangesAsync(); } } //Now we load the team which have got the highest votes for the current event //So we check if we have a tie by checkingthe tie team for the current event IList <TieBreaker> ties = await _context.TieBreaker.Where(tie => tie.EventID.Equals(app.EventID)).ToListAsync(); if (ties.Count() == 1) { Team team = await _context.Team.FirstOrDefaultAsync(t => t.TeamId.Equals(ties[0].TeamID) && t.EventID.Equals(ties[0].EventID)); PeopleWinner people = new PeopleWinner() { EventID = ties[0].EventID, TeamID = ties[0].TeamID, TeamName = team.TeamName, UserID = ties[0].UserID }; _context.PeopleWinner.Add(people); await _context.SaveChangesAsync(); //Now that we know that we don't have a tie breaker for people winner //We will just have to remove the tie breaker teams IList <TieBreaker> tiebreakers = await _context.TieBreaker.Where(tie => tie.EventID.Equals(app.EventID)).ToListAsync(); foreach (var tie in tiebreakers) { _context.TieBreaker.Remove(tie); await _context.SaveChangesAsync(); } return(RedirectToPage("PeopleWinnerList")); } else { //Which would mean that there is same votes among current event teams return(RedirectToPage("TieBreak")); //So for now we won't delete teams but will do it later on } }