public async Task <AccountInfo> CreateAccount(AccountForCreate model)
        {
            var accountEntity = _mapper.Map <AccountInfo>(model);

            await _accountRepository.AddAsync(accountEntity);

            await _accountRepository.SaveChangesAsync();

            return(accountEntity);
        }
        public async Task <IActionResult> CreateAccount(AccountForCreate model)
        {
            _logger.LogInformation("Create new account");

            var userFromDb = _userManager.FindByEmailAsync(model.Email);

            if (userFromDb == null)
            {
                ModelState.AddModelError(nameof(model.Email), $"User with email: {model.Email} not found");

                return(new UnprocessableEntityObjectResult(ModelState));
            }

            var accountEntity = await _accountService.CreateAccount(model);

            var accountToReturn = _mapper.Map <AccountForDisplay>(accountEntity);

            return(CreatedAtRoute(
                       "GetAccount",
                       new { id = accountToReturn.Id }, accountToReturn));
        }
Beispiel #3
0
        public async Task <IActionResult> Create(AccountForCreate user)
        {
            if (ModelState.IsValid)
            {
                ApplicationUser identityUser = new ApplicationUser()
                {
                    UserName    = user.Email,
                    Email       = user.Email,
                    PhoneNumber = user.PhoneNum,
                    FullName    = user.FullName,
                    Address     = user.Address
                };
                var FindResult = _userManager.FindByEmailAsync(user.Email).Result;

                if (FindResult != null)
                {
                    ModelState.AddModelError("", "exist this email !");
                    return(View(user));
                }
                var result = await _userManager.CreateAsync(identityUser, user.Password);

                if (result.Succeeded)
                {
                    var role = _roleManager.FindByIdAsync(user.Role_Id);
                    await _userManager.AddToRoleAsync(identityUser, role.Result.Name);

                    return(RedirectToAction("Index", "User"));
                }
                else
                {
                    foreach (var item in result.Errors)
                    {
                        ModelState.AddModelError("", item.Description);
                    }
                }
            }

            return(View(user));
        }