Exemple #1
0
        public async Task <IActionResult> Register([FromBody][Required] RegisterUser registerUser)
        {
            if (registerUser.Admin)
            {
                ModelState.Remove("Program");
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (_userManager.Users.Any(u => u.UserName == registerUser.UserName))
            {
                return(BadRequest("Username not available"));
            }
            if (_userManager.Users.Any(u => u.Email == registerUser.Email))
            {
                return(BadRequest("E-mail not available"));
            }
            CapstoneUser user = new CapstoneUser {
                UserName = registerUser.UserName, Admin = registerUser.Admin, Email = registerUser.Email
            };
            IdentityResult result = await _userManager.CreateAsync(user, registerUser.Password);

            if (result.Succeeded)
            {
                if (!registerUser.Admin)
                {
                    await _studentService.CreateAsync(new Student { User = user, FirstName = registerUser.FirstName, LastName = registerUser.LastName, Program = registerUser.Program });
                }
                return(Ok(new
                {
                    id = user.Id
                }));
            }
            else
            {
                foreach (IdentityError error in result.Errors)
                {
                    ModelState.AddModelError("User Create Error", error.Description);
                }
                return(BadRequest(ModelState));
            }
        }