コード例 #1
0
        public async Task <ActionResult> Edit(InviteWeddingGuestViewModel model)
        {
            if (ModelState.IsValid)
            {
                var guest = await _unitOfWork.WeddingGuestRepository.GetByIDAsync(model.Id);

                guest.InjectFrom(model);
                await _unitOfWork.SaveAsync();

                Success("Wedding Guest was updated successfully.", true);
                return(RedirectToAction("Index", new { id = model.Id }));
            }
            return(View(model));
        }
コード例 #2
0
        public async Task <ActionResult> Edit(string userId)
        {
            if (userId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            var guest = await UserManager.FindByIdAsync(userId) as WeddingGuest;

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

            var viewModel = new InviteWeddingGuestViewModel();

            viewModel.InjectFrom(guest);

            return(View(viewModel));
        }
コード例 #3
0
        public async Task <ActionResult> Invite(InviteWeddingGuestViewModel model)
        {
            InviteWeddingGuestViewModel viewModel = new InviteWeddingGuestViewModel();

            if (ModelState.IsValid)
            {
                var newUser = new WeddingGuest
                {
                    FirstName        = model.FirstName,
                    LastName         = model.LastName,
                    UserName         = model.Email,
                    Email            = model.Email,
                    PhoneNumber      = model.PhoneNumber,
                    IsActive         = true,
                    InviteDate       = DateTime.Now,
                    HasPlusOne       = model.HasPlusOne,
                    PlusOneIsKnown   = model.PlusOneIsKnown,
                    FirstNamePlusOne = model.FirstNamePlusOne,
                    LastNamePlusOne  = model.LastNamePlusOne,
                    InviteMessage    = model.InviteMessage
                };
                // set guest's password to their email address
                string password    = model.Email;
                var    adminresult = await UserManager.CreateAsync(newUser, password);

                string requestMessage = "";

                if (adminresult.Succeeded)
                {
                    // send invitation email to guest
                    string body;
                    string salutation;
                    string plusOneMessage = newUser.HasPlusOne && !newUser.PlusOneIsKnown ? "<h4>But wait, there's more... you get to bring a <i>PLUS ONE!</i> (so choose your guest wisely...)</h4>" : "";
                    string specialMessage = string.IsNullOrEmpty(newUser.InviteMessage) ? "" : "<hr /><p><strong>Special Message from The Honeys: </strong>" + newUser.InviteMessage + "</p><hr />";

                    if (newUser.PlusOneIsKnown)
                    {
                        salutation = newUser.FirstName + " and " + newUser.FirstNamePlusOne;
                    }
                    else
                    {
                        salutation = newUser.FirstName;
                    }

                    using (var sr = new StreamReader(Server.MapPath("\\Templates\\") + "InviteWeddingGuestEmailTemplate.html"))
                    {
                        body = await sr.ReadToEndAsync();
                    }
                    var code = await UserManager.GenerateEmailConfirmationTokenAsync(newUser.Id);

                    var    callbackUrl = Url.Action("RSVP", "WeddingGuests", new { userId = newUser.Id }, protocol: Request.Url.Scheme);
                    string messageBody = string.Format(body, salutation, plusOneMessage, specialMessage, callbackUrl);
                    await UserManager.SendEmailAsync(newUser.Id, "Please RSVP--You're invited to Russ and Lisa's Wedding!", messageBody);

                    //Add User to the selected Roles
                    var result = await UserManager.AddToRoleAsync(newUser.Id, "WeddingGuest");

                    if (!result.Succeeded)
                    {
                        ModelState.AddModelError("", result.Errors.First());
                        ViewBag.RoleId = new SelectList(await RoleManager.Roles.ToListAsync(), "Name", "Name");
                        return(View(viewModel));
                    }
                }
                else
                {
                    ModelState.AddModelError("", adminresult.Errors.First());
                    ViewBag.RoleId = new SelectList(await RoleManager.Roles.ToListAsync(), "Name", "Name");
                    return(View(viewModel));
                }
                Success("Wedding Guest was invited successfully. " + requestMessage, true);
                return(RedirectToAction("Index"));
            }
            return(View(viewModel));
        }