Пример #1
0
        public async Task Assign_Roles_To_User()
        {
            try
            {
                ApplicationUser applicationUser = await _applicationUserRepository.FindByNameOrEmailAsync("admin");

                IdentityResult administratorRole =
                    await _applicationUserRepository.AddToRoleAsync(applicationUser, "Administrator");

                IdentityResult clientRole = await _applicationUserRepository.AddToRoleAsync(applicationUser, "Client");

                IdentityResult officeEmployeeRole =
                    await _applicationUserRepository.AddToRoleAsync(applicationUser, "OfficeEmployee");

                IdentityResult technicalEmployeeRole =
                    await _applicationUserRepository.AddToRoleAsync(applicationUser, "TechnicalEmployee");

                Assert.IsTrue(administratorRole.Succeeded);
                Assert.IsTrue(clientRole.Succeeded);
                Assert.IsTrue(officeEmployeeRole.Succeeded);
                Assert.IsTrue(technicalEmployeeRole.Succeeded);
            }
            catch (DbUpdateException e)
            {
                Assert.Fail(e.Message);
            }
        }
Пример #2
0
        public async Task <ActionResult <EmployeeViewModel> > PostEmployee(EmployeeInputModel employeeModel)
        {
            EmployeeCharge employeeCharge = await _employeesRepository.GetAllEmployeeCharges()
                                            .FirstOrDefaultAsync(c => c.Id == employeeModel.ChargeId);

            if (employeeCharge is null)
            {
                return(BadRequest("El cargo del empleado no se encuentra registrado."));
            }

            Employee employee = _mapper.Map <Employee>(employeeModel);

            employee.EmployeeCharge = employeeCharge;

            IdentityResult result =
                await _applicationUserRepository.CreateAsync(employee.User, employeeModel.User.Password);

            if (!result.Succeeded)
            {
                return(this.IdentityResultErrors(result));
            }

            IdentityResult roleResult =
                await _applicationUserRepository.AddToRoleAsync(employee.User, GetEmployeeRole(employee));

            if (!roleResult.Succeeded)
            {
                return(this.IdentityResultErrors(roleResult));
            }

            _employeesRepository.Insert(employee);

            try
            {
                await _unitWork.SaveAsync();
            }
            catch (DbUpdateException)
            {
                if (EmployeeExists(employee.Id))
                {
                    return(Conflict($"Ya existe un empleado registrado con el código {employeeModel.Id}"));
                }

                throw;
            }

            return(_mapper.Map <EmployeeViewModel>(employee));
        }
Пример #3
0
        public async Task <ActionResult <ClientViewModel> > PostClient(ClientInputModel clientInput)
        {
            Client client = _mapper.Map <Client>(clientInput);

            IdentityResult result =
                await _applicationUserRepository.CreateAsync(client.User, clientInput.User.Password);

            if (!result.Succeeded)
            {
                return(this.IdentityResultErrors(result));
            }

            IdentityResult roleResult = await _applicationUserRepository.AddToRoleAsync(client.User, "Client");

            if (!roleResult.Succeeded)
            {
                return(this.IdentityResultErrors(roleResult));
            }

            client.PublishEvent(new SavedPerson(client));
            _clientsRepository.Insert(client);

            try
            {
                await _unitWork.SaveAsync();
            }
            catch (DbUpdateException)
            {
                if (ClientExists(clientInput.Id))
                {
                    return(Conflict($"El cliente con identificación {clientInput.Id} ya se encuentra registrado."));
                }

                throw;
            }

            return(_mapper.Map <ClientViewModel>(client));
        }