-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridController.cs
More file actions
266 lines (234 loc) · 12.2 KB
/
GridController.cs
File metadata and controls
266 lines (234 loc) · 12.2 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
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;
using System.Data;
using Syncfusion.EJ2.Base;
using Microsoft.Data.SqlClient;
namespace CustomAdaptor_MSSQL.Server.Controllers
{
[ApiController]
public class GridController : ControllerBase
{
string ConnectionString = @"<Enter a valid connection string>";
/// <summary>
/// Processes the DataManager request to perform searching, filtering, sorting, and paging operations.
/// </summary>
/// <param name="DataManagerRequest">Contains the details of the data operation requested.</param>
/// <returns>Returns a JSON object with the filtered, sorted, and paginated data along with the total record count.</returns>
[HttpPost]
[Route("api/[controller]")]
public object Post([FromBody] DataManagerRequest DataManagerRequest)
{
// Retrieve data from the data source (e.g., database).
IQueryable<Orders> DataSource = GetOrderData().AsQueryable();
// Initialize QueryableOperation instance.
QueryableOperation queryableOperation = new QueryableOperation();
// Handling searching operation.
if (DataManagerRequest.Search != null && DataManagerRequest.Search.Count > 0)
{
DataSource = queryableOperation.PerformSearching(DataSource, DataManagerRequest.Search);
// Add custom logic here if needed and remove above method.
}
// Handling filtering operation.
if (DataManagerRequest.Where != null && DataManagerRequest.Where.Count > 0)
{
foreach (WhereFilter condition in DataManagerRequest.Where)
{
foreach (WhereFilter predicate in condition.predicates)
{
DataSource = queryableOperation.PerformFiltering(DataSource, DataManagerRequest.Where, predicate.Operator);
// Add custom logic here if needed and remove above method.
}
}
}
// Handling sorting operation.
if (DataManagerRequest.Sorted != null && DataManagerRequest.Sorted.Count > 0)
{
DataSource = queryableOperation.PerformSorting(DataSource, DataManagerRequest.Sorted);
// Add custom logic here if needed and remove above method.
}
// Get the total count of records.
int totalRecordsCount = DataSource.Count();
// Handling paging operation.
if (DataManagerRequest.Skip != 0)
{
DataSource = queryableOperation.PerformSkip(DataSource, DataManagerRequest.Skip);
// Add custom logic here if needed and remove above method.
}
if (DataManagerRequest.Take != 0)
{
DataSource = queryableOperation.PerformTake(DataSource, DataManagerRequest.Take);
// Add custom logic here if needed and remove above method.
}
// Return data based on the request.
return new { result = DataSource, count = totalRecordsCount };
}
/// <summary>
/// Retrieves the order data from the database.
/// </summary>
/// <returns>Returns a list of orders fetched from the database.</returns>
[HttpGet]
[Route("api/[controller]")]
public List<Orders> GetOrderData()
{
string queryStr = "SELECT * FROM dbo.Orders ORDER BY OrderID;";
SqlConnection sqlConnection = new(ConnectionString);
sqlConnection.Open();
SqlCommand sqlCommand = new(queryStr, sqlConnection);
SqlDataAdapter DataAdapter = new(sqlCommand);
DataTable DataTable = new();
DataAdapter.Fill(DataTable);
sqlConnection.Close();
// Map data to a list.
List<Orders> dataSource = (from DataRow Data in DataTable.Rows
select new Orders()
{
OrderID = Convert.ToInt32(Data["OrderID"]),
CustomerID = Data["CustomerID"].ToString(),
EmployeeID = Convert.IsDBNull(Data["EmployeeID"]) ? 0 : Convert.ToUInt16(Data["EmployeeID"]),
ShipCity = Data["ShipCity"].ToString(),
Freight = Convert.ToDecimal(Data["Freight"])
}
).ToList();
return dataSource;
}
/// <summary>
/// Inserts a new data item into the data collection.
/// </summary>
/// <param name="value">It contains the new record detail which is need to be inserted.</param>
/// <returns>Returns void.</returns>
[HttpPost]
[Route("api/[controller]/Insert")]
public void Insert([FromBody] CRUDModel<Orders> value)
{
//Create query to insert the specific into the database by accessing its properties.
string queryStr = $"Insert into Orders(CustomerID,Freight,ShipCity,EmployeeID) values('{value.value.CustomerID}','{value.value.Freight}','{value.value.ShipCity}','{value.value.EmployeeID}')";
SqlConnection SqlConnection = new SqlConnection(ConnectionString);
SqlConnection.Open();
// Execute the SQL command.
SqlCommand SqlCommand = new SqlCommand(queryStr, SqlConnection);
// Execute this code to reflect the changes into the database.
SqlCommand.ExecuteNonQuery();
SqlConnection.Close();
// Add custom logic here if needed and remove above method.
}
/// <summary>
/// Update a existing data item from the data collection.
/// </summary>
/// <param name="value">It contains the updated record detail which is need to be updated.</param>
/// <returns>Returns void.</returns>
[HttpPost]
[Route("api/[controller]/Update")]
public void Update([FromBody] CRUDModel<Orders> value)
{
// Create query to update the changes into the database by accessing its properties.
string queryStr = $"Update Orders set CustomerID='{value.value.CustomerID}', Freight='{value.value.Freight}',EmployeeID='{value.value.EmployeeID}',ShipCity='{value.value.ShipCity}' where OrderID='{value.value.OrderID}'";
SqlConnection SqlConnection = new SqlConnection(ConnectionString);
SqlConnection.Open();
// Execute the SQL command.
SqlCommand SqlCommand = new SqlCommand(queryStr, SqlConnection);
// Execute this code to reflect the changes into the database.
SqlCommand.ExecuteNonQuery();
SqlConnection.Close();
//Add custom logic here if needed and remove above method.
}
/// <summary>
/// Remove a specific data item from the data collection.
/// </summary>
/// <param name="value">It contains the specific record detail which is need to be removed.</param>
/// <return>Returns void.</return>
[HttpPost]
[Route("api/[controller]/Remove")]
public void Remove([FromBody] CRUDModel<Orders> value)
{
// Create query to remove the specific from database by passing the primary key column value.
string queryStr = $"Delete from Orders where OrderID={value.key}";
SqlConnection SqlConnection = new SqlConnection(ConnectionString);
SqlConnection.Open();
// Execute the SQL command.
SqlCommand SqlCommand = new SqlCommand(queryStr, SqlConnection);
// Execute this code to reflect the changes into the database.
SqlCommand.ExecuteNonQuery();
SqlConnection.Close();
// Add custom logic here if needed and remove above method.
}
/// <summary>
/// Batch update (Insert, Update, and Delete) a collection of data items from the data collection.
/// </summary>
/// <param name="CRUDModel<T>">The set of information along with details about the CRUD actions to be executed from the database.</param>
/// <returns>Returns void.</returns>
[HttpPost]
[Route("api/[controller]/BatchUpdate")]
public IActionResult BatchUpdate([FromBody] CRUDModel<Orders> value)
{
if (value.changed != null && value.changed.Count > 0)
{
foreach (Orders Record in (IEnumerable<Orders>)value.changed)
{
// Create query to update the changes into the database by accessing its properties.
string queryStr = $"Update Orders set CustomerID='{Record.CustomerID}', Freight='{Record.Freight}',EmployeeID='{Record.EmployeeID}',ShipCity='{Record.ShipCity}' where OrderID='{Record.OrderID}'";
SqlConnection SqlConnection = new SqlConnection(ConnectionString);
SqlConnection.Open();
// Execute the SQL command.
SqlCommand SqlCommand = new SqlCommand(queryStr, SqlConnection);
// Execute this code to reflect the changes into the database.
SqlCommand.ExecuteNonQuery();
SqlConnection.Close();
// Add custom logic here if needed and remove above method.
}
}
if (value.added != null && value.added.Count > 0)
{
foreach (Orders Record in (IEnumerable<Orders>)value.added)
{
// Create query to insert the specific into the database by accessing its properties.
string queryStr = $"Insert into Orders(CustomerID,Freight,ShipCity,EmployeeID) values('{Record.CustomerID}','{Record.Freight}','{Record.ShipCity}','{Record.EmployeeID}')";
SqlConnection SqlConnection = new SqlConnection(ConnectionString);
SqlConnection.Open();
// Execute the SQL command.
SqlCommand SqlCommand = new SqlCommand(queryStr, SqlConnection);
// Execute this code to reflect the changes into the database.
SqlCommand.ExecuteNonQuery();
SqlConnection.Close();
// Add custom logic here if needed and remove above method.
}
}
if (value.deleted != null && value.deleted.Count > 0)
{
foreach (Orders Record in (IEnumerable<Orders>)value.deleted)
{
// Create query to remove the specific from database by passing the primary key column value.
string queryStr = $"Delete from Orders where OrderID={Record.OrderID}";
SqlConnection SqlConnection = new SqlConnection(ConnectionString);
SqlConnection.Open();
// Execute the SQL command.
SqlCommand SqlCommand = new SqlCommand(queryStr, SqlConnection);
// Execute this code to reflect the changes into the database.
SqlCommand.ExecuteNonQuery();
SqlConnection.Close();
// Add custom logic here if needed and remove above method.
}
}
return new JsonResult(value);
}
public class CRUDModel<T> where T : class
{
public string? action { get; set; }
public string? keyColumn { get; set; }
public object? key { get; set; }
public T? value { get; set; }
public List<T>? added { get; set; }
public List<T>? changed { get; set; }
public List<T>? deleted { get; set; }
public IDictionary<string, object>? @params { get; set; }
}
public class Orders
{
[Key]
public int? OrderID { get; set; }
public string? CustomerID { get; set; }
public int? EmployeeID { get; set; }
public decimal? Freight { get; set; }
public string? ShipCity { get; set; }
}
}
}