Exemplo n.º 1
0
        public ActionResult EditPatron()
        {
            var currentUser = User.Identity.Name;
            var patron      = _context.Patrons.SingleOrDefault(c => c.Email == currentUser);

            if (patron == null)
            {
                return(HttpNotFound());
            }

            var roleNames = _context.RoleNames.ToList();
            var cities    = _context.Cities.ToList();
            var states    = _context.States.ToList();
            var zipCodes  = _context.ZipCodes.ToList();
            var viewModel = new RegisterPatronViewModel
            {
                Patron    = patron,
                Cities    = cities,
                RoleNames = roleNames,
                States    = states,
                ZipCodes  = zipCodes,
            };

            return(View(viewModel));
        }
Exemplo n.º 2
0
        public async Task <ActionResult> RegisterPatron(Patron patron)
        {
            if (ModelState.IsValid && patron.Id == 0)
            {
                var user = new ApplicationUser {
                    UserName = patron.Email, Email = patron.Email
                };
                var result = await UserManager.CreateAsync(user, patron.Password);

                if (result.Succeeded)
                {
                    var roleStore   = new RoleStore <IdentityRole>(new ApplicationDbContext());
                    var roleManager = new RoleManager <IdentityRole>(roleStore);
                    await roleManager.CreateAsync(new IdentityRole("Patron"));

                    await UserManager.AddToRoleAsync(user.Id, "Patron");

                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    _context.Patrons.Add(patron);
                }

                AddErrors(result);
            }
            else if (patron.Id != 0)
            {
                var patronInDb = _context.Patrons.Single(c => c.Id == patron.Id);
                patronInDb.FirstName     = patron.FirstName;
                patronInDb.LastName      = patron.LastName;
                patronInDb.BirthDate     = patron.BirthDate;
                patronInDb.StreetOne     = patron.StreetOne;
                patronInDb.StreetTwo     = patron.StreetTwo;
                patronInDb.StateId       = patron.StateId;
                patronInDb.CityId        = patron.CityId;
                patronInDb.ZipCodeId     = patron.ZipCodeId;
                patronInDb.PhoneNumber   = patron.PhoneNumber;
                patronInDb.FollowedBars  = patron.FollowedBars;
                patronInDb.BarReviews    = patron.BarReviews;
                patronInDb.ParsedAddress = (patron.StreetOne + patron.StreetTwo).Replace(" ", "+");
            }
            else if (!ModelState.IsValid)
            {
                var viewModel = new RegisterPatronViewModel
                {
                    Patron    = patron,
                    ZipCodes  = _context.ZipCodes.ToList(),
                    Cities    = _context.Cities.ToList(),
                    States    = _context.States.ToList(),
                    RoleNames = _context.RoleNames.ToList(),
                };
                return(View("RegisterPatron", viewModel));
            }

            _context.SaveChanges();
            return(RedirectToAction("PatronDashboard", "Patron"));
        }
Exemplo n.º 3
0
        public ActionResult RegisterPatron(RoleViewModel model)
        {
            var roleNames = _context.RoleNames.ToList();
            var cities    = _context.Cities.ToList();
            var states    = _context.States.ToList();
            var zipCodes  = _context.ZipCodes.ToList();
            var viewModel = new RegisterPatronViewModel
            {
                Patron    = new Patron(),
                Cities    = cities,
                RoleNames = roleNames,
                States    = states,
                ZipCodes  = zipCodes,
            };

            viewModel.Patron.RoleNameId = model.RoleNameId;

            return(View(viewModel));
        }