public async Task <IActionResult> Create(PatronCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = _mapper.Map <User>(model);

                user.LibraryCard = new LibraryCard
                {
                    Fees    = 0,
                    Created = DateTime.Now
                };

                user.HomeLibraryBranch = _branch.GetBranchByName(model.HomeLibraryBranchName);

                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var confirmationLink = Url.Action("ConfirmEmail", "Account",
                                                      new { userId = user.Id, token = token }, Request.Scheme);

                    _logger.Log(LogLevel.Warning, confirmationLink);

                    var addToRoleResult = await _userManager.AddToRoleAsync(user, "Patron");

                    if (!addToRoleResult.Succeeded)
                    {
                        ModelState.AddModelError("", "Cannot add user to the Patron role.");
                        return(View(model));
                    }

                    BackgroundJob.Enqueue <IEmailService>(x => x.SendEmailAsync(user.FirstName, user.Email, "Email confirmation",
                                                                                $"Congratulations! You are registered. </br> This is your libraryCardId: {user.LibraryCard.Id} </br>" +
                                                                                $"<a href= \"{confirmationLink}\">Please confirm your email address " +
                                                                                $"by clicking this text</a>"));

                    if (_signInManager.IsSignedIn(User) && User.IsInRole("Admin"))
                    {
                        return(RedirectToAction("UsersList", "Administration"));
                    }

                    ViewBag.ErrorTitle   = "Registration successful";
                    ViewBag.ErrorMessage = $"This is your libraryCardId: {user.LibraryCard.Id}. Before you can log in," +
                                           $" please confirm your email, by clicking on the confirmation link " +
                                           $"we have emailed you.";

                    return(View("~/Views/Account/RegistrationSuccessful.cshtml"));
                }

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }
            }

            return(View(model));
        }
Exemple #2
0
        public async Task <IActionResult> Create([Bind("PatronId,FirstName,LastName,LibraryId")] Patron patron)
        {
            if (ModelState.IsValid)
            {
                _context.Add(patron);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            PatronCreateViewModel patronCreateViewModel = new PatronCreateViewModel(_context);

            patronCreateViewModel.Patron = patron;
            return(View(patronCreateViewModel));
        }
        // GET: Patrons/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var patron = await _context.Patron.FindAsync(id);

            if (patron == null)
            {
                return(NotFound());
            }
            PatronCreateViewModel patronCreateViewModel = new PatronCreateViewModel(_context);

            patronCreateViewModel.Patron = patron;
            return(View(patronCreateViewModel));
        }
        public async Task <IActionResult> Create(Patron patron)
        {
            if (patron.LibraryId == 0)
            {
                patron.LibraryId = null;
            }

            if (ModelState.IsValid)
            {
                _context.Add(patron);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            {
                PatronCreateViewModel patronCreateViewModel = new PatronCreateViewModel(_context)
                {
                    Patron = patron
                };
                return(View(patronCreateViewModel));
            }
        }
        // GET: Patrons/Create
        public IActionResult Create()
        {
            PatronCreateViewModel patronCreateViewModel = new PatronCreateViewModel(_context);

            return(View(patronCreateViewModel));
        }