/// <summary> /// Recursively collects a table of all devices associated with the employees in the specified tree. /// </summary> /// <param name="tree"></param> /// <param name="table"></param> private DataTable GetDevicesTable(EmployeeTree tree, DataTable table = null) { if (table == null) { table = new DataTable("devices"); } string query = Queries.SelectDevicesByEmpNum(tree.Employee.Number); using (var results = DBFactory.GetDatabase().DataTableFromQueryString(query)) { if (results.Rows.Count > 0) { table.Merge(results); } } if (tree.Subordinates.Count > 0) { foreach (var sub in tree.Subordinates) { table.Merge(GetDevicesTable(sub, table)); } } return(table); }
public async Task <EmployeeTree> CreateEmployee(EmployeeTree employee) { context.Employees.Add(employee); await context.SaveChangesAsync(); return(employee); }
public void AddDirectReport_WhenAddingAnEmployeeAsADirectReport_AddsToListOfDirectReports() { var employeeTree = new EmployeeTree(1, "Amy Reid"); employeeTree.AddDirectReport(2, "Sachin Kainth"); employeeTree.DirectReports.Count.Should().Be(1); employeeTree.DirectReports[0].Id.Should().Be(2); employeeTree.DirectReports[0].Name.Should().Be("Sachin Kainth"); }
public async void StartSearch() { try { // Prompt user for supervisor. var supervisorMunis = MunisFunctions.MunisUserSearch(this); // Make sure we have an employee number to work with. if (string.IsNullOrEmpty(supervisorMunis.Number)) { return; } var supervisorThis = new Employee(supervisorMunis.Name, supervisorMunis.Number); currentSupervisor = supervisorThis; // Setup form and display working spinner. this.Text += " for " + supervisorThis.Name; this.Show(); workingSpinner.Visible = true; // Populate the employee list. employeeList = await GetEmployeeList(); // Make sure the form hasn't been disposed. if (!this.IsDisposed) { // Build employee hierarchy tree. var empTree = GetSubordinates(supervisorThis); currentTree = empTree; // Build node tree from the employee tree. var nodeTree = new TreeNode(empTree.Employee.Name); nodeTree.Tag = empTree.Employee.Number; BuildTree(empTree, nodeTree); // Set the tree view control to the node tree. HierarchyTree.Nodes.Add(nodeTree); HierarchyTree.Nodes[0].Expand(); // Report completion. statusLabel.Text = employeesFound + " employees found."; } } catch (Exception ex) { ErrorHandling.ErrHandle(ex, System.Reflection.MethodBase.GetCurrentMethod()); } finally { // Stop the spinner and remove the big employee list from scope. workingSpinner.Visible = false; employeeList?.Clear(); employeeList = null; } }
public async Task <EmployeeTree> UpdateEmployee(EmployeeTree employee) { try { context.Employees.Update(employee); await context.SaveChangesAsync(); } catch (Exception ex) { throw ex; } return(employee); }
public async Task <IActionResult> Edit(EmployeeTree employeeTree) { try { await employeeService.UpdateEmployee(employeeTree); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } } catch (Exception) { return(NotFound()); } return(View(employeeTree)); }
private void GetEmployeeChildTree(EmployeeTree employee) { var employeeReporters = context.Employees .Where(x => x.ManagerId == employee.Id).ToList(); if (employeeReporters.Count() > 0) { foreach (var reportersNode in employeeReporters) { if (employee.Reporters == null) { employee.Reporters = new List <EmployeeTree>(); employee.Reporters.Add(reportersNode); } GetEmployeeChildTree(reportersNode); employee.Reporters.Add(reportersNode); } } }
/// <summary> /// Recursively searches data for specified employee/supervisor and builds a hierarchy tree. /// </summary> /// <param name="employee"></param> /// <param name="tree"></param> /// <returns></returns> private EmployeeTree GetSubordinates(Employee employee, EmployeeTree tree = null) { if (this.IsDisposed) { return(null); } // Start a new tree on first recurse. if (tree == null) { tree = new EmployeeTree(employee); } // Search the list of employees for matching supervisors. var results = employeeList.FindAll(e => e.SupervisorId == employee.Number); if (results.Count > 0) { employeesFound += results.Count; foreach (var emp in results) { // Make sure employee is not set as their own supervisor to prevent endless loops. if (emp.Number != employee.Number) { tree.Subordinates.Add(new EmployeeTree(new Employee(emp.Name, emp.Number))); statusLabel.Text = "Searching... Found " + employeesFound; } } } // Recurse with subordinates. if (tree.Subordinates.Count > 0) { foreach (var sub in tree.Subordinates) { GetSubordinates(sub.Employee, sub); } } return(tree); }
/// <summary> /// Recursively builds a node tree from the employee tree. /// </summary> /// <param name="tree"></param> /// <param name="parentNode"></param> private void BuildTree(EmployeeTree tree, TreeNode parentNode = null) { if (parentNode == null) { parentNode = new TreeNode(tree.Employee.Name); parentNode.Tag = tree.Employee.Number; } foreach (var sub in tree.Subordinates) { // Add child nodes to parent node. var childNode = new TreeNode(sub.Employee.Name); childNode.Tag = sub.Employee.Number; parentNode.Nodes.Add(childNode); // If sub tree has subordinates, resurse with them. if (sub.Subordinates.Count > 0) { BuildTree(sub, childNode); } } }