示例#1
0
        public async Task <IActionResult> Register(RegisterViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(StatusCode(StatusCodes.Status400BadRequest,
                                  new Response {
                    Status = "Error", Message = "fill all fields"
                }));
            }

            var userExist = await _userManager.FindByEmailAsync(model.Email);

            if (userExist != null)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError,
                                  new Response {
                    Status = "Error", Message = "User with that email exists"
                }));
            }

            ApplicationUser user = new ApplicationUser
            {
                FullName      = model.FullName,
                SecurityStamp = Guid.NewGuid().ToString(),
                UserName      = model.Email,
                Email         = model.Email,
            };

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

            if (res.Succeeded)
            {
                _networkRepository.AddNetwork(new Network {
                    ApplicationUserId = user.Id
                });

                if (await _networkRepository.SaveChangesAsync())
                {
                    return(Ok(new Response {
                        Status = "Success", Message = "Registered successfully"
                    }));
                }
            }

            return(StatusCode(StatusCodes.Status500InternalServerError,
                              new Response {
                Status = "Error", Message = "Registration failed"
            }));
        }