public async Task <IActionResult> Create(BallotIssueCreate model)
        {
            if (ModelState.IsValid)
            {
                BallotIssue issue = new BallotIssue
                {
                    BallotIssueTitle = model.BallotIssueTitle,
                    Description      = model.Description,
                    ElectionId       = _context.StateSingleton.Find(State.STATE_ID).currentElection
                };
                _context.Add(issue);

                foreach (string title in model.OptionsTitles)
                {
                    if (title != null && title.Length > 0)
                    {
                        IssueOption opt = new IssueOption
                        {
                            BallotIssueId    = issue.BallotIssueId,
                            IssueOptionTitle = title,
                            IssueOptionInfo  = title,
                        };
                        _context.Add(opt);
                    }
                }

                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(model));
        }
        public async Task <IActionResult> Create(BallotIssue issue)
        {
            if (ModelState.IsValid)
            {
                issue.ElectionId = _managedElectionID;

                for (int i = issue.BallotIssueOptions.Count - 1; i >= 0; i--)
                {
                    if (issue.BallotIssueOptions[i].IssueOptionInfo == null || issue.BallotIssueOptions[i].IssueOptionInfo == "")
                    {
                        issue.BallotIssueOptions.RemoveAt(i);
                    }
                }

                if (issue.BallotIssueOptions.Count < 2)
                {
                    ViewData["IssueOptionsError"] = "Please enter at least 2 options.";
                    return(View(issue));
                }

                _context.Add(issue);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(issue));
        }
 public Models.BallotIssueItem GetModel(BallotIssue issue)
 {
     return(new Models.BallotIssueItem()
     {
         VoteIssueId = issue.BallotIssueId,
         Name = issue.Name,
         OfficalName = issue.OfficalName,
         Description = issue.Description,
         Subtext = issue.Subtext,
         VotedFor = issue.VotedFor
     });
 }
        public async Task <IActionResult> Edit(int id, BallotIssue ballotIssue)
        {
            if (id != ballotIssue.BallotIssueId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    for (int i = ballotIssue.BallotIssueOptions.Count - 1; i >= 0; i--)
                    {
                        if (ballotIssue.BallotIssueOptions[i].IssueOptionInfo == null ||
                            ballotIssue.BallotIssueOptions[i].IssueOptionInfo == "")
                        {
                            ballotIssue.BallotIssueOptions.RemoveAt(i);
                        }
                    }

                    if (ballotIssue.BallotIssueOptions.Count < 2)
                    {
                        ViewData["IssueOptionsError"] = "Please enter at least 2 options.";
                        return(View(ballotIssue));
                    }

                    // remove the existing issue options
                    var existing = _context.IssueOptions.Where(op => op.BallotIssueId == id).ToList();
                    _context.RemoveRange(existing);

                    ballotIssue.ElectionId = _managedElectionID;
                    _context.Update(ballotIssue);
                    await _context.SaveChangesAsync();
                }

                catch (DbUpdateConcurrencyException)
                {
                    if (!BallotIssueExists(id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(ballotIssue));
        }
        public virtual IActionResult GetOptionFields(int count)
        {
            List <IssueOption> options = new List <IssueOption>();

            for (int i = 0; i <= count; i++)
            {
                options.Add(new IssueOption());
            }

            BallotIssue ballotIssue = new BallotIssue
            {
                BallotIssueOptions = options
            };

            return(PartialView("IssueOption", ballotIssue));
        }
        public async Task <IActionResult> Copy(int?id)
        {
            // New Election
            var election = await _context.Elections.FindAsync(id);

            election.ElectionId   = _context.Elections.OrderByDescending(e => e.ElectionId).FirstOrDefault().ElectionId + 1;
            election.ElectionName = "Copy of " + election.ElectionName;
            _context.Add(election);
            await _context.SaveChangesAsync();

            // Copy Races
            var races  = _context.Races.Where(r => r.ElectionId == id);
            var raceId = _context.Races.OrderByDescending(r => r.RaceId).FirstOrDefault().RaceId;
            var i      = 1;

            foreach (var r in races)
            {
                var tempRace = r;
                tempRace.RaceId     = raceId + i;
                tempRace.ElectionId = election.ElectionId;
                _context.Add(tempRace);
                await _context.SaveChangesAsync();

                i++;
            }

            // Copy Steps
            var steps   = _context.Steps.Where(s => s.ElectionId == id);
            var stepsId = _context.Steps.OrderByDescending(s => s.ID).FirstOrDefault().ID;

            foreach (var s in steps)
            {
                var tempS = s;
                tempS.ID         = ++stepsId;
                tempS.ElectionId = election.ElectionId;
                _context.Add(tempS);
                await _context.SaveChangesAsync();
            }

            // Copy Candidate
            var candidates   = _context.Candidates.Where(c => c.ElectionId == id);
            var candidatesId = _context.Candidates.OrderByDescending(c => c.CandidateId).FirstOrDefault().CandidateId;

            i = 1;
            foreach (var c in candidates)
            {
                var tempCandidate = new Candidate {
                    CandidateId    = candidatesId + i,
                    ElectionId     = election.ElectionId,
                    Name           = c.Name,
                    Picture        = "images/default.jpg",
                    OrganizationId = c.OrganizationId
                };
                _context.Add(tempCandidate);
                await _context.SaveChangesAsync();

                i++;

                // Copy CandidateDetails
                var candidateDetails   = _context.CandidateDetails.Where(cd => cd.CandidateId == c.CandidateId);
                var candidateDetailsId = _context.CandidateDetails.OrderByDescending(cd => cd.ID).FirstOrDefault().ID;
                var j = 1;
                foreach (var cd in candidateDetails)
                {
                    var tempCandidateDetails = new CandidateDetail {
                        ID          = candidateDetailsId + j,
                        CandidateId = tempCandidate.CandidateId,
                        Title       = cd.Title,
                        Text        = cd.Text,
                        Format      = cd.Format,
                        Lang        = cd.Lang
                    };
                    _context.Add(tempCandidateDetails);
                    await _context.SaveChangesAsync();

                    j++;
                }

                // Copy Candidate Contacts
                var candidateContacts   = _context.Contacts.Where(con => con.CandidateId == c.CandidateId);
                var candidateContactsId = _context.Contacts.OrderByDescending(con => con.ContactId).FirstOrDefault().ContactId;
                j = 1;
                foreach (var con in candidateContacts)
                {
                    var tempCandidateContacts = new Contact {
                        ContactId     = candidateContactsId + j,
                        ContactMethod = con.ContactMethod,
                        ContactValue  = con.ContactValue,
                        CandidateId   = tempCandidate.CandidateId
                    };
                    _context.Add(tempCandidateContacts);
                    await _context.SaveChangesAsync();

                    j++;
                }

                // Copy Candidate Races
                var newRaces = _context.Races.Where(r => r.ElectionId == election.ElectionId);
                var k        = 0;
                foreach (var r in races)
                {
                    var candidateRaces   = _context.CandidateRaces.Where(cr => cr.CandidateId == c.CandidateId && cr.RaceId == r.RaceId);
                    var candidateRacesId = _context.CandidateRaces.OrderByDescending(cr => cr.CandidateRaceId).FirstOrDefault().CandidateRaceId;
                    j = 1;
                    foreach (var cr in candidateRaces)
                    {
                        var tempCR = new CandidateRace {
                            CandidateRaceId = candidateRacesId + j,
                            CandidateId     = tempCandidate.CandidateId,
                            RaceId          = newRaces.Skip(k).First().RaceId,
                            BallotOrder     = cr.BallotOrder
                        };
                        _context.Add(tempCR);
                        await _context.SaveChangesAsync();

                        j++;
                    }
                    k++;
                }
            }

            // Copy Polling Places
            var pollingPlaces   = _context.PollingPlaces.Where(c => c.ElectionId == id);
            var pollingPlacesId = _context.PollingPlaces.OrderByDescending(pp => pp.PollingPlaceId).FirstOrDefault().PollingPlaceId;

            i = 1;
            foreach (var pp in pollingPlaces)
            {
                var tempPp = new PollingPlace {
                    PollingPlaceId     = pollingPlacesId + i,
                    ElectionId         = election.ElectionId,
                    PollingPlaceName   = pp.PollingPlaceName,
                    PollingStationName = pp.PollingStationName,
                    Address            = pp.Address,
                    WheelchairInfo     = pp.WheelchairInfo,
                    ParkingInfo        = pp.ParkingInfo,
                    Latitude           = pp.Latitude,
                    Longitude          = pp.Longitude,
                    AdvanceOnly        = pp.AdvanceOnly,
                    LocalArea          = pp.LocalArea,
                    Phone = pp.Phone,
                    Email = pp.Email
                };
                _context.Add(tempPp);
                await _context.SaveChangesAsync();

                i++;

                // Copy PollingPlaceDates
                var pollingPlaceDates = _context.PollingPlaceDates.Where(ppd => ppd.PollingPlaceId == pp.PollingPlaceId);
                var pollingDateId     = _context.PollingPlaceDates.OrderByDescending(ppd => ppd.PollingDateId).FirstOrDefault().PollingDateId;
                var j = 1;
                foreach (var ppd in pollingPlaceDates)
                {
                    var tempPpd = new PollingPlaceDate {
                        PollingDateId  = pollingDateId + j,
                        PollingPlaceId = tempPp.PollingPlaceId,
                        PollingDate    = ppd.PollingDate,
                        StartTime      = ppd.StartTime,
                        EndTime        = ppd.EndTime
                    };
                    _context.Add(tempPpd);
                    await _context.SaveChangesAsync();

                    j++;
                }
            }

            // Copy BallotIssues
            var ballotIssues   = _context.BallotIssues.Where(b => b.ElectionId == id);
            var ballotIssuesId = _context.BallotIssues.OrderByDescending(b => b.BallotIssueId).FirstOrDefault().BallotIssueId;

            i = 1;
            foreach (var b in ballotIssues)
            {
                var tempB = new BallotIssue {
                    BallotIssueId    = ballotIssuesId + i,
                    ElectionId       = election.ElectionId,
                    BallotIssueTitle = b.BallotIssueTitle,
                    Description      = b.Description
                };
                _context.Add(tempB);
                await _context.SaveChangesAsync();

                i++;

                // IssueOptions
                var issueOptions   = _context.IssueOptions.Where(io => io.BallotIssueId == b.BallotIssueId);
                var issueOptionsId = _context.IssueOptions.OrderByDescending(io => io.IssueOptionId).FirstOrDefault().IssueOptionId;
                var j = 1;
                foreach (var bi in issueOptions)
                {
                    var tempBi = new IssueOption {
                        IssueOptionId   = issueOptionsId + j,
                        IssueOptionInfo = bi.IssueOptionInfo,
                        BallotIssueId   = tempB.BallotIssueId
                    };
                    _context.Add(tempBi);
                    await _context.SaveChangesAsync();

                    j++;
                }
            }

            // Copy Social Medias
            var socialMedias   = _context.SocialMedias.Where(c => c.ElectionId == id);
            var socialMediasId = _context.SocialMedias.OrderByDescending(sm => sm.ID).FirstOrDefault().ID;

            foreach (var sm in socialMedias)
            {
                var tempSM = sm;
                tempSM.ID         = ++socialMediasId;
                tempSM.ElectionId = election.ElectionId;
                _context.Add(tempSM);
                await _context.SaveChangesAsync();
            }

            return(RedirectToAction(nameof(Index)));
        }