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> Edit(int id, [Bind("BallotIssueTitle,Description,OptionsTitles")] BallotIssueCreate model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var bi = _context.BallotIssues.Find(id);
                    bi.BallotIssueTitle = model.BallotIssueTitle;
                    bi.Description      = model.Description;
                    _context.Update(bi);

                    foreach (string title in model.OptionsTitles)
                    {
                        if (title != null && title.Length > 0)
                        {
                            var existing = _context.IssueOptions.Where(op => op.BallotIssueId == id).ToList();
                            _context.RemoveRange(existing);

                            IssueOption opt = new IssueOption
                            {
                                BallotIssueId    = id,
                                IssueOptionTitle = title,
                                IssueOptionInfo  = title,
                            };
                            _context.Add(opt);
                        }
                    }
                    await _context.SaveChangesAsync();
                }

                catch (DbUpdateConcurrencyException)
                {
                    if (!BallotIssueExists(id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(model));
        }
Exemplo n.º 3
0
        public static XmlNodeList AddressIssue(this DotNS api, Issue issue, IssueOption option)
        {
            if (!api.IsAuthed)
            {
                throw new Exception("Not authentificated.");
            }
            var nvc = new NameValueCollection();

            nvc.Add("nation", api.Nation);
            nvc.Add("c", "issue");
            nvc.Add("issue", issue.ID.ToString());
            nvc.Add("option", option.ID.ToString());
            var    resp     = Utilities.API(nvc, null, api.Pin, api.UserAgent);
            string xml      = Utilities.StrResp(resp);
            var    nodelist = Utilities.Parse(xml, "*");

            return(nodelist);
        }
Exemplo n.º 4
0
        public static PrivateNationInfo GetPrivateNation(this DotNS api)
        {
            if (!api.IsAuthed)
            {
                throw new Exception("Not authentificated.");
            }
            var nvc = new NameValueCollection();

            nvc.Add("nation", api.Nation);
            nvc.Add("q", "dossier+issues+issuesummary+nextissue+nextissuetime+notices+packs+ping+rdossier+unread");
            var    resp     = Utilities.API(nvc, null, api.Pin, api.UserAgent);
            string xml      = Utilities.StrResp(resp);
            var    nodelist = Utilities.Parse(xml);

            var nsinfo = new PrivateNationInfo();

            foreach (XmlNode node in nodelist)
            {
                switch (node.Name.ToLower())
                {
                case "unread":
                    nsinfo.UnreadIssues    = int.Parse(node.ChildNodes.FindProperty("issues"));
                    nsinfo.UnreadTelegrams = int.Parse(node.ChildNodes.FindProperty("telegrams"));
                    nsinfo.UnreadNotices   = int.Parse(node.ChildNodes.FindProperty("notices"));
                    nsinfo.UnreadRegion    = int.Parse(node.ChildNodes.FindProperty("rmb"));
                    nsinfo.UnreadWA        = int.Parse(node.ChildNodes.FindProperty("wa"));
                    nsinfo.UnreadNews      = int.Parse(node.ChildNodes.FindProperty("news"));
                    break;

                case "notices":
                    List <Notice> notices = new List <Notice>();
                    foreach (XmlNode n in node.ChildNodes)
                    {
                        var child  = n.ChildNodes;
                        var notice = new Notice();
                        notice.Read      = child.FindProperty("ok") == "1";
                        notice.Text      = child.FindProperty("text");
                        notice.Timestamp = long.Parse(child.FindProperty("timestamp"));
                        notice.Title     = child.FindProperty("title");
                        notice.Type      = child.FindProperty("type");
                        notice.TypeIcon  = child.FindProperty("type_icon");
                        notice.URL       = child.FindProperty("url");
                        notice.Who       = child.FindProperty("who");
                        notices.Add(notice);
                    }
                    nsinfo.Notices = notices.ToArray();
                    break;

                case "dossier":
                    var dossier = new List <string>();
                    foreach (XmlNode n in node.ChildNodes)
                    {
                        dossier.Add(n.InnerText);
                    }
                    nsinfo.NationDossier = dossier.ToArray();
                    break;

                case "rdossier":
                    var rdossier = new List <string>();
                    foreach (XmlNode n in node.ChildNodes)
                    {
                        rdossier.Add(n.InnerText);
                    }
                    nsinfo.RegionDossier = rdossier.ToArray();
                    break;

                case "issues":
                    var issues = new List <Issue>();
                    foreach (XmlNode n in node.ChildNodes)
                    {
                        var child = n.ChildNodes;
                        var issue = new Issue();
                        issue.Title  = child.FindProperty("title");
                        issue.Text   = child.FindProperty("text");
                        issue.Author = child.FindProperty("author");
                        issue.Editor = child.FindProperty("editor");
                        issue.Pic1   = child.FindProperty("pic1");
                        issue.Pic2   = child.FindProperty("pic2");
                        issue.ID     = int.Parse(n.Attributes["id"].InnerText);
                        var options = new List <IssueOption>();
                        foreach (XmlNode n2 in child)
                        {
                            if (n2.Name == "OPTION")
                            {
                                var option = new IssueOption()
                                {
                                    ID   = int.Parse(n2.Attributes["id"].InnerText),
                                    Text = n2.InnerText
                                };
                                options.Add(option);
                            }
                        }
                        issue.Options = options.ToArray();
                        issues.Add(issue);
                    }
                    nsinfo.Issues = issues.ToArray();
                    break;

                case "nextissue":
                    nsinfo.NextIssue = node.InnerText; break;

                case "nextissuetime":
                    nsinfo.NextIssueTime = long.Parse(node.InnerText); break;

                case "packs":
                    nsinfo.Packs = int.Parse(node.InnerText); break;
                }
            }
            return(nsinfo);
        }
        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)));
        }