1
+ using Microsoft . AspNetCore . Mvc ;
2
+ using System . ComponentModel . DataAnnotations ;
3
+ using System . Data ;
4
+ using Microsoft . Data . SqlClient ;
5
+ using Syncfusion . EJ2 . Base ;
6
+ using Dapper ;
7
+
8
+ namespace Grid_Dapper_CustomAdaptor . Server . Controllers
9
+ {
10
+ [ ApiController ]
11
+ public class GridController : ControllerBase
12
+ {
13
+ string ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\asp.core\Binding-data-from-remote-service-to-angular-data-grid\UrlAdaptor\UrlAdaptor.Server\App_Data\NORTHWND.MDF;Integrated Security=True;Connect Timeout=30;Encrypt=True" ;
14
+
15
+ /// <summary>
16
+ /// Processes the DataManager request to perform searching, filtering, sorting, and paging operations.
17
+ /// </summary>
18
+ /// <param name="DataManagerRequest">Contains the details of the data operation requested.</param>
19
+ /// <returns>Returns a JSON object with the filtered, sorted, and paginated data along with the total record count.</returns>
20
+ [ HttpPost ]
21
+ [ Route ( "api/[controller]" ) ]
22
+ public object Post ( [ FromBody ] DataManagerRequest DataManagerRequest )
23
+ {
24
+ // Retrieve data from the data source (e.g., database).
25
+ IQueryable < Orders > DataSource = GetOrderData ( ) . AsQueryable ( ) ;
26
+
27
+ // Initialize QueryableOperation instance.
28
+ QueryableOperation queryableOperation = new QueryableOperation ( ) ;
29
+
30
+ // Handling searching operation.
31
+ if ( DataManagerRequest . Search != null && DataManagerRequest . Search . Count > 0 )
32
+ {
33
+ DataSource = queryableOperation . PerformSearching ( DataSource , DataManagerRequest . Search ) ;
34
+ //Add custom logic here if needed and remove above method.
35
+ }
36
+
37
+ // Handling filtering operation.
38
+ if ( DataManagerRequest . Where != null && DataManagerRequest . Where . Count > 0 )
39
+ {
40
+ foreach ( WhereFilter condition in DataManagerRequest . Where )
41
+ {
42
+ foreach ( WhereFilter predicate in condition . predicates )
43
+ {
44
+ DataSource = queryableOperation . PerformFiltering ( DataSource , DataManagerRequest . Where , predicate . Operator ) ;
45
+ //Add custom logic here if needed and remove above method.
46
+ }
47
+ }
48
+ }
49
+
50
+ // Handling sorting operation.
51
+ if ( DataManagerRequest . Sorted != null && DataManagerRequest . Sorted . Count > 0 )
52
+ {
53
+ DataSource = queryableOperation . PerformSorting ( DataSource , DataManagerRequest . Sorted ) ;
54
+ //Add custom logic here if needed and remove above method.s
55
+ }
56
+
57
+
58
+ // Get the total count of records.
59
+ int totalRecordsCount = DataSource . Count ( ) ;
60
+
61
+ // Handling paging operation.
62
+ if ( DataManagerRequest . Skip != 0 )
63
+ {
64
+ DataSource = queryableOperation . PerformSkip ( DataSource , DataManagerRequest . Skip ) ;
65
+ //Add custom logic here if needed and remove above method.
66
+ }
67
+ if ( DataManagerRequest . Take != 0 )
68
+ {
69
+ DataSource = queryableOperation . PerformTake ( DataSource , DataManagerRequest . Take ) ;
70
+ //Add custom logic here if needed and remove above method.
71
+ }
72
+
73
+ // Return data based on the request.
74
+ return new { result = DataSource , count = totalRecordsCount } ;
75
+ }
76
+
77
+ /// <summary>
78
+ /// Retrieves the order data from the database.
79
+ /// </summary>
80
+ /// <returns>Returns a list of orders fetched from the database.</returns>
81
+ [ HttpGet ]
82
+ [ Route ( "api/[controller]" ) ]
83
+ public List < Orders > GetOrderData ( )
84
+ {
85
+ string queryStr = "SELECT * FROM dbo.Orders ORDER BY OrderID;" ;
86
+
87
+ //Create SQL connection.
88
+ using ( IDbConnection Connection = new SqlConnection ( ConnectionString ) )
89
+ {
90
+ Connection . Open ( ) ;
91
+ // Dapper automatically handles mapping to your Order class.
92
+ List < Orders > orders = Connection . Query < Orders > ( queryStr ) . ToList ( ) ;
93
+ return orders ;
94
+ }
95
+ }
96
+
97
+ /// <summary>
98
+ /// Inserts a new data item into the data collection.
99
+ /// </summary>
100
+ /// <param name="value">It contains the new record detail which is need to be inserted.</param>
101
+ /// <returns>Returns void.</returns>
102
+ [ HttpPost ]
103
+ [ Route ( "api/[controller]/Insert" ) ]
104
+ public void Insert ( [ FromBody ] CRUDModel < Orders > value )
105
+ {
106
+ //Create query to insert the specific into the database by accessing its properties.
107
+ string queryStr = "INSERT INTO Orders(CustomerID, Freight, ShipCity, EmployeeID) VALUES(@CustomerID, @Freight, @ShipCity, @EmployeeID)" ;
108
+
109
+ //Create SQL connection.
110
+ using ( IDbConnection Connection = new SqlConnection ( ConnectionString ) )
111
+ {
112
+ Connection . Open ( ) ;
113
+ //Execute this code to reflect the changes into the database.
114
+ Connection . Execute ( queryStr , value . value ) ;
115
+ }
116
+
117
+ //Add custom logic here if needed and remove above method.
118
+ }
119
+
120
+ /// <summary>
121
+ /// Update a existing data item from the data collection.
122
+ /// </summary>
123
+ /// <param name="value">It contains the updated record detail which is need to be updated.</param>
124
+ /// <returns>Returns void.</returns>
125
+ [ HttpPost ]
126
+ [ Route ( "api/[controller]/Update" ) ]
127
+ public void Update ( [ FromBody ] CRUDModel < Orders > value )
128
+ {
129
+ //Create query to update the changes into the database by accessing its properties.
130
+ string queryStr = "UPDATE Orders SET CustomerID = @CustomerID, Freight = @Freight, ShipCity = @ShipCity, EmployeeID = @EmployeeID WHERE OrderID = @OrderID" ;
131
+
132
+ //Create SQL connection.
133
+ using ( IDbConnection Connection = new SqlConnection ( ConnectionString ) )
134
+ {
135
+ Connection . Open ( ) ;
136
+ //Execute this code to reflect the changes into the database.
137
+ Connection . Execute ( queryStr , value . value ) ;
138
+ }
139
+
140
+ //Add custom logic here if needed and remove above method.
141
+ }
142
+
143
+ /// <summary>
144
+ /// Remove a specific data item from the data collection.
145
+ /// </summary>
146
+ /// <param name="value">It contains the specific record detail which is need to be removed.</param>
147
+ /// <return>Returns void.</return>
148
+ [ HttpPost ]
149
+ [ Route ( "api/[controller]/Remove" ) ]
150
+ public void Remove ( [ FromBody ] CRUDModel < Orders > value )
151
+ {
152
+ //Create query to remove the specific from database by passing the primary key column value.
153
+ string queryStr = "DELETE FROM Orders WHERE OrderID = @OrderID" ;
154
+
155
+ //Create SQL connection.
156
+ using ( IDbConnection Connection = new SqlConnection ( ConnectionString ) )
157
+ {
158
+ Connection . Open ( ) ;
159
+ int orderID = Convert . ToInt32 ( value . key . ToString ( ) ) ;
160
+ //Execute this code to reflect the changes into the database.
161
+ Connection . Execute ( queryStr , new { OrderID = orderID } ) ;
162
+ }
163
+
164
+ //Add custom logic here if needed and remove above method.
165
+ }
166
+
167
+
168
+ /// <summary>
169
+ /// Batch update (Insert, Update, and Delete) a collection of data items from the data collection.
170
+ /// </summary>
171
+ /// <param name="value">The set of information along with details about the CRUD actions to be executed from the database.</param>
172
+ /// <returns>Returns void.</returns>
173
+ [ HttpPost ]
174
+ [ Route ( "api/[controller]/BatchUpdate" ) ]
175
+ public IActionResult BatchUpdate ( [ FromBody ] CRUDModel < Orders > value )
176
+ {
177
+ if ( value . changed != null && value . changed . Count > 0 )
178
+ {
179
+ foreach ( Orders Record in ( IEnumerable < Orders > ) value . changed )
180
+ {
181
+ //Create query to update the changes into the database by accessing its properties.
182
+ string queryStr = "UPDATE Orders SET CustomerID = @CustomerID, Freight = @Freight, ShipCity = @ShipCity, EmployeeID = @EmployeeID WHERE OrderID = @OrderID" ;
183
+
184
+ //Create SQL connection.
185
+ using ( IDbConnection Connection = new SqlConnection ( ConnectionString ) )
186
+ {
187
+ Connection . Open ( ) ;
188
+ //Execute this code to reflect the changes into the database.
189
+ Connection . Execute ( queryStr , Record ) ;
190
+ }
191
+
192
+ //Add custom logic here if needed and remove above method.
193
+ }
194
+ }
195
+ if ( value . added != null && value . added . Count > 0 )
196
+ {
197
+ foreach ( Orders Record in ( IEnumerable < Orders > ) value . added )
198
+ {
199
+ //Create query to insert the specific into the database by accessing its properties.
200
+ string queryStr = "INSERT INTO Orders (CustomerID, Freight, ShipCity, EmployeeID) VALUES (@CustomerID, @Freight, @ShipCity, @EmployeeID)" ;
201
+
202
+ //Create SQL connection.
203
+ using ( IDbConnection Connection = new SqlConnection ( ConnectionString ) )
204
+ {
205
+ Connection . Open ( ) ;
206
+ //Execute this code to reflect the changes into the database.
207
+ Connection . Execute ( queryStr , Record ) ;
208
+ }
209
+
210
+ //Add custom logic here if needed and remove above method.
211
+ }
212
+ }
213
+ if ( value . deleted != null && value . deleted . Count > 0 )
214
+ {
215
+ foreach ( Orders Record in ( IEnumerable < Orders > ) value . deleted )
216
+ {
217
+ //Create query to remove the specific from database by passing the primary key column value.
218
+ string queryStr = "DELETE FROM Orders WHERE OrderID = @OrderID" ;
219
+
220
+ //Create SQL connection.
221
+ using ( IDbConnection Connection = new SqlConnection ( ConnectionString ) )
222
+ {
223
+ Connection . Open ( ) ;
224
+ //Execute this code to reflect the changes into the database.
225
+ Connection . Execute ( queryStr , new { OrderID = Record . OrderID } ) ;
226
+ }
227
+
228
+ //Add custom logic here if needed and remove above method.
229
+ }
230
+ }
231
+ return new JsonResult ( value ) ;
232
+ }
233
+
234
+ public class Orders
235
+ {
236
+ [ Key ]
237
+ public int ? OrderID { get ; set ; }
238
+ public string ? CustomerID { get ; set ; }
239
+ public int ? EmployeeID { get ; set ; }
240
+ public decimal ? Freight { get ; set ; }
241
+ public string ? ShipCity { get ; set ; }
242
+ }
243
+
244
+ public class CRUDModel < T > where T : class
245
+ {
246
+ public string ? action { get ; set ; }
247
+ public string ? keyColumn { get ; set ; }
248
+ public object ? key { get ; set ; }
249
+ public T ? value { get ; set ; }
250
+ public List < T > ? added { get ; set ; }
251
+ public List < T > ? changed { get ; set ; }
252
+ public List < T > ? deleted { get ; set ; }
253
+ public IDictionary < string , object > ? @params { get ; set ; }
254
+ }
255
+ }
256
+ }
0 commit comments