Esempio n. 1
0
        public Tuple <string, bool> AddElectorate(string name, string detail, string image)
        {
            Electorate newItem = new Electorate()
            {
                Name   = name,
                Detail = detail,
                Image  = image
            };

            using (var ctx = _dbContext = new votingBackendDbContext(builder.Options))
            {
                try
                {
                    ctx.ElectorateSet.Add(newItem);
                    ctx.SaveChanges();

                    if (newItem.Id >= 0)
                    {
                        return(Tuple.Create("Success", true));
                    }
                    else
                    {
                        return(Tuple.Create("Unable to register electorate", false));
                    }
                }
                catch (Exception ex)
                {
                    return(Tuple.Create(ex.Message, false));
                }
            }
        }
Esempio n. 2
0
 public static void LogOut(ref Electorate electorate)
 {
     if (electorate == null)
     {
         return;
     }
     //set loged flag
     electorate.logged = 0;
     ElectorateFacade.UpdateElector(electorate);
 }
Esempio n. 3
0
 public static bool TryLogin(ref Electorate electorate)
 {
     if (electorate.logged == 1)
     {
         //someone is loged alrady
         return(false);
     }
     //set loged flag
     electorate.logged = 1;
     ElectorateFacade.UpdateElector(electorate);
     return(true);
 }
 public static Electorate GetElectorate(Electorate electorateTemplate)
 {
     using (var db = new ElectionsEntities())
     {
         var query = from b in db.Electorates
                     where b.pesel == electorateTemplate.pesel &&
                     (electorateTemplate.name == null || b.name == electorateTemplate.name) &&
                     (electorateTemplate.surname == null || b.surname == electorateTemplate.surname)
                     select b;
         return(query.SingleOrDefault());
     }
 }
 public static void UpdateElector(Electorate electorate)
 {
     using (var db = new ElectionsEntities())
     {
         var result = db.Electorates.SingleOrDefault(b => b.idelectorate == electorate.idelectorate);
         if (result != null)
         {
             result.name    = electorate.name;
             result.surname = electorate.surname;
             result.pesel   = electorate.pesel;
             result.voted   = electorate.voted;
             result.logged  = electorate.logged;
             db.SaveChanges();
         }
     }
 }
 public static Electorate AddNewElectorate(Electorate electorateTemplate)
 {
     using (var db = new ElectionsEntities())
     {
         Electorate check = new Electorate();
         check.pesel = electorateTemplate.pesel;
         if (GetElectorate(check) != null)
         {
             //there already is electorate with provided pesel
             return(null);
         }
         db.Electorates.Add(electorateTemplate);
         db.SaveChanges();
         return(electorateTemplate);
     }
 }
        public static void Vote(ref Electorate electorate, Candidate candidate)
        {
            using (var db = new ElectionsEntities())
            {
                Vote vote = new Vote()
                {
                    date        = DateTime.Now,
                    idcandidate = candidate?.idcandidates,
                    valid       = candidate != null ? (byte)1 : (byte)0,
                    withRights  = 1,
                };
                db.Votes.Add(vote);

                electorate.voted = 1;
                ElectorateFacade.UpdateElector(electorate);

                db.SaveChanges();
            }
        }
