//public async Task<IActionResult> Create([Bind("Name,SupervisorId,Id,TimeStamp")] Team team) public async Task <IActionResult> CreateAsync([Bind("Name,SupervisorId,Id,TimeStamp")] Team team) { if (string.IsNullOrEmpty(team.SupervisorId.ToString())) { ApplicationUser user = await _userManager.FindByNameAsync(User.Identity.Name); _supervisorRepo.Add(new Supervisor { EmployeeId = user.Id, User = user }); _supervisorRepo.SaveChanges(); team.SupervisorId = _supervisorRepo.GetAll().Where(x => x.EmployeeId == user.Id).FirstOrDefault().Id; } if (ModelState.IsValid) { //team.Id = Guid.NewGuid(); _teamRepo.Add(team); _teamRepo.SaveChanges(); //_context.Add(team); //await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } //ViewData["SupervisorId"] = new SelectList(_context.Set<Supervisor>(), "Id", "Id", team.SupervisorId); ViewData["SupervisorId"] = new SelectList(_supervisorRepo.GetAll().ToHashSet <Supervisor>(), "Id", "Id", team.SupervisorId); return(View(team)); }
public async Task <IActionResult> Edit(DepartmentEditViewModel model) { if (ModelState.IsValid) { Supervisor supervisor = _supervisorRepo.GetAll().Where(x => x.EmployeeId == model.NewSuperVisorEmployeeId).FirstOrDefault(); Department department = _departmentRepo.GetAllWithUsers().Where(x => x.Id == model.DepartmentId).FirstOrDefault(); if (supervisor == null && model.NewSuperVisorEmployeeId != null && !model.NewSuperVisorEmployeeId.Equals("")) { List <Department> departments = new List <Department> { department }; if (_userManager.FindByIdAsync(model.NewSuperVisorEmployeeId) == null) { throw new Exception("Department Controller could not find user with id of " + model.NewSuperVisorEmployeeId); } _supervisorRepo.Add(new Supervisor { EmployeeId = model.NewSuperVisorEmployeeId }); _supervisorRepo.SaveChanges(); Supervisor SavedSupervisor = _supervisorRepo.GetAll().Where(x => x.EmployeeId == model.NewSuperVisorEmployeeId).FirstOrDefault(); department.SupervisorId = SavedSupervisor.Id; _departmentRepo.SaveChanges(); } else if (supervisor != null) { // supervisor.Departments.Add(department); department.SupervisorId = supervisor.Id; _supervisorRepo.SaveChanges(); } foreach (string userId in model.IdsToAdd ?? new string[] { }) { ApplicationUser user = await _userManager.FindByIdAsync(userId); if (user != null) { //department.AssignedEmployees.Add(user); user.DepartmentId = department.Id; IdentityResult result = await _userManager.UpdateAsync(user); //result = _departmentRepo.SaveChanges(); // result = _departmentRepo.AddToDepartment(user, model.DepartmentId);//_.AddToRoleAsync(user, model.RoleName); if (!result.Succeeded) { AddErrorsFromResult(result); } } if (ModelState.IsValid) { //_departmentRepo.Add(department); _departmentRepo.SaveChanges(); //return RedirectToAction("Index"); } } foreach (string userId in model.IdsToDelete ?? new string[] { }) { ApplicationUser user = await _userManager.FindByIdAsync(userId); if (user != null) { //department.AssignedEmployees.Remove(user); user.DepartmentId = null; if (supervisor != null) { if (user.Id == supervisor.EmployeeId) { Supervisor newSupervisor = new Supervisor { EmployeeId = department.AssignedEmployees.FirstOrDefault().Id }; _supervisorRepo.SaveChanges(); department.SupervisorId = newSupervisor.Id; } IdentityResult result = await _userManager.UpdateAsync(user); //result = _departmentRepo.RemoveFromDepartment(user, model.DepartmentId);//_.RemoveFromRoleAsync(user, model.RoleName); if (!result.Succeeded) { AddErrorsFromResult(result); } else { } } } } } if (ModelState.IsValid) { return(RedirectToAction(nameof(Index))); } else { return(Edit(model.DepartmentId)); } }