@@ -12,7 +12,7 @@ namespace Grid_Dapper.Controllers
12
12
public class GridController : ControllerBase
13
13
{
14
14
15
- string ConnectionString = @"<Enter a valid connection string>" ;
15
+ string ConnectionString = @"<Enter a valid connection string>" ;
16
16
17
17
/// <summary>
18
18
/// Processes the DataManager request to perform searching, filtering, sorting, and paging operations.
@@ -23,64 +23,57 @@ public class GridController : ControllerBase
23
23
[ Route ( "api/[controller]" ) ]
24
24
public object Post ( [ FromBody ] DataManagerRequest DataManagerRequest )
25
25
{
26
- // Retrieve data from the data source (e.g., database)
26
+ // Retrieve data from the data source (e.g., database).
27
27
IQueryable < Orders > DataSource = GetOrderData ( ) . AsQueryable ( ) ;
28
28
29
- QueryableOperation queryableOperation = new QueryableOperation ( ) ; // Initialize QueryableOperation instance
29
+ // Initialize QueryableOperation instance.
30
+ QueryableOperation queryableOperation = new QueryableOperation ( ) ;
30
31
31
-
32
- // Handling Searching operation
32
+ // Handling searching operation.
33
33
if ( DataManagerRequest . Search != null && DataManagerRequest . Search . Count > 0 )
34
34
{
35
35
DataSource = queryableOperation . PerformSearching ( DataSource , DataManagerRequest . Search ) ;
36
+ //Add custom logic here if needed and remove above method.
36
37
}
37
38
38
- // Handling filtering operation
39
+ // Handling filtering operation.
39
40
if ( DataManagerRequest . Where != null && DataManagerRequest . Where . Count > 0 )
40
41
{
41
- foreach ( var condition in DataManagerRequest . Where )
42
+ foreach ( WhereFilter condition in DataManagerRequest . Where )
42
43
{
43
- foreach ( var predicate in condition . predicates )
44
+ foreach ( WhereFilter predicate in condition . predicates )
44
45
{
45
46
DataSource = queryableOperation . PerformFiltering ( DataSource , DataManagerRequest . Where , predicate . Operator ) ;
47
+ //Add custom logic here if needed and remove above method.
46
48
}
47
49
}
48
50
}
49
51
50
- // Handling Sorting operation.
52
+ // Handling sorting operation.
51
53
if ( DataManagerRequest . Sorted != null && DataManagerRequest . Sorted . Count > 0 )
52
54
{
53
55
DataSource = queryableOperation . PerformSorting ( DataSource , DataManagerRequest . Sorted ) ;
54
- }
55
- // Handle aggregation
56
- List < string > str = new List < string > ( ) ;
57
- if ( DataManagerRequest . Aggregates != null )
58
- {
59
- for ( var i = 0 ; i < DataManagerRequest . Aggregates . Count ; i ++ )
60
- {
61
- str . Add ( DataManagerRequest . Aggregates [ i ] . Field ) ;
62
- }
56
+ //Add custom logic here if needed and remove above method.
63
57
}
64
58
65
- // Assuming PerformSelect is the method that handles the aggregation logic
66
- IEnumerable aggregate = queryableOperation . PerformSelect ( DataSource , str ) ;
67
59
68
60
// Get the total count of records.
69
61
int totalRecordsCount = DataSource . Count ( ) ;
70
62
71
63
// Handling paging operation.
72
64
if ( DataManagerRequest . Skip != 0 )
73
65
{
74
-
75
66
DataSource = queryableOperation . PerformSkip ( DataSource , DataManagerRequest . Skip ) ;
67
+ //Add custom logic here if needed and remove above method.
76
68
}
77
69
if ( DataManagerRequest . Take != 0 )
78
70
{
79
71
DataSource = queryableOperation . PerformTake ( DataSource , DataManagerRequest . Take ) ;
72
+ //Add custom logic here if needed and remove above method.
80
73
}
81
74
82
75
// Return data based on the request.
83
- return new { result = DataSource , count = totalRecordsCount , aggregate = aggregate } ;
76
+ return new { result = DataSource , count = totalRecordsCount } ;
84
77
}
85
78
86
79
[ HttpGet ]
@@ -101,120 +94,139 @@ public List<Orders> GetOrderData()
101
94
/// <summary>
102
95
/// Inserts a new data item into the data collection.
103
96
/// </summary>
104
- /// <param name="newRecord ">It contains the new record detail which is need to be inserted.</param>
105
- /// <returns>Returns void</returns>
97
+ /// <param name="value ">It contains the new record detail which is need to be inserted.</param>
98
+ /// <returns>Returns void. </returns>
106
99
[ HttpPost ]
107
100
[ Route ( "api/[controller]/Insert" ) ]
108
101
public void Insert ( [ FromBody ] CRUDModel < Orders > value )
109
102
{
110
- //Create query to insert the specific into the database by accessing its properties
111
- string Query = "INSERT INTO Orders(CustomerID, Freight, ShipCity, EmployeeID) VALUES(@CustomerID, @Freight, @ShipCity, @EmployeeID)" ;
103
+ //Create query to insert the specific into the database by accessing its properties.
104
+ string queryStr = "INSERT INTO Orders(CustomerID, Freight, ShipCity, EmployeeID) VALUES(@CustomerID, @Freight, @ShipCity, @EmployeeID)" ;
105
+
106
+ //Create SQL connection.
112
107
using ( IDbConnection Connection = new SqlConnection ( ConnectionString ) )
113
108
{
114
109
Connection . Open ( ) ;
115
- //Execute this code to reflect the changes into the database
116
- Connection . Execute ( Query , value . value ) ;
110
+ //Execute this code to reflect the changes into the database.
111
+ Connection . Execute ( queryStr , value . value ) ;
117
112
}
118
113
114
+ //Add custom logic here if needed and remove above method.
119
115
}
120
116
121
117
/// <summary>
122
118
/// Update a existing data item from the data collection.
123
119
/// </summary>
124
- /// <param name="Order ">It contains the updated record detail which is need to be updated.</param>
125
- /// <returns>Returns void</returns>
120
+ /// <param name="value ">It contains the updated record detail which is need to be updated.</param>
121
+ /// <returns>Returns void. </returns>
126
122
[ HttpPost ]
127
123
[ Route ( "api/[controller]/Update" ) ]
128
124
public void Update ( [ FromBody ] CRUDModel < Orders > value )
129
125
{
130
- //Create query to update the changes into the database by accessing its properties
131
- string Query = "UPDATE Orders SET CustomerID = @CustomerID, Freight = @Freight, ShipCity = @ShipCity, EmployeeID = @EmployeeID WHERE OrderID = @OrderID" ;
126
+ //Create query to update the changes into the database by accessing its properties.
127
+ string queryStr = "UPDATE Orders SET CustomerID = @CustomerID, Freight = @Freight, ShipCity = @ShipCity, EmployeeID = @EmployeeID WHERE OrderID = @OrderID" ;
128
+
129
+ //Create SQL connection.
132
130
using ( IDbConnection Connection = new SqlConnection ( ConnectionString ) )
133
131
{
134
132
Connection . Open ( ) ;
135
- //Execute this code to reflect the changes into the database
136
- Connection . Execute ( Query , value . value ) ;
133
+ //Execute this code to reflect the changes into the database.
134
+ Connection . Execute ( queryStr , value . value ) ;
137
135
}
136
+
137
+ //Add custom logic here if needed and remove above method.
138
138
}
139
139
140
140
/// <summary>
141
141
/// Remove a specific data item from the data collection.
142
142
/// </summary>
143
143
/// <param name="value">It contains the specific record detail which is need to be removed.</param>
144
- /// <return>Returns void</return>
144
+ /// <return>Returns void. </return>
145
145
[ HttpPost ]
146
146
[ Route ( "api/[controller]/Remove" ) ]
147
147
public void Remove ( [ FromBody ] CRUDModel < Orders > value )
148
148
{
149
149
//Create query to remove the specific from database by passing the primary key column value.
150
- string Query = "DELETE FROM Orders WHERE OrderID = @OrderID" ;
150
+ string queryStr = "DELETE FROM Orders WHERE OrderID = @OrderID" ;
151
+
152
+ //Create SQL connection.
151
153
using ( IDbConnection Connection = new SqlConnection ( ConnectionString ) )
152
154
{
153
155
Connection . Open ( ) ;
154
156
int orderID = Convert . ToInt32 ( value . key . ToString ( ) ) ;
155
- //Execute this code to reflect the changes into the database
156
- Connection . Execute ( Query , new { OrderID = orderID } ) ;
157
+ //Execute this code to reflect the changes into the database.
158
+ Connection . Execute ( queryStr , new { OrderID = orderID } ) ;
157
159
}
160
+
161
+ //Add custom logic here if needed and remove above method.
158
162
}
159
163
160
164
161
165
162
166
/// <summary>
163
- /// The code for handling CRUD operation when enbaling batch editing
167
+ /// Batch update (Insert, Update, and Delete) a collection of data items from the data collection.
164
168
/// </summary>
165
- /// <param name="batchmodel"></param>
166
- /// <returns></returns>
167
-
169
+ /// <param name="value">The set of information along with details about the CRUD actions to be executed from the database.</param>
170
+ /// <returns>Returns void.</returns>
168
171
[ HttpPost ]
169
172
[ Route ( "api/[controller]/BatchUpdate" ) ]
170
173
public IActionResult BatchUpdate ( [ FromBody ] CRUDModel < Orders > value )
171
174
{
172
- //TODO: Enter the connectionstring of database
173
175
if ( value . changed != null && value . changed . Count > 0 )
174
176
{
175
- foreach ( var Record in ( IEnumerable < Orders > ) value . changed )
177
+ foreach ( Orders Record in ( IEnumerable < Orders > ) value . changed )
176
178
{
177
- //Create query to update the changes into the database by accessing its properties
178
- string Query = "UPDATE Orders SET CustomerID = @CustomerID, Freight = @Freight, ShipCity = @ShipCity, EmployeeID = @EmployeeID WHERE OrderID = @OrderID" ;
179
- using ( IDbConnection Connection = new SqlConnection ( ConnectionString ) )
180
- {
181
- Connection . Open ( ) ;
182
- //Execute this code to reflect the changes into the database
183
- Connection . Execute ( Query , Record ) ;
184
- }
179
+ //Create query to update the changes into the database by accessing its properties.
180
+ string queryStr = "UPDATE Orders SET CustomerID = @CustomerID, Freight = @Freight, ShipCity = @ShipCity, EmployeeID = @EmployeeID WHERE OrderID = @OrderID" ;
181
+
182
+ //Create SQL connection.
183
+ using ( IDbConnection Connection = new SqlConnection ( ConnectionString ) )
184
+ {
185
+ Connection . Open ( ) ;
186
+ //Execute this code to reflect the changes into the database.
187
+ Connection . Execute ( queryStr , Record ) ;
185
188
}
186
189
190
+ //Add custom logic here if needed and remove above method.
191
+ }
187
192
}
188
193
if ( value . added != null && value . added . Count > 0 )
189
194
{
190
- foreach ( var Record in ( IEnumerable < Orders > ) value . added )
195
+ foreach ( Orders Record in ( IEnumerable < Orders > ) value . added )
191
196
{
192
- //Create query to insert the specific into the database by accessing its properties
193
- string Query = "INSERT INTO Orders (CustomerID, Freight, ShipCity, EmployeeID) VALUES (@CustomerID, @Freight, @ShipCity, @EmployeeID)" ;
194
- using ( IDbConnection Connection = new SqlConnection ( ConnectionString ) )
195
- {
196
- Connection . Open ( ) ;
197
- //Execute this code to reflect the changes into the database
198
- Connection . Execute ( Query , Record ) ;
199
- }
197
+ //Create query to insert the specific into the database by accessing its properties.
198
+ string queryStr = "INSERT INTO Orders (CustomerID, Freight, ShipCity, EmployeeID) VALUES (@CustomerID, @Freight, @ShipCity, @EmployeeID)" ;
199
+
200
+ //Create SQL connection.
201
+ using ( IDbConnection Connection = new SqlConnection ( ConnectionString ) )
202
+ {
203
+ Connection . Open ( ) ;
204
+ //Execute this code to reflect the changes into the database.
205
+ Connection . Execute ( queryStr , Record ) ;
206
+ }
207
+
208
+ //Add custom logic here if needed and remove above method.
200
209
}
201
210
}
202
211
if ( value . deleted != null && value . deleted . Count > 0 )
203
212
{
204
- foreach ( var Record in ( IEnumerable < Orders > ) value . deleted )
213
+ foreach ( Orders Record in ( IEnumerable < Orders > ) value . deleted )
205
214
{
206
215
//Create query to remove the specific from database by passing the primary key column value.
207
- string Query = "DELETE FROM Orders WHERE OrderID = @OrderID" ;
216
+ string queryStr = "DELETE FROM Orders WHERE OrderID = @OrderID" ;
217
+
218
+ //Create SQL connection.
208
219
using ( IDbConnection Connection = new SqlConnection ( ConnectionString ) )
209
220
{
210
221
Connection . Open ( ) ;
211
- //Execute this code to reflect the changes into the database
212
- Connection . Execute ( Query , new { OrderID = Record . OrderID } ) ;
222
+ //Execute this code to reflect the changes into the database.
223
+ Connection . Execute ( queryStr , new { OrderID = Record . OrderID } ) ;
213
224
}
225
+
226
+ //Add custom logic here if needed and remove above method.
214
227
}
215
228
}
216
- return new JsonResult ( value ) ;
217
-
229
+ return new JsonResult ( value ) ;
218
230
}
219
231
220
232
@@ -249,6 +261,6 @@ public class Orders
249
261
public decimal ? Freight { get ; set ; }
250
262
public string ? ShipCity { get ; set ; }
251
263
}
252
-
253
- }
264
+
265
+ }
254
266
}
0 commit comments