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