Esempio n. 8
0
        public void TestStrategicVoteNeverHarms()
        {
            var seed   = new Random().Next();
            var random = new Random(seed);

            using var a = AssertEx.Context("seed", seed);

            var candidateCount = random.Next(3) + 3;
            var voterCount     = random.Next(3) + 6;
            var voterCounts    = Enumerable.Range(0, voterCount).Select(_ => random.Next(10) + 1);

            var voters = new CandidateComparerCollection <Voter>(
                candidateCount,
                Electorate.Normal(candidateCount, random).Cycle().Quality(random).Take(voterCount)
                .Zip(voterCounts, (a, b) => ((Voter)a, b))
                .ToCountedList());

            Assert.Ignore("Not implemented.");
        }
    public static void CleanMetadata(FeatureCollection featureCollection, State?state = null)
    {
        if (!featureCollection.Features.First().Properties.ContainsKey("Elect_div"))
        {
            return;
        }
        foreach (var feature in featureCollection.Features)
        {
            var electorate          = (string)feature.Properties["Elect_div"];
            var stateFromProperties = GetState(feature, state);
            var area = (double)feature.Properties["Area_SqKm"];

            var shortName = Electorate.GetShortName(electorate);
            feature.Properties.Clear();
            feature.Properties["electorateName"]      = electorate;
            feature.Properties["electorateShortName"] = shortName;
            feature.Properties["area"]  = Math.Round(area, 6);
            feature.Properties["state"] = stateFromProperties;
        }
    }
        private void LoginButton_Click(object sender, RoutedEventArgs e)
        {
            if (nameTexBox.Text == string.Empty || surnameTexBox.Text == string.Empty || peselTexBox.Text == string.Empty)
            {
                BizzLayer.LoginFacade.RegisterLoginAttemp(peselTexBox.Text, succesful: false, valid: false);
                MessageBox.Show("Wrong login data", "Login problem", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            string name    = nameTexBox.Text;
            string surname = surnameTexBox.Text;
            string pesel   = peselTexBox.Text;

            Model.PeselData peselData = ParsingUtility.PeselParser.GetPeselData(pesel);

            //check if pesel is valid
            if (peselData == null)
            {
                BizzLayer.LoginFacade.RegisterLoginAttemp(null, succesful: false, valid: false);
                MessageBox.Show("Invalid Pesel!", "Login problem", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            //check if user exists in database
            Electorate templateElectorate = new Electorate()
            {
                name    = ParsingUtility.HashCreator.GetHash(name),
                surname = ParsingUtility.HashCreator.GetHash(surname),
                pesel   = peselData.Pesel
            };
            Electorate electorate = BizzLayer.ElectorateFacade.GetElectorate(templateElectorate);

            if (electorate == null)
            {
                bool?gotResult = BizzLayer.LoginFacade.CheckVotingRights(peselData);
                if (gotResult == false)
                {
                    BizzLayer.LoginFacade.RegisterAttempToVoteWitoutRights();
                    BizzLayer.LoginFacade.RegisterLoginAttemp(peselData.Pesel, succesful: false, valid: true);
                    MessageBox.Show("I am sorry, you have no voting rights.", "Login problem", MessageBoxButton.OK, MessageBoxImage.Information);
                    return;
                }
                else if (gotResult == null)
                {
                    MessageBox.Show("Cannot connect to fetch some login data.\nPlease check your internet connection.", "Login problem", MessageBoxButton.OK, MessageBoxImage.Information);
                    return;
                }

                electorate = BizzLayer.ElectorateFacade.AddNewElectorate(templateElectorate);
                if (electorate == null)
                {
                    BizzLayer.LoginFacade.RegisterLoginAttemp(peselData.Pesel, succesful: false, valid: false);
                    MessageBox.Show("Wrong login data", "Login problem", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
            }

            if (BizzLayer.LoginFacade.TryLogin(ref electorate))
            {
                if (electorate.voted == 1)
                {
                    BizzLayer.LoginFacade.RegisterLoginAttemp(peselData.Pesel, succesful: false, valid: true);
                    MessageBox.Show("You can not vote more than once", "Login problem", MessageBoxButton.OK, MessageBoxImage.Information);
                    return;
                }
                Globals.currentlyLoggedElectorate = electorate;
                Globals.electoralWindow.ShowWithReset();
                Hide();
                BizzLayer.LoginFacade.RegisterLoginAttemp(peselData.Pesel, succesful: true, valid: true);
            }
            else
            {
                BizzLayer.LoginFacade.RegisterLoginAttemp(peselData.Pesel, succesful: false, valid: true);
                MessageBox.Show("Someone is already logged on this account", "Login problem", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }
        }
Esempio n. 11
0
 static string GetCSharpName(Electorate electorate)
 {
     return(electorate.Name.Replace(" ", "").Replace("-", "").Replace("'", ""));
 }
    public static async Task <Electorate> ScrapeElectorate(string shortName, State state)
    {
        var tempElectorateHtmlPath = Path.Combine(DataLocations.TempPath, $"{shortName}.html");

        try
        {
            await Downloader.DownloadFile(tempElectorateHtmlPath, $"https://www.aec.gov.au/profiles/{state}/{shortName}.htm");

            var prefix = "Profile of the electoral division of ";
            //if (!File.Exists(tempElectorateHtmlPath))
            //{
            //    await Downloader.DownloadFile(tempElectorateHtmlPath, $"https://www.aec.gov.au/Elections/federal_elections/2019/profiles/{state}/{shortName}.htm");
            //    prefix = "2019 federal election: profile of the electoral division of ";
            //}
            if (!File.Exists(tempElectorateHtmlPath))
            {
                await Downloader.DownloadFile(tempElectorateHtmlPath, $"https://www.aec.gov.au/Elections/federal_elections/2016/profiles/{state}/{shortName}.htm");

                prefix = "2016 federal election: profile of the electoral division of ";
            }
            if (!File.Exists(tempElectorateHtmlPath))
            {
                throw new Exception($"Could not download {shortName}");
            }
            var document = new HtmlDocument();
            document.Load(tempElectorateHtmlPath);

            var fullName           = GetFullName(document, prefix);
            var values             = new Dictionary <string, HtmlNode>(StringComparer.OrdinalIgnoreCase);
            var profileId          = FindProfileTable(document);
            var htmlNodeCollection = profileId.SelectNodes("dt");
            foreach (var keyNode in htmlNodeCollection)
            {
                var valueNode = keyNode.NextSibling.NextSibling;
                values[keyNode.InnerText.Trim().Trim(':').Replace("  ", " ")] = valueNode;
            }

            var electorate = new Electorate
            {
                Name      = fullName,
                ShortName = shortName,
                State     = state
            };
            if (values.TryGetValue("Date this name and boundary was gazetted", out var gazettedHtml))
            {
                electorate.DateGazetted = DateTime.ParseExact(gazettedHtml.InnerText, "d MMMM yyyy", null);
            }

            electorate.Members             = ElectorateMembers(values).ToList();
            electorate.DemographicRating   = values["Demographic Rating"].TrimmedInnerHtml();
            electorate.ProductsAndIndustry = values["Products/Industries of the Area"].TrimmedInnerHtml();
            electorate.NameDerivation      = values["Name derivation"].TrimmedInnerHtml();
            if (values.TryGetValue("Location Description", out var description))
            {
                electorate.Description = description.TrimmedInnerHtml();
            }

            if (values.TryGetValue("Area and Location Description", out description))
            {
                electorate.Description = description.TrimmedInnerHtml();
            }

            if (values.TryGetValue("Area", out var area))
            {
                electorate.Area = double.Parse(area.InnerHtml.Trim().Replace("&nbsp;", " ").Replace(" ", "").Replace("sqkm", "").Replace(",", ""));
            }

            return(electorate);
        }
        catch (Exception exception)
        {
            throw new Exception($"Failed to parse {shortName} {tempElectorateHtmlPath}", exception);
        }
    }