Skip to content

Commit 027f939

Browse files
Perform data and CRUD operation in ej2 angular grid using WebApiAdaptor
1 parent d504975 commit 027f939

File tree

15 files changed

+91
-135
lines changed

15 files changed

+91
-135
lines changed

.github/workflows/gitleaks.yaml

Lines changed: 0 additions & 38 deletions
This file was deleted.

WebApiAdaptor.Client/obj/Debug/webapiadaptor.client.esproj.CoreCompileInputs.cache

Whitespace-only changes.

WebApiAdaptor.Client/obj/Debug/webapiadaptor.client.esproj.FileListAbsolute.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
<ejs-grid #grid [dataSource]='data' [editSettings]="editSettings" [toolbar]="toolbar" allowFiltering="true" allowPaging="true" allowSorting="true" height="320">
1+
<ejs-grid #grid [dataSource]='data' [editSettings]="editSettings" [toolbar]="toolbar" allowPaging="true" allowSorting="true" allowFiltering="true" height="320">
22
<e-columns>
33
<e-column field='OrderID' headerText='Order ID' isPrimaryKey=true width='150'></e-column>
44
<e-column field='CustomerID' headerText='Customer Name' width='150'></e-column>
55
<e-column field='ShipCity' headerText='ShipCity' width='150' textAlign='Right'></e-column>
6+
<e-column field='ShipCountry' headerText='Ship Country' width='150'></e-column>
67
</e-columns>
8+
</ejs-grid>

WebApiAdaptor.Client/src/app/app.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class AppComponent {
1515

1616
ngOnInit(): void {
1717
this.data = new DataManager({
18-
url: 'https://localhost:7112/api/Orders',
18+
url: 'https://localhost:7041/api/Orders',
1919
adaptor: new WebApiAdaptor()
2020
});
2121

WebApiAdaptor.Client/src/app/app.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { EditService, FilterService, GridModule, PageService, SortService, Toolb
1515
AppRoutingModule,
1616
GridModule
1717
],
18-
providers: [EditService, ToolbarService, FilterService, SortService, PageService],
18+
providers: [EditService, ToolbarService, SortService, FilterService, PageService],
1919
bootstrap: [AppComponent]
2020
})
2121
export class AppModule { }

WebApiAdaptor.Client/src/proxy.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const { env } = require('process');
22

33
const target = env.ASPNETCORE_HTTPS_PORT ? `https://localhost:${env.ASPNETCORE_HTTPS_PORT}` :
4-
env.ASPNETCORE_URLS ? env.ASPNETCORE_URLS.split(';')[0] : 'https://localhost:7112';
4+
env.ASPNETCORE_URLS ? env.ASPNETCORE_URLS.split(';')[0] : 'https://localhost:7041';
55

