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