-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
241 lines (212 loc) · 8.02 KB
/
Program.cs
File metadata and controls
241 lines (212 loc) · 8.02 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using AutoMapper;
using Microsoft.Extensions.Logging;
using PayrollEngine.Client.Model;
using PayrollEngine.Client.Service.Api;
using System.Collections.Generic;
using System.Linq;
using Tasks = System.Threading.Tasks;
namespace PayrollEngine.Client.Tutorial.ExtendedObjectModel;
/// <summary>Extended object model tutorial program</summary>
internal class Program : ConsoleProgram<Program>
{
/// <inheritdoc />
protected override async Tasks.Task RunAsync()
{
// tenant
var tenant = await GetTenantAsync(ConsoleArguments.Get(1));
if (tenant == null)
{
return;
}
// employees
var employees = await GetEmployeesAsync(tenant.Id);
if (employees.Any())
{
DisplayEmployees(tenant, employees);
WriteLine();
}
else
{
WriteInfoLine($"Missing employees for tenant {tenant.Identifier}");
}
// activities
var activities = await GetActivitiesAsync(tenant.Id);
if (activities.Any())
{
DisplayActivities(tenant, activities);
WriteLine();
}
else
{
WriteInfoLine($"Missing activities for tenant {tenant.Identifier}");
}
// employee details
var employeeErpId = ConsoleArguments.Get(2);
if (!string.IsNullOrWhiteSpace(employeeErpId))
{
var employee = await GetEmployeeAsync(tenant.Id, employeeErpId);
if (employee != null)
{
DisplayEmployee(tenant, employee);
}
else
{
WriteInfoLine($"Unknown employee with Erp id {employeeErpId}");
}
}
PressAnyKey();
}
#region Domain
/// <summary>Get employee by Erp id</summary>
/// <param name="tenantId">The tenant id</param>
/// <param name="employeeErpId">The employee Erp id</param>
private async Tasks.Task<MyEmployee> GetEmployeeAsync(int tenantId, string employeeErpId) =>
(await new EmployeeService(HttpClient).QueryAsync<MyEmployee>(new(tenantId), new()
{
// Erp id
Filter = $"TA_ErpId eq '{employeeErpId}'",
// active only
Status = ObjectStatus.Active
})).FirstOrDefault();
/// <summary>Get active employees</summary>
/// <param name="tenantId">The tenant id</param>
private async Tasks.Task<List<MyEmployee>> GetEmployeesAsync(int tenantId) =>
await new EmployeeService(HttpClient).QueryAsync<MyEmployee>(new(tenantId), new()
{
// active only
Status = ObjectStatus.Active
});
/// <summary>Get active tasks</summary>
/// <param name="tenantId">The tenant id</param>
private async Tasks.Task<List<Activity>> GetActivitiesAsync(int tenantId) =>
(await new TaskService(HttpClient).QueryAsync<Task>(new(tenantId), new()
{
// active only
Status = ObjectStatus.Active
})).Select(MapTaskToActivity).ToList();
/// <summary>Map a payroll task to a custom activity</summary>
/// <param name="task">The payroll task</param>
/// <returns>The custom activity</returns>
private static Activity MapTaskToActivity(Task task)
{
// logger
var loggerFactory = LoggerFactory.Create(builder => { builder.AddConsole(); });
// mapper configuration
var config = new MapperConfiguration(cfg =>
cfg.CreateMap<Task, Activity>()
.ForMember(dest => dest.ActivityId, act => act.MapFrom(
src => src.GetAttributeGuid("ErpId")))
.ForMember(dest => dest.State, act => act.MapFrom(
src => src.Completed.HasValue ? ActivityStateCode.Completed : ActivityStateCode.Scheduled)),
loggerFactory);
// map task to activity
var mapper = new Mapper(config);
var activity = mapper.Map<Activity>(task);
return activity;
}
/// <summary>Get tenant by console argument</summary>
/// <param name="tenantIdentifier">The tenant identifier</param>
private async Tasks.Task<Tenant> GetTenantAsync(string tenantIdentifier)
{
// tenant argument
if (string.IsNullOrWhiteSpace(tenantIdentifier))
{
WriteErrorLine("Missing argument tenant identifier.");
PressAnyKey();
return null;
}
// tenant request
var tenant = await new TenantService(HttpClient).GetAsync<Tenant>(new(), tenantIdentifier);
if (tenant == null)
{
WriteErrorLine($"Invalid tenant identifier {tenantIdentifier}.");
PressAnyKey();
return null;
}
return tenant;
}
#endregion
#region Output
/// <summary>Display the activities</summary>
/// <param name="tenant">The tenant</param>
/// <param name="activities">The activities</param>
private static void DisplayActivities(Tenant tenant, List<Activity> activities)
{
// title
WriteTitleLine($"{tenant.Identifier}: Activities");
// table
var line = new string('-', 25 + 20 + 40);
WriteLine(line);
WriteLine($"{"Name",-25}{"State",-20}{"Activity Id",-40}");
WriteLine(line);
foreach (var activity in activities)
{
WriteLine($"{activity.Name,-25}{activity.State,-20}{activity.ActivityId}");
}
WriteLine(line);
}
/// <summary>Display the employees</summary>
/// <param name="tenant">The tenant</param>
/// <param name="employee">The employee</param>
private static void DisplayEmployee(Tenant tenant, MyEmployee employee)
{
// title
WriteTitleLine($"{tenant.Identifier}: employee {employee.Identifier}");
// table
var line = new string('-', 25 + 40);
WriteLine(line);
WriteLine($"{"Attribute",-25}{"Value",-40}");
WriteLine(line);
WriteLine("Identifier".PadRight(25) + employee.Identifier);
WriteLine("FirstName".PadRight(25) + employee.FirstName);
WriteLine("LastName".PadRight(25) + employee.LastName);
WriteLine("Created".PadRight(25) + employee.Created);
WriteLine("Updated".PadRight(25) + employee.Updated);
WriteLine("Status".PadRight(25) + employee.Status);
WriteLine("Id".PadRight(25) + employee.Id);
WriteLine("ErpId".PadRight(25) + employee.ErpId);
WriteLine(line);
}
/// <summary>Display the employees</summary>
/// <param name="tenant">The tenant</param>
/// <param name="employees">The employees</param>
private static void DisplayEmployees(Tenant tenant, List<MyEmployee> employees)
{
// title
WriteTitleLine($"{tenant.Identifier}: Employees");
// table
var line = new string('-', 25 + 20 + 20 + 15 + 40);
WriteLine(line);
WriteLine($"{"Identifier",-25}{"First name",-20}{"Last name",-20}{"Payroll Id",-15}{"Erp Id",-40}");
WriteLine(line);
foreach (var employee in employees)
{
WriteLine($"{employee.Identifier,-25}{employee.FirstName,-20}{employee.LastName,-20}{employee.Id,-15}{employee.ErpId,-40}");
}
WriteLine(line);
}
/// <inheritdoc />
protected override Tasks.Task HelpAsync()
{
WriteLine("Usage: ExtendedObjectModel Tenant [EmployeeErpId]");
WriteLine();
WriteLine("Arguments:");
WriteLine(" 1. Tenant identifier");
WriteLine(" 2. Employee Erp Guid (optional)");
WriteLine();
WriteLine("Examples:");
WriteLine(" ExtendedObjectModel MyTenant");
WriteLine(" ExtendedObjectModel MyTenant f37d1b9b-944f-44fa-bf4c-61f4ee502205");
return Tasks.Task.CompletedTask;
}
#endregion
/// <summary>Program entry point</summary>
static async Tasks.Task Main()
{
// init logger
Log.SetLogger(new Serilog.PayrollLog());
// execute program
using var program = new Program();
await program.ExecuteAsync();
}
}