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
            }));
        }
Ejemplo n.º 2
0
        public PositionsTableRow(PositionEligibilityEntry dbEntry)
        {
            PositionId   = dbEntry.Position.Id;
            PositionName = dbEntry.Position.HumanName;

            UserId = dbEntry.Username;

            CanNominate = dbEntry.CanNominate;
            CanVote     = dbEntry.CanVote;
        }
        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
            }));
        }
Ejemplo n.º 4
0
        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 }));
        }