public async Task <ActionResult> MakeAdmin(string id) { var employee = _userManager.FindByIdAsync(id).Result; var result = await _userManager.RemoveFromRoleAsync(employee, "Employee"); if (result.Succeeded) { var response = await _userManager.AddToRoleAsync(employee, "Administrator"); if (response.Succeeded) { var allocation = await _allocationRepo.GetLeaveAllocationsByEmployee(employee.Id); var requests = await _requestRepo.GetLeaveRequestByEmployee(employee.Id); if (allocation != null) { foreach (var item in allocation) { await _allocationRepo.Delete(item); } } if (requests != null) { foreach (var item in requests) { await _requestRepo.Delete(item); } } } else { ModelState.AddModelError("", "Failed to assign Administrator role"); return(RedirectToAction("Details", "LeaveAllocation", new { id = employee.Id })); } } else { ModelState.AddModelError("", "Failed to remove Employee role"); return(RedirectToAction("Details", "LeaveAllocation", new { id = employee.Id })); } return(RedirectToAction("ListEmployees", "LeaveAllocation")); }
// GET: LeaveTypesController/Delete/5 public async Task <ActionResult> Delete(int id) { if (!await _repo.isExists(id)) { return(NotFound()); } var leavetype = await _repo.FindById(id); if (leavetype == null) { return(NotFound()); } var isSuccess = await _repo.Delete(leavetype); if (!isSuccess) { return(BadRequest()); } var allocation = await _AlloRepo.FindAll(); if (allocation != null) { var AlloToDetete = allocation.ToList().Where(Q => Q.LeaveTypeId == id); foreach (var item in AlloToDetete) { await _AlloRepo.Delete(item); } } var request = await _requestRepo.FindAll(); if (allocation != null) { var requestToDetete = request.ToList().Where(Q => Q.LeaveTypeId == id); foreach (var item in requestToDetete) { await _requestRepo.Delete(item); } } return(RedirectToAction(nameof(Index))); }