public ActionResult Nominations(int id) { Election election = db.Elections.Find(id); if (election == null) { return(HttpNotFound()); } string currentUsername = User.Identity.GetUserId(); // ReSharper disable once SimplifyLinqExpression if (!election.EligibilityEntries.Any(entry => entry.Username == currentUsername)) { // This student is not eligible for this election - pretend it doesn't exist return(HttpNotFound()); } List <PositionEligibilityEntry> positionEligibilityEntries = db.PositionEligibilityEntries .Where(entry => entry.Username == currentUsername) .Where(entry => entry.Position.ElectionId == election.Id) .ToList(); NomineeFetcher nomineeFetcher = new NomineeFetcher(new TimetableUserRepository()); NominationsData data = new NominationsData( election, ElectionLifecycleInfo.CanNominate(election), nomineeFetcher.Fetch(positionEligibilityEntries.Select(entry => entry.Position)) ); foreach (PositionEligibilityEntry entry in positionEligibilityEntries) { data.ElgibilePositions.Add(new NominationsData.PositionData( entry.Position, entry.CanNominate, entry.Position.NominationEntries.Any(nomination => nomination.Username == currentUsername) )); } return(View(data)); }
public ActionResult UpdateNominationsStatus(int positionId, bool newStatus) { VotablePosition position = db.VotablePositions.Find(positionId); if (position == null) { return(HttpNotFound()); } Election election = position.Election; string currentUsername = User.Identity.GetUserId(); // ReSharper disable once SimplifyLinqExpression if (!election.EligibilityEntries.Any(entry => entry.Username == currentUsername)) { // This student is not eligible for this election - pretend it doesn't exist return(HttpNotFound()); } PositionEligibilityEntry positionEligibilityEntry = db.PositionEligibilityEntries .FirstOrDefault(entry => entry.PositionId == positionId && entry.Username == currentUsername); if (positionEligibilityEntry == null) { // This student is not eligible for this position - pretend it doesn't exist return(HttpNotFound()); } // At this point we are sure that the position is visible to this student // Now we need to check whether (s)he can change the status (at this time) if (!ElectionLifecycleInfo.CanNominate(election) || !positionEligibilityEntry.CanNominate) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } // At this point we are sure that the student can change the nomination status for this position // Now we just need to create or delete the record in database (or do nothing if it's already matching) NominationEntry exitingNomination = position.NominationEntries .FirstOrDefault(entry => entry.Username == currentUsername); if (exitingNomination != null) { if (newStatus) { // Do nothing } else { // There is an existing one, remove it db.NominationEntries.Remove(exitingNomination); db.SaveChanges(); } } else { if (newStatus) { // No nomination currently, create one db.NominationEntries.Add(new NominationEntry() { Position = position, Username = currentUsername }); db.SaveChanges(); } else { // Do nothing } } AuditLogManager.RecordNominationUpdate(User, position, newStatus); return(RedirectToAction("Nominations", new { id = election.Id })); }