66
const PROXY_CONFIG = [
77
{

WebApiAdaptor.Client/src/styles.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
@import '../node_modules/@syncfusion/ej2-dropdowns/styles/material.css';
66
@import '../node_modules/@syncfusion/ej2-inputs/styles/material.css';
77
@import '../node_modules/@syncfusion/ej2-navigations/styles/material.css';
8+
@import '../node_modules/@syncfusion/ej2-popups/styles/material.css';
89
@import '../node_modules/@syncfusion/ej2-splitbuttons/styles/material.css';
910
@import '../node_modules/@syncfusion/ej2-angular-grids/styles/material.css';

WebApiAdaptor.Server/Controllers/OrdersController.cs

Lines changed: 48 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,18 @@
44

55
namespace WebApiAdaptor.Server.Controllers
66
{
7-
87
[Route("api/[controller]")]
98
[ApiController]
109
public class OrdersController : ControllerBase
1110
{
12-
// GET: api/Orders
1311
[HttpGet]
14-
// Action to retrieve orders
1512
public object Get()
1613
{
1714
var queryString = Request.Query;
1815
var data = OrdersDetails.GetAllRecords().ToList();
16+
string filter = queryString["$filter"]; // filtering params
17+
string sort = queryString["$orderby"]; //sorting params
1918

20-
string? sort = queryString["$orderby"]; // Get sorting parameter
21-
string? filter = queryString["$filter"]; // // Get filtering parameter
22-
23-
//Peform sort operation
24-
if (!string.IsNullOrEmpty(sort))
25-
{
26-
var sortConditions = sort.Split(',');
27-
28-
var orderedData = data.OrderBy(x => 0); // Start with a stable sort
29-
30-
foreach (var sortCondition in sortConditions)
31-
{
32-
var sortParts = sortCondition.Trim().Split(' ');
33-
var sortBy = sortParts[0];
34-
var sortOrder = sortParts.Length > 1 && sortParts[1].ToLower() == "desc";
35-
36-
switch (sortBy)
37-
{
38-
case "OrderID":
39-
orderedData = sortOrder ? orderedData.ThenByDescending(x => x.OrderID) : orderedData.ThenBy(x => x.OrderID);
40-
break;
41-
case "CustomerID":
42-
orderedData = sortOrder ? orderedData.ThenByDescending(x => x.CustomerID) : orderedData.ThenBy(x => x.CustomerID);
43-
break;
44-
case "ShipCity":
45-
orderedData = sortOrder ? orderedData.ThenByDescending(x => x.ShipCity) : orderedData.ThenBy(x => x.ShipCity);
46-
break;
47-
}
48-
}
49-
50-
data = [.. orderedData];
51-
}
5219
if (filter != null)
5320
{
5421
var filters = filter.Split(new string[] { " and " }, StringSplitOptions.RemoveEmptyEntries);
@@ -57,22 +24,21 @@ public object Get()
5724
{
5825
if (filterItem.Contains("substringof"))
5926
{
60-
// Performing Search operation
61-
27+
// Perform searching operation
6228
var searchParts = filterItem.Split('(', ')', '\'');
6329
var searchValue = searchParts[3];
6430

6531
// Apply the search value to all searchable fields
6632
data = data.Where(cust =>
67-
cust != null &&
68-
((cust.OrderID?.ToString()?.Contains(searchValue) ?? false) ||
69-
(cust.CustomerID?.ToLower()?.Contains(searchValue) ?? false) ||
70-
(cust.ShipCity?.ToLower()?.Contains(searchValue) ?? false))).ToList();
33+
cust.OrderID.ToString().Contains(searchValue) ||
34+
cust.CustomerID.ToLower().Contains(searchValue) ||
35+
cust.ShipCity.ToLower().Contains(searchValue)
36+
// Add conditions for other searchable fields as needed
37+
).ToList();
7138
}
7239
else
7340
{
74-
// Performing filter operation
75-
41+
// Perform filtering operation
7642
var filterfield = "";
7743
var filtervalue = "";
7844
var filterParts = filterItem.Split('(', ')', '\'');
@@ -90,36 +56,64 @@ public object Get()
9056
switch (filterfield)
9157
{
9258
case "OrderID":
93-
data = data.Where(cust => cust != null && cust.OrderID?.ToString() == filtervalue.ToString()).ToList();
59+
data = (from cust in data
60+
where cust.OrderID.ToString() == filtervalue.ToString()
61+
select cust).ToList();
9462
break;
9563
case "CustomerID":
96-
data = data.Where(cust => cust != null && cust.CustomerID?.ToLower().StartsWith(filtervalue.ToString()) == true).ToList();
64+
data = (from cust in data
65+
where cust.CustomerID.ToLower().StartsWith(filtervalue.ToString())
66+
select cust).ToList();
9767
break;
9868
case "ShipCity":
99-
data = data.Where(cust => cust != null && cust.ShipCity?.ToLower().StartsWith(filtervalue.ToString()) == true).ToList();
69+
data = (from cust in data
70+
where cust.ShipCity.ToLower().StartsWith(filtervalue.ToString())
71+
select cust).ToList();
10072
break;
101-
// Add more cases for other searchable fields if needed
10273
}
74+
}
75+
}
76+
}
10377

78+
// Perform sorting operation
79+
if (!string.IsNullOrEmpty(sort))
80+
{
81+
var sortConditions = sort.Split(',');
82+
var orderedData = data.OrderBy(x => 0); // Start with a stable sort
83+
foreach (var sortCondition in sortConditions)
84+
{
85+
var sortParts = sortCondition.Trim().Split(' ');
86+
var sortBy = sortParts[0];
87+
var sortOrder = sortParts.Length > 1 && sortParts[1].ToLower() == "desc";
88+
switch (sortBy)
89+
{
90+
case "OrderID":
91+
orderedData = sortOrder ? orderedData.ThenByDescending(x => x.OrderID) : orderedData.ThenBy(x => x.OrderID);
92+
break;
93+
case "CustomerID":
94+
orderedData = sortOrder ? orderedData.ThenByDescending(x => x.CustomerID) : orderedData.ThenBy(x => x.CustomerID);
95+
break;
96+
case "ShipCity":
97+
orderedData = sortOrder ? orderedData.ThenByDescending(x => x.ShipCity) : orderedData.ThenBy(x => x.ShipCity);
98+
break;
10499
}
105100
}
101+
data = orderedData.ToList();
106102
}
107-
//Perform page operation
108103

104+
// Perform Paging operation
109105
int skip = Convert.ToInt32(queryString["$skip"]);
110106
int take = Convert.ToInt32(queryString["$top"]);
111-
int TotalRecordsCount = data.Count;
112107

113-
return take != 0 ? new { Items = data.Skip(skip).Take(take).ToList(), Count = TotalRecordsCount } : new { Items = data, Count = TotalRecordsCount };
108+
return take != 0 ? new { Items = data.Skip(skip).Take(take).ToList(), Count = data.Count() } : new { Items = data, Count = data.Count() };
114109
}
115110

116-
117111
// POST: api/Orders
118112
[HttpPost]
119113
/// <summary>
120114
/// Inserts a new data item into the data collection.
121115
/// </summary>
122-
/// <param name="value">It holds new record detail which is need to be inserted.</param>
116+
/// <param name="newRecord">It holds new record detail which is need to be inserted.</param>
123117
/// <returns>Returns void</returns>
124118
public void Post([FromBody] OrdersDetails newRecord)
125119
{
@@ -144,10 +138,11 @@ public void Put(int id, [FromBody] OrdersDetails order)
144138
existingOrder.OrderID = order.OrderID;
145139
existingOrder.CustomerID = order.CustomerID;
146140
existingOrder.ShipCity = order.ShipCity;
141+
existingOrder.ShipCountry = order.ShipCountry;
147142
}
148143
}
149144

150-
// DELETE: api/Orders/5
145+
// DELETE: api/5
151146
[HttpDelete("{id}")]
152147
/// <summary>
153148
/// Remove a specific data item from the data collection.

WebApiAdaptor.Server/Models/OrdersDetails.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public OrdersDetails(
2727

2828
public static List<OrdersDetails> GetAllRecords()
2929
{
30-
if (order.Count == 0)
30+
if (order.Count() == 0)
3131
{
3232
int code = 10000;
3333
for (int i = 1; i < 10; i++)
@@ -55,4 +55,4 @@ public static List<OrdersDetails> GetAllRecords()
5555
public DateTime ShippedDate { get; set; }
5656
public string? ShipAddress { get; set; }
5757
}
58-
}
58+
}

0 commit comments

Comments
 (0)