Example #1
0
        public async Task <IActionResult> Edit(int id, PollingPlace pollingPlace, string removedDates)
        {
            pollingPlace.ElectionId = _managedElectionID;

            if (removedDates != null && !removedDates.Equals(""))
            {
                string[] itemsToRemove = removedDates.Split(',', StringSplitOptions.RemoveEmptyEntries);
                int[]    indexes       = new int[itemsToRemove.Length];
                for (int i = 0; i < itemsToRemove.Length; ++i)
                {
                    indexes[i] = int.Parse(itemsToRemove[i]);
                }

                // sort in ascending order
                indexes = indexes.OrderBy(i => i).ToArray();

                for (int i = indexes.Length - 1; i >= 0; --i)
                {
                    pollingPlace.PollingPlaceDates.RemoveAt(indexes[i]);
                }
            }

            if (ModelState.IsValid)
            {
                try
                {
                    if (pollingPlace.PollingPlaceDates != null)
                    {
                        // remove the existing dates from the database so they do not get duplicated
                        var existing = await _context.PollingPlaceDates
                                       .Where(pollDate => pollDate.PollingPlaceId == id)
                                       .ToListAsync();

                        _context.RemoveRange(existing);

                        foreach (var date in pollingPlace.PollingPlaceDates)
                        {
                            date.StartTime = date.PollingDate.Add(date.StartTime.TimeOfDay);
                            date.EndTime   = date.PollingDate.Add(date.EndTime.TimeOfDay);
                        }
                    }

                    _context.Update(pollingPlace);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!PollingPlaceExists(id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(pollingPlace));
        }
Example #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SyncCommand"/> class.
 /// Can I have a new command that synchronizes the target?
 /// </summary>
 /// <param name="parent">
 /// The parent-station sending the command.
 /// </param>
 public SyncCommand(Station parent)
 {
     Contract.Requires(parent != null);
     _sender          = parent.Address.ToString();
     _voterData       = parent.Database.AllVoters.ToArray();
     _precinctData    = parent.Database.AllPrecincts.ToArray();
     _pollingplace    = parent.PollingPlace;
     _addresses       = parent.Peers.Keys.Select(endpoint => endpoint.ToString()).ToArray();
     _publicKeys      = parent.Peers.Values.Select(key => key.Value.ToBytes()).ToArray();
     _masterPwHash    = Bytes.FromFile("Master.pw");
     _electionDataKey = parent.Crypto.VoterDataEncryptionKey.Value.ToBytes();
 }
Example #3
0
        public virtual IActionResult GetDateFields(int count)
        {
            List <PollingPlaceDate> dates = new List <PollingPlaceDate>();

            for (int i = 0; i <= count; i++)
            {
                dates.Add(new PollingPlaceDate {
                    PollingDate = DateTime.Now
                });
            }

            PollingPlace pollingPlace = new PollingPlace
            {
                PollingPlaceDates = dates
            };

            return(PartialView("PollingDate", pollingPlace));
        }
Example #4
0
        public async Task <IActionResult> Create(PollingPlace pollingPlace, string removedDates)
        {
            pollingPlace.ElectionId = _managedElectionID;

            if (removedDates != null && !removedDates.Equals(""))
            {
                string[] itemsToRemove = removedDates.Split(',', StringSplitOptions.RemoveEmptyEntries);
                int[]    indexes       = new int[itemsToRemove.Length];
                for (int i = 0; i < itemsToRemove.Length; ++i)
                {
                    indexes[i] = int.Parse(itemsToRemove[i]);
                }

                // sort in ascending order
                indexes = indexes.OrderBy(i => i).ToArray();

                for (int i = indexes.Length - 1; i >= 0; --i)
                {
                    pollingPlace.PollingPlaceDates.RemoveAt(indexes[i]);
                }
            }

            if (ModelState.IsValid)
            {
                foreach (var date in pollingPlace.PollingPlaceDates)
                {
                    date.StartTime = date.PollingDate.Add(date.StartTime.TimeOfDay);
                    date.EndTime   = date.PollingDate.Add(date.EndTime.TimeOfDay);
                }

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

                return(RedirectToAction(nameof(Index)));
            }
            return(View(pollingPlace));
        }
        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)));
        }