-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
149 lines (129 loc) · 4.64 KB
/
Program.cs
File metadata and controls
149 lines (129 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using PayrollEngine.Client.Model;
using PayrollEngine.Client.QueryExpression;
using PayrollEngine.Client.Service.Api;
using Task = System.Threading.Tasks.Task;
namespace PayrollEngine.Client.Tutorial.BuildDataQuery;
/// <summary>The build data query tutorial program</summary>
internal class Program : ConsoleProgram<Program>
{
/// <inheritdoc />
protected override async Task RunAsync()
{
var tenant = await GetTenantAsync();
if (tenant == null)
{
return;
}
// all employees query
await QueryEmployeesAsync(tenant.Id, "All query", new());
// top 2 employees query
await QueryEmployeesAsync(tenant.Id, "Top 2 query", BuildTopQuery(2));
// employees erp filter query
var erpId = ConsoleArguments.Get(3);
if (!string.IsNullOrWhiteSpace(erpId))
{
await QueryEmployeesAsync(tenant.Id, $"Erp id query - {erpId}",
BuildErpFilterQuery(erpId));
}
// employees name query
var nameFilter = ConsoleArguments.Get(2);
if (!string.IsNullOrWhiteSpace(nameFilter))
{
await QueryEmployeesAsync(tenant.Id, $"Name query - {nameFilter}",
NameQuery(nameFilter));
}
PressAnyKey();
}
private static DivisionQuery BuildTopQuery(int? top = null) =>
new()
{
// order by first name descending
OrderBy = new OrderByDescending(nameof(Employee.FirstName)),
Top = top
};
private static DivisionQuery BuildErpFilterQuery(string erpId) =>
new()
{
// compare attribute value
Filter = string.IsNullOrWhiteSpace(erpId) ? null :
new Equals("ErpId".ToTextAttributeField(), erpId)
};
private static DivisionQuery NameQuery(string nameFilter) =>
new()
{
// active employees only
Filter = new ActiveStatus().And(
// employees created in 2019
new Equals(new Year(nameof(Employee.Created)), 2019).And(
// employees containing the search criteria in the first or last name
new Contains(nameof(Employee.FirstName), nameFilter).Or(
new Contains(nameof(Employee.LastName), nameFilter))))
};
private async Task QueryEmployeesAsync(int tenantId, string title, DivisionQuery query)
{
WriteTitleLine(title);
// receive employees with query
var employeeService = new EmployeeService(HttpClient);
var employees = await employeeService.QueryAsync<Employee>(new(tenantId), query);
DisplayEmployees(employees);
}
private async Task<Tenant> GetTenantAsync()
{
// tenant argument
var tenantIdentifier = ConsoleArguments.Get(1);
if (string.IsNullOrWhiteSpace(tenantIdentifier))
{
WriteErrorLine("Missing tenant identifier.");
PressAnyKey();
return null;
}
// tenant request
var tenantService = new TenantService(HttpClient);
var tenant = await tenantService.GetAsync<Tenant>(new(), tenantIdentifier);
if (tenant == null)
{
WriteErrorLine($"Invalid tenant identifier {tenantIdentifier}.");
PressAnyKey();
return null;
}
return tenant;
}
#region Output
private static void DisplayEmployees(List<Employee> employees)
{
if (employees.Any())
{
// table
var line = new string('-', 15 + 15 + 15 + 25 + 10);
WriteLine(line);
WriteLine($"{"Created",-15}{"First name",-15}{"Last name",-15}{"Identifier",-25}{"Id",-10}");
WriteLine(line);
foreach (var employee in employees)
{
WriteLine($"{employee.Created.ToCompactString(),-15}" +
$"{employee.FirstName,-15}{employee.LastName,-15}" +
$"{employee.Identifier,-25}{employee.Id,-10}");
}
WriteLine(line);
WriteInfoLine($"Total {employees.Count} employees");
}
else
{
WriteInfoLine("No employees found");
}
WriteLine();
}
#endregion
/// <summary>Program entry point</summary>
static async Task Main()
{
// init logger
Log.SetLogger(new Serilog.PayrollLog());
// execute program
using var program = new Program();
await program.ExecuteAsync();
}
}