コード例 #1
0
        public async Task <IActionResult> AddEmployee(AppUserCreateDto userDto)
        {
            var result = await _appUserService.CreateEmployee(userDto);

            if (result.Succeeded)
            {
                return(Ok());
            }

            return(BadRequest(result.Errors));
        }
コード例 #2
0
        private async Task <IEnumerable <ValidationResult> > ValidateUserCreateDto(AppUserCreateDto user)
        {
            var validationResults = new List <ValidationResult>();

            if (await _userManager.Users.AnyAsync(u => u.Email == user.Email))
            {
                validationResults.Add(new ValidationResult($"Пользователь с таким e-mail: {user.Email} уже зарегистрирована в системе"));
            }

            if (await _userManager.Users.AnyAsync(u => u.PhoneNumber == user.PhoneNumber))
            {
                validationResults.Add(new ValidationResult($"Пользователь с номером телефона: {user.PhoneNumber} уже зарегистрирована в системе"));
            }

            return(validationResults);
        }
コード例 #3
0
        public async Task <IdentityResult> CreateEmployee(AppUserCreateDto userDto)
        {
            var currUser = await GetCurrentUserAsync();

            var employee = _mapper.Map <AppUser>(userDto);

            employee.CompanyId = currUser.CompanyId;

            var result = _userManager.CreateAsync(employee, "password").Result;

            if (result.Succeeded)
            {
                var user = _userManager.FindByNameAsync(employee.UserName).Result;
                await _userManager.AddToRoleAsync(user, "employee");
            }
            return(result);
        }