-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmployeesController.cs
More file actions
271 lines (221 loc) · 8.89 KB
/
EmployeesController.cs
File metadata and controls
271 lines (221 loc) · 8.89 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
using Microsoft.AspNetCore.Mvc;
using System.Data;
using System.Text.Json.Serialization;
namespace YourApp.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class EmployeesController : ControllerBase
{
private readonly IEmployeeDataService _employeeDataService;
public EmployeesController(IEmployeeDataService employeeDataService)
{
_employeeDataService = employeeDataService;
}
[HttpPost("serverside")]
public async Task<IActionResult> GetServerSideData([FromBody] ServerSideRequest request)
{
try
{
var response = await _employeeDataService.GetServerSideDataAsync(request);
return Ok(response);
}
catch (Exception ex)
{
return StatusCode(500, new { error = ex.Message });
}
}
}
// Request/Response DTOs
public class ServerSideRequest
{
public int StartRow { get; set; }
public int EndRow { get; set; }
public List<SortModel> SortModel { get; set; } = new();
public Dictionary<string, object> FilterModel { get; set; } = new();
public List<string> GroupKeys { get; set; } = new();
}
public class SortModel
{
public string ColId { get; set; } = string.Empty;
public string Sort { get; set; } = string.Empty; // "asc" or "desc"
}
public class ServerSideResponse
{
public List<Dictionary<string, object>> Data { get; set; } = new();
public int TotalRows { get; set; }
}
// Data Service Interface
public interface IEmployeeDataService
{
Task<ServerSideResponse> GetServerSideDataAsync(ServerSideRequest request);
Task<DataTable> GetEmployeeDataTableAsync();
}
// Data Service Implementation
public class EmployeeDataService : IEmployeeDataService
{
private readonly IConfiguration _configuration;
private readonly string _connectionString;
public EmployeeDataService(IConfiguration configuration)
{
_configuration = configuration;
_connectionString = _configuration.GetConnectionString("DefaultConnection") ?? "";
}
public async Task<ServerSideResponse> GetServerSideDataAsync(ServerSideRequest request)
{
var dataTable = await GetEmployeeDataTableAsync();
// Apply filters
var filteredTable = ApplyFilters(dataTable, request.FilterModel);
// Apply sorting
var sortedTable = ApplySorting(filteredTable, request.SortModel);
// Get total count after filtering
var totalRows = sortedTable.Rows.Count;
// Apply paging
var pagedData = ApplyPaging(sortedTable, request.StartRow, request.EndRow);
// Convert DataTable to List of dictionaries
var data = ConvertDataTableToList(pagedData);
return new ServerSideResponse
{
Data = data,
TotalRows = totalRows
};
}
public async Task<DataTable> GetEmployeeDataTableAsync()
{
// Create sample DataTable - replace with your actual data source
var dataTable = new DataTable();
// Define columns
dataTable.Columns.Add("id", typeof(int));
dataTable.Columns.Add("name", typeof(string));
dataTable.Columns.Add("email", typeof(string));
dataTable.Columns.Add("department", typeof(string));
dataTable.Columns.Add("salary", typeof(decimal));
dataTable.Columns.Add("joinDate", typeof(DateTime));
dataTable.Columns.Add("isActive", typeof(bool));
// Add sample data - replace with your actual data loading logic
await Task.Run(() =>
{
var random = new Random();
var departments = new[] { "Engineering", "Sales", "Marketing", "HR", "Finance" };
var names = new[] { "John Smith", "Jane Doe", "Bob Johnson", "Alice Brown", "Charlie Wilson",
"Diana Davis", "Frank Miller", "Grace Lee", "Henry Taylor", "Ivy Chen" };
for (int i = 1; i <= 10000; i++) // Large dataset for testing
{
var name = names[random.Next(names.Length)];
var email = $"{name.Replace(" ", ".").ToLower()}@company.com";
var department = departments[random.Next(departments.Length)];
var salary = random.Next(40000, 150000);
var joinDate = DateTime.Now.AddDays(-random.Next(1, 3650));
var isActive = random.Next(0, 2) == 1;
dataTable.Rows.Add(i, name, email, department, salary, joinDate, isActive);
}
});
return dataTable;
}
private DataTable ApplyFilters(DataTable dataTable, Dictionary<string, object> filterModel)
{
if (filterModel == null || !filterModel.Any())
return dataTable;
var filteredTable = dataTable.Clone();
foreach (DataRow row in dataTable.Rows)
{
bool includeRow = true;
foreach (var filter in filterModel)
{
var columnName = filter.Key;
var filterValue = filter.Value;
if (!dataTable.Columns.Contains(columnName))
continue;
// Simple text filter implementation
if (filterValue is string textFilter && !string.IsNullOrEmpty(textFilter))
{
var cellValue = row[columnName]?.ToString() ?? "";
if (!cellValue.Contains(textFilter, StringComparison.OrdinalIgnoreCase))
{
includeRow = false;
break;
}
}
}
if (includeRow)
filteredTable.ImportRow(row);
}
return filteredTable;
}
private DataTable ApplySorting(DataTable dataTable, List<SortModel> sortModel)
{
if (sortModel == null || !sortModel.Any())
return dataTable;
var sortExpressions = sortModel
.Where(s => dataTable.Columns.Contains(s.ColId))
.Select(s => $"{s.ColId} {(s.Sort.ToUpper() == "DESC" ? "DESC" : "ASC")}")
.ToArray();
if (sortExpressions.Any())
{
var sortedRows = dataTable.Select("", string.Join(", ", sortExpressions));
var sortedTable = dataTable.Clone();
foreach (var row in sortedRows)
sortedTable.ImportRow(row);
return sortedTable;
}
return dataTable;
}
private DataTable ApplyPaging(DataTable dataTable, int startRow, int endRow)
{
var pagedTable = dataTable.Clone();
var pageSize = endRow - startRow;
for (int i = startRow; i < Math.Min(endRow, dataTable.Rows.Count); i++)
{
pagedTable.ImportRow(dataTable.Rows[i]);
}
return pagedTable;
}
private List<Dictionary<string, object>> ConvertDataTableToList(DataTable dataTable)
{
var list = new List<Dictionary<string, object>>();
foreach (DataRow row in dataTable.Rows)
{
var dict = new Dictionary<string, object>();
foreach (DataColumn column in dataTable.Columns)
{
var value = row[column];
dict[column.ColumnName] = value == DBNull.Value ? null : value;
}
list.Add(dict);
}
return list;
}
}
}
// Startup.cs or Program.cs configuration
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddHttpClient();
// Register the data service
services.AddScoped<IEmployeeDataService, EmployeeDataService>();
// Configure CORS if needed
services.AddCors(options =>
{
options.AddDefaultPolicy(builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseCors();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}