Ejemplo n.º 1
0
        public void Register([FromBody] RegisterEmployeeDto employeeDto)
        {
            if (!_roleManager.RoleExistsAsync(Roles.Employee).Result)
            {
                throw new ApplicationException(ExceptionMessages.RolesHaveNotBeenCreated);
            }

            var userId = GetUserId();

            var user = new ApplicationUser
            {
                UserName = employeeDto.Email,
                Email    = employeeDto.Email
            };

            var result = _userManager.CreateAsync(user, employeeDto.Password).Result;

            if (!result.Succeeded)
            {
                throw new ApplicationException(ExceptionMessages.UsernameAlreadyExists);
            }

            using (var dbContext = new ApplicationDbContext())
            {
                var appUser = _userManager.Users.SingleOrDefault(au => au.Email == employeeDto.Email);

                result = _userManager.AddToRoleAsync(appUser, Roles.Employee).Result;

                if (!result.Succeeded)
                {
                    throw new ApplicationException(ExceptionMessages.InternalServerError);
                }

                var employee = new Clinic_Employee
                {
                    UserId      = appUser.Id,
                    OwnerUserId = userId
                };

                dbContext.Clinic_Employees.Add(employee);
                dbContext.SaveChanges();
            }
        }
Ejemplo n.º 2
0
        private Clinic_Employee CreateEmployee(string email, string password, Clinic clinic)
        {
            Clinic_Employee employee;

            var user = new ApplicationUser
            {
                UserName = email,
                Email    = email
            };

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

            if (!result.Succeeded)
            {
                throw new ApplicationException(ExceptionMessages.UsernameAlreadyExists);
            }

            using (var dbContext = new ApplicationDbContext())
            {
                var appUser = _userManager.Users.SingleOrDefault(au => au.Email == email);

                result = _userManager.AddToRoleAsync(appUser, Roles.Employee).Result;

                if (!result.Succeeded)
                {
                    throw new ApplicationException(ExceptionMessages.InternalServerError);
                }

                employee = new Clinic_Employee
                {
                    UserId      = appUser.Id,
                    OwnerUserId = clinic.UserId
                };

                dbContext.Clinic_Employees.Add(employee);
                dbContext.SaveChanges();
            }

            return(employee);
        }