Ejemplo n.º 1
0
        public IActionResult AddBallot()
        {
            var model = new AddBallotViewModel()
            {
                OfficeNames = _Context.Office.Select(o => o.OfficeName)
            };

            return(View(model));
        }
Ejemplo n.º 2
0
        public IActionResult AddBallot(AddBallotViewModel model)
        {
            if (ModelState.IsValid)
            {
                var data = new BallotDataModel()
                {
                    BallotName  = model.BallotName,
                    ElectionDay = model.ElectionDay,
                    OfficeName  = model.OfficeName
                };
                IEnumerable <ApplicationUser> userList = new HashSet <ApplicationUser>();
                switch (model.Zone)
                {
                case "ZipCode":
                    data.RegionName   = null;
                    data.ZipCode      = int.Parse(model.ZipCode);
                    data.DistrictName = null;
                    userList          = _Context.Zip
                                        // get the one zip
                                        .Where(z => z.ZipCode == data.ZipCode)
                                        // load its addresses
                                        .Include(z => z.Residents)
                                        // convert addresses to one list
                                        .SelectMany(z => z.Residents)
                                        // load the user
                                        .Include(a => a.User)
                                        // convert address to user
                                        .Select(a => a.User)
                                        // return users with confirmed emails
                                        .Where(u => u.EmailConfirmed);
                    break;

                case "District":
                    data.RegionName   = null;
                    data.ZipCode      = null;
                    data.DistrictName = model.DistrictName;
                    userList          = _Context.District
                                        // get the one district
                                        .Where(d => d.DistrictName == data.DistrictName)
                                        // load its bridge table
                                        .Include(d => d.Zip)
                                        // convert bridge table to one list
                                        .SelectMany(d => d.Zip)
                                        // convert bridge table to zip
                                        .Select(zfd => zfd.Zip)
                                        // load its addresses
                                        .Include(z => z.Residents)
                                        // convert addresses to one list
                                        .SelectMany(z => z.Residents)
                                        // load the user
                                        .Include(a => a.User)
                                        // convert address to user
                                        .Select(a => a.User)
                                        // return users with confirmed emails
                                        .Where(u => u.EmailConfirmed);
                    break;

                case "Region":
                    data.RegionName   = model.RegionName;
                    data.ZipCode      = null;
                    data.DistrictName = null;
                    userList          = _Context.Region
                                        // get the one region
                                        .Where(r => r.RegionName == data.RegionName)
                                        // load its bridge table
                                        .Include(r => r.District)
                                        // convert the bridge table to one list
                                        .SelectMany(r => r.District)
                                        // convert the bridge to district
                                        .Select(dfr => dfr.District)
                                        // load its bridge table
                                        .Include(d => d.Zip)
                                        // convert bridge table to one list
                                        .SelectMany(d => d.Zip)
                                        // convert bridge table to zip
                                        .Select(zfd => zfd.Zip)
                                        // load its addresses
                                        .Include(z => z.Residents)
                                        // convert addresses to one list
                                        .SelectMany(z => z.Residents)
                                        // load the user
                                        .Include(a => a.User)
                                        // convert address to user
                                        .Select(a => a.User)
                                        // return users with confirmed emails
                                        .Where(u => u.EmailConfirmed)
                                        .Distinct();
                    break;
                }
                string subject = "New Ballot Avaliable";
                string body    = "There is a new ballot that you can vote on.";
                foreach (ApplicationUser user in userList)
                {
                    _EmailService.SendEmailAsync(user, subject, body);
                }

                _Context.Ballot.Add(data);

                _Context.SaveChanges();

                return(RedirectToAction(nameof(Dashboard)));
            }

            return(View(model));
        }