Ejemplo n.º 1
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            // Get the Mentor
            var mentor = await _context.GetMentorAsync(this.Username);

            // Add the pairing to the mentor
            var mentorId = mentor.Mentor.ID;
            var joinCode = Guid.NewGuid().ToString();
            var addDate  = DateTime.Now;

            var pair = new Pair
            {
                DateCreated = addDate,
                MentorID    = mentorId,
                Mentor      = mentor.Mentor,
                JoinCode    = joinCode
            };

            await _context.AddPairAsync(pair);


            var message = "Welcome to the Mentor Protege Program powered by Esolvit Government Solutions. Please use the Code Below to join!: " + joinCode;
            var subject = "Join Mentor-Protege Program.";

            try
            {
                await _emailSender.SendEmailAsync(Input.ProtegeEmailAddress, subject, message);

                await _emailSender.SendEmailAsync(Input.ClientEmailAddress, subject, message);
            }
            catch (Exception)
            {
            }


            var mentorRole        = new IdentityRole("Mentor-" + joinCode);
            var mentorRoleSuccess = await _roleManager.CreateAsync(mentorRole);

            if (mentorRoleSuccess.Succeeded)
            {
                await _userManager.AddToRoleAsync(mentor, mentorRole.Name);
            }

            else
            {
                return(RedirectToPage("/Error"));
            }


            return(RedirectToPage("/Mentor/Pairing/Index"));
        }