public ActionResult EditPositionEntry(PositionEntryEdit model)
        {
            if (!ModelState.IsValid)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            PositionEligibilityEntry entry = _db.PositionEligibilityEntries.Find(model.UserId, model.PositionId);

            if (entry == null)
            {
                return(Json(new
                {
                    Success = false,
                    HumanError = "Error: record wasn't found. Please reload the page and try again"
                }));
            }

            entry.CanNominate = model.CanNominate;
            entry.CanVote     = model.CanVote;

            _db.SaveChanges();

            PositionsTableRow tableRow = new PositionsTableRow(entry);

            PopulateUserInfo(tableRow);

            return(Json(new
            {
                Success = true,
                Entry = tableRow
            }));
        }
        public ActionResult NewPositionEntry(NewPositionEntry model)
        {
            if (!ModelState.IsValid)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            // STEP 1: Fetch everything we got as IDs

            Election           election = _db.Elections.Find(model.ElectionId);
            VotablePosition    position = _db.VotablePositions.Find(model.PositionId);
            TimetableUserEntry user     = _userRepository.GetByUsername(model.UserId);

            // STEP 2: Check that everything is correct

            List <string> errors        = new List <string>();
            bool          suggestReload = false;

            if (election == null)
            {
                errors.Add("Failed to find election");
                suggestReload = true;
            }

            if (position == null)
            {
                errors.Add("Failed to find the selected position");
                suggestReload = true;
            }

            if (election != null && position != null && position.ElectionId != election.Id)
            {
                errors.Add("The selected position doesn't belong to current election");
                suggestReload = true;
            }

            if (user == null)
            {
                errors.Add("Invalid student ID");
            }

            if (user != null && !user.IsStudentActive)
            {
                errors.Add("Only active students can participate in elections");
            }

            if (errors.Count > 0)
            {
                ViewBag.SuggestReload = suggestReload;
                string errorHtml = PartialView("_ErrorsDisplay", errors).RenderToString();

                return(Json(new
                {
                    Success = false,
                    HumanErrorHtml = errorHtml
                }));
            }

            // STEP 3: Check that there isn't an entry already for this user-position tuple

            bool isDuplicateEntry = _db.PositionEligibilityEntries
                                    .Any(entry => entry.PositionId == position.Id && entry.Username == user.UserId);

            if (isDuplicateEntry)
            {
                return(Json(new
                {
                    Success = false,
                    HumanError =
                        "Error: there is already an entry for this user-position tuple. Please edit that instead"
                }));
            }

            // STEP 4: Create a new entity and save it

            PositionEligibilityEntry newEntry = new PositionEligibilityEntry()
            {
                Username = model.UserId,
                Position = position,

                CanNominate = model.CanNominate,
                CanVote     = model.CanVote
            };

            _db.PositionEligibilityEntries.Add(newEntry);
            _db.SaveChanges();

            // STEP 5: Prepare the displayed table row

            PositionsTableRow tableRow = new PositionsTableRow(newEntry);

            PopulateUserInfo(tableRow, user);

            // STEP 6: Reply that we are done

            return(Json(new
            {
                Success = true,
                Entry = tableRow
            }));
        }