Skip to content

Commit fcfbb00

Browse files
Perform data and CRUD operation in ej2 angular grid using UrlAdaptor
1 parent 69eda7d commit fcfbb00

File tree

2 files changed

+82
-7
lines changed

2 files changed

+82
-7
lines changed

UrlAdaptor.Server/Controllers/GridController.cs

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace UrlAdaptor.Server.Controllers
77
{
88
[ApiController]
9-
public class GridController : ControllerBase
9+
public class GridController : Controller
1010
{
1111
[HttpPost]
1212
[Route("api/[controller]")]
@@ -55,6 +55,7 @@ public object Post([FromBody] DataManagerRequest DataManagerRequest)
5555
{
5656
DataSource = queryableOperation.PerformTake(DataSource, DataManagerRequest.Take);
5757
}
58+
5859
// Return data based on the request
5960
return new { result = DataSource, count = totalRecordsCount };
6061
}
@@ -73,7 +74,7 @@ public List<OrdersDetails> GetOrderData()
7374
/// <param name="newRecord">It contains the new record detail which is need to be inserted.</param>
7475
/// <returns>Returns void</returns>
7576
[HttpPost]
76-
[Route("api/Grid/Insert")]
77+
[Route("api/[controller]/Insert")]
7778
public void Insert([FromBody] CRUDModel<OrdersDetails> newRecord)
7879
{
7980
if (newRecord.value != null)
@@ -88,7 +89,7 @@ public void Insert([FromBody] CRUDModel<OrdersDetails> newRecord)
8889
/// <param name="Order">It contains the updated record detail which is need to be updated.</param>
8990
/// <returns>Returns void</returns>
9091
[HttpPost]
91-
[Route("api/Grid/Update")]
92+
[Route("api/[controller]/Update")]
9293
public void Update([FromBody] CRUDModel<OrdersDetails> Order)
9394
{
9495
var updatedOrder = Order.value;
@@ -113,7 +114,7 @@ public void Update([FromBody] CRUDModel<OrdersDetails> Order)
113114
/// <param name="value">It contains the specific record detail which is need to be removed.</param>
114115
/// <return>Returns void</return>
115116
[HttpPost]
116-
[Route("api/Grid/Remove")]
117+
[Route("api/[controller]/Remove")]
117118
public void Remove([FromBody] CRUDModel<OrdersDetails> value)
118119
{
119120
int orderId = int.Parse(value.key.ToString());
@@ -125,6 +126,80 @@ public void Remove([FromBody] CRUDModel<OrdersDetails> value)
125126
}
126127
}
127128

129+
/// <summary>
130+
/// The code for handling CRUD action at single request using crudURL
131+
/// </summary>
132+
/// <param name="request">It contains the details of the record and action to be done</param>
133+
134+
[HttpPost]
135+
[Route("api/[controller]/CrudUpdate")]
136+
public void CrudUpdate([FromBody] CRUDModel<OrdersDetails> request)
137+
{
138+
// perform update operation
139+
if (request.action == "update")
140+
{
141+
var orderValue = request.value;
142+
OrdersDetails existingRecord = OrdersDetails.GetAllRecords().Where(or => or.OrderID == orderValue.OrderID).FirstOrDefault();
143+
existingRecord.OrderID = orderValue.OrderID;
144+
existingRecord.CustomerID = orderValue.CustomerID;
145+
existingRecord.ShipCity = orderValue.ShipCity;
146+
}
147+
// perform insert operation
148+
else if (request.action == "insert")
149+
{
150+
OrdersDetails.GetAllRecords().Insert(0, request.value);
151+
}
152+
// perform remove operation
153+
else if (request.action == "remove")
154+
{
155+
OrdersDetails.GetAllRecords().Remove(OrdersDetails.GetAllRecords().Where(or => or.OrderID == int.Parse(request.key.ToString())).FirstOrDefault());
156+
}
157+
}
158+
159+
/// <summary>
160+
/// The code for handling CRUD operation when enbaling batch editing
161+
/// </summary>
162+
/// <param name="batchmodel"></param>
163+
/// <returns></returns>
164+
165+
[HttpPost]
166+
[Route("api/[controller]/BatchUpdate")]
167+
public IActionResult BatchUpdate([FromBody] CRUDModel<OrdersDetails> batchmodel)
168+
{
169+
if (batchmodel.added != null)
170+
{
171+
foreach (var addedOrder in batchmodel.added)
172+
{
173+
OrdersDetails.GetAllRecords().Insert(0, addedOrder);
174+
}
175+
}
176+
if (batchmodel.changed != null)
177+
{
178+
foreach (var changedOrder in batchmodel.changed)
179+
{
180+
var existingOrder = OrdersDetails.GetAllRecords().FirstOrDefault(or => or.OrderID == changedOrder.OrderID);
181+
if (existingOrder != null)
182+
{
183+
existingOrder.CustomerID = changedOrder.CustomerID;
184+
existingOrder.ShipCity = changedOrder.ShipCity;
185+
// Update other properties as needed
186+
}
187+
}
188+
}
189+
if (batchmodel.deleted != null)
190+
{
191+
foreach (var deletedOrder in batchmodel.deleted)
192+
{
193+
var orderToDelete = OrdersDetails.GetAllRecords().FirstOrDefault(or => or.OrderID == deletedOrder.OrderID);
194+
if (orderToDelete != null)
195+
{
196+
OrdersDetails.GetAllRecords().Remove(orderToDelete);
197+
}
198+
}
199+
}
200+
return Json(batchmodel);
201+
}
202+
128203

129204
public class CRUDModel<T> where T : class
130205
{

urladaptor.client/src/app/app.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ export class AppComponent {
1717
ngOnInit(): void {
1818
this.data = new DataManager({
1919
url: 'https://localhost:7018/api/grid', // Replace your hosted link
20-
insertUrl: 'https://localhost:7018/api/grid/Insert',
20+
insertUrl: 'https://localhost:7018/api/grid/Insert',
2121
updateUrl: 'https://localhost:7018/api/grid/Update',
2222
removeUrl: 'https://localhost:7018/api/grid/Remove',
23-
//crudUrl:'https://localhost:7018/api/grid/CrudUpdate',
24-
//batchUrl:'https://localhost:7018/api/grid/BatchUpdate',
23+
//crudUrl:'https://localhost:7018/api/grid/CrudUpdate', // perform all CRUD action at single request using crudURL
24+
//batchUrl:'https://localhost:7018/api/grid/BatchUpdate', // perform CRUD action using batchURL when enabling batch editing
2525
adaptor: new UrlAdaptor()
2626
});
2727
this.toolbar = ['Add', 'Update', 'Delete', 'Cancel', 'Search'];

0 commit comments

Comments
 (0)