public async Task <IActionResult> IndexAsync(IndexViewModel indexInputModel) { if (ModelState.IsValid) { var userId = userManager.GetUserId(this.User); var companyCode = indexInputModel.CompanyCode; if (!companyService.IsValidCompany(companyCode)) { ModelState.AddModelError("CompanyCode", "Company code is not valid"); return(View(indexInputModel)); } else { var companyId = companyService.FindByCompanyCode(companyCode).Id; var addCompanyToEmployeeServiceModel = new AddCompanyToEmployeeServiceModel() { EmployeeId = userId, CompanyId = companyId }; employeeService.AddCompanyToEmployee(addCompanyToEmployeeServiceModel); string roleName = "Employee"; bool roleExists = await roleManager.RoleExistsAsync(roleName); if (!roleExists) { var role = new IdentityRole(); role.Name = roleName; await roleManager.CreateAsync(role); } var employeeId = userManager.GetUserId(this.User); var user = await employeeService.FindById(employeeId); await userManager.AddToRoleAsync(user, "Employee"); } return(RedirectToAction("Index", "Home")); } return(View(indexInputModel)); }
public async Task <IActionResult> CompanyCreationAsync(CompanyCreationInputModel companyCreationInputModel) { if (ModelState.IsValid) { if (comapnyService.Exists(companyCreationInputModel.Name)) { ModelState.AddModelError("Name", "Name already exists"); return(View(companyCreationInputModel)); } else { var createCompanyServiceModel = new CreateCompanyServiceModel() { Name = companyCreationInputModel.Name, Description = companyCreationInputModel.Description }; comapnyService.Create(createCompanyServiceModel); var addCompanyToEmployeeServiceModel = new AddCompanyToEmployeeServiceModel() { CompanyId = comapnyService.FindByName(companyCreationInputModel.Name).Id, EmployeeId = userManager.GetUserId(this.User) }; employeeDataService.AddCompanyToEmployee(addCompanyToEmployeeServiceModel); var thisUser = this.User; jobTitleService.AddJobTitle(companyCreationInputModel.BossTitle, thisUser); await employeeService.AddManagerRole(thisUser); return(this.RedirectToAction("Index", "Home")); } } return(View(companyCreationInputModel)); }