public async Task <IActionResult> RegisterEmployer(RegisterEmployerViewmodel model)
        {
            if (ModelState.IsValid)
            {
                var user = new IdentityUser()
                {
                    UserName = model.User.Email,
                    Email    = model.User.Email
                };
                var result = await _userManager.CreateAsync(user, model.User.Password);

                if (result.Succeeded)
                {
                    await _userManager.AddToRoleAsync(user, "Employer");

                    var addToCompanyResult = _companyHandler.AddEmployee(user.Id, _companyHandler.GetById(model.CompanyId));
                    if (addToCompanyResult)
                    {
                        return(RedirectToAction("ListCompanies", "Company"));
                    }
                }

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }
            }

            return(View(model));
        }
Beispiel #2
0
        public async Task <IActionResult> EditUsersInCompany(List <UserRoleViewModel> model, string companyId)
        {
            var company = ModelConverter.ConvertCompanyDtoToModel(_companyHandler.GetById(companyId));

            if (company == null)
            {
                throw new Exception("Company was not found");
            }

            for (int i = 0; i < model.Count; i++)
            {
                var user = await _userManager.FindByIdAsync(model[i].UserId);

                bool result = false;


                if (model[i].IsSelected && !_companyHandler.CheckIfEmployeeInCompany(user.Id, companyId))
                {
                    result = _companyHandler.AddEmployee(user.Id, ModelConverter.ConvertModelToCompanyDto(company));
                }
                else if (!model[i].IsSelected && _companyHandler.CheckIfEmployeeInCompany(user.Id, companyId))
                {
                    result = _companyHandler.RemoveEmployee(user.Id);
                }
                else
                {
                    continue;
                }

                if (result)
                {
                    if (i < (model.Count - 1))
                    {
                        continue;
                    }
                    else
                    {
                        return(RedirectToAction("CompanyDetails", new { companyId }));
                    }
                }
            }

            return(RedirectToAction("CompanyDetails", new { companyId }));
        }