private static async Task BuildAdvancedOrganization(IServiceProvider provider) { var ptoPolicyController = provider.GetRequiredService <PaidTimeOffPolicyController>(); var policies = (await ptoPolicyController.GetAllPtoPoliciesAsync()).Payload; var employeeController = provider.GetRequiredService <EmployeeController>(); var mapper = provider.GetRequiredService <IViewModelToDomainEntityMapper <RegisterOrUpdateEmployeeCommand, Employee> >(); Console.WriteLine("Simulating HR personnel creating employees in the UI..."); // Generate organization N levels deep, with multiple top-level employees. for (int n = 0; n < advancedOrgNumTrees; n++) { var boss = NewEmployeeGenerator.GenerateEmployee(advancedOrgDepth); foreach (var employee in FlattenOrganization(boss)) { Console.WriteLine($"Registering new employee: {employee.FirstName} {employee.LastName}."); var employeeVm = mapper.MapToViewModel(employee); var selectedPtoPolicy = policies.Where(p => p.EmployeeLevel == employee.EmployeeLevel).RandomSubset(1).First(); employeeVm.PaidTimeOffPolicyId = selectedPtoPolicy.Id; if (!selectedPtoPolicy.AllowsUnlimitedPto) { employeeVm.PtoHoursRemaining = 0.0m; } var registerEmployeeResult = await employeeController.RegisterOrUpdateEmployeeAsync(employeeVm); if (registerEmployeeResult.Succeeded) { Console.WriteLine("Got success result from controller."); } else { Console.WriteLine($@"Failed with message: ""{string.Join(" ", registerEmployeeResult.Errors)}"""); break; } employee.Id = registerEmployeeResult.Payload.Id; } } Console.WriteLine("Done registering employees. Verifying entire organization and getting stats..."); var verifyOrgResult = await employeeController.VerifyOrganizationAsync(); if (verifyOrgResult.Succeeded) { Console.WriteLine("Got success result from controller."); var stats = verifyOrgResult.Payload.Stats; var sb = new StringBuilder().AppendLine($"{stats.Count} organizational structures analyzed."); foreach (var stat in stats) { sb.AppendLine($"Num Employees: {stat.NumEmployees}, Org Complexity: {stat.OrgComplexity}"); } sb.AppendLine($"Total Employees: {stats.Sum(s => s.NumEmployees)}, Total Complexity: {stats.Sum(s => s.OrgComplexity)}"); Console.WriteLine(sb.ToString()); } else { Console.WriteLine($@"Failed with message: ""{string.Join(" ", verifyOrgResult.Errors)}"""); } Console.WriteLine("Querying the API to get complete org structure..."); var orgChartResult = await employeeController.GetEmployeeOrgChartAsync(); if (orgChartResult.Succeeded) { Console.WriteLine("Got success result from controller. Here is the org chart:"); Console.WriteLine(string.Join("\r\n\r\n", from e in orgChartResult.Payload select GetPrintableOrgTree(e).ToString())); } else { Console.WriteLine($@"Failed with message: ""{string.Join(" ", orgChartResult.Errors)}"""); return; } }
private static async Task BuildBasicOrganization(IServiceProvider provider) { // *** Simulate data hitting the web API from the UI. *** // Pretend we are ASP.NET Core resolving the controller. var ptoPolicyController = provider.GetRequiredService <PaidTimeOffPolicyController>(); Console.WriteLine("Querying PTO policies. This should cause them to be added to cache."); _ = await ptoPolicyController.GetAllPtoPoliciesAsync(); Console.WriteLine("Querying PTO policies again to test the cache. Here they are:"); var policies = (await ptoPolicyController.GetAllPtoPoliciesAsync()).Payload; foreach (var policy in policies) { Console.WriteLine(policy); } var employeeController = provider.GetRequiredService <EmployeeController>(); var mapper = provider.GetRequiredService <IViewModelToDomainEntityMapper <RegisterOrUpdateEmployeeCommand, Employee> >(); Console.WriteLine("Simulating HR personnel creating employees in the UI..."); // Generate a fake organization N levels deep. var boss = NewEmployeeGenerator.GenerateEmployee(basicOrgDepth); foreach (var employee in FlattenOrganization(boss)) { Console.WriteLine($"Registering new employee: {employee.FirstName} {employee.LastName}."); var employeeVm = mapper.MapToViewModel(employee); var selectedPtoPolicy = policies.Where(p => p.EmployeeLevel == employee.EmployeeLevel).RandomSubset(1).First(); employeeVm.PaidTimeOffPolicyId = selectedPtoPolicy.Id; if (!selectedPtoPolicy.AllowsUnlimitedPto) { employeeVm.PtoHoursRemaining = 0.0m; } var registerEmployeeResult = await employeeController.RegisterOrUpdateEmployeeAsync(employeeVm); if (registerEmployeeResult.Succeeded) { Console.WriteLine("Got success result from controller."); } else { Console.WriteLine($@"Failed with message: ""{string.Join(" ", registerEmployeeResult.Errors)}"""); break; } employee.Id = registerEmployeeResult.Payload.Id; } Console.WriteLine("Done registering employees. Getting full employee list..."); var employeeListResult = await employeeController.GetEmployeeListAsync(); if (employeeListResult.Succeeded) { Console.WriteLine("Got success result from controller. Here they are:"); Console.WriteLine("Id Name Gender Level Num.Subordinates Manager Name"); Console.WriteLine("------------------------------------------------------------------------------------------------"); foreach (var emp in employeeListResult.Payload) { Console.WriteLine($"{emp.Id,-3} {(emp.LastName + ", " + emp.FirstName + " " + emp.MiddleName).Trim(',', ' '),-30} {emp.Gender,-6} {emp.EmployeeLevel,-2} {emp.NumSubordinates,-7} {(emp.ManagerLastName + ", " + emp.ManagerFirstName + " " + emp.ManagerMiddleName).Trim(',', ' '),-30}"); } } else { Console.WriteLine($@"Failed with message: ""{string.Join(" ", employeeListResult.Errors)}"""); return; } Console.WriteLine("Querying the API again to get details of a random employee..."); var empId = employeeListResult.Payload.RandomSubset(1).First().Id; var employeeDetailResult = await employeeController.GetEmployeeDetailsAsync(empId); if (employeeDetailResult.Succeeded) { Console.WriteLine("Got success result from controller. Here are the employee details:"); Console.WriteLine(employeeDetailResult.Payload); } else { Console.WriteLine($@"Failed with message: ""{string.Join(" ", employeeDetailResult.Errors)}"""); return; } Console.WriteLine("Querying the API to get complete org structure..."); var orgChartResult = await employeeController.GetEmployeeOrgChartAsync(); if (orgChartResult.Succeeded) { Console.WriteLine("Got success result from controller. Here is the org chart:"); Console.WriteLine(string.Join("\r\n\r\n", from e in orgChartResult.Payload select GetPrintableOrgTree(e).ToString())); } else { Console.WriteLine($@"Failed with message: ""{string.Join(" ", orgChartResult.Errors)}"""); return; } }