Skip to content

Commit 027f939

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

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+
}

‎WebApiAdaptor.Server/Program.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
var builder = WebApplication.CreateBuilder(args);
22

3-
// Add services to the container.
43
// Add services to the container.
54
builder.Services.AddControllers().AddJsonOptions(options =>
65
{
@@ -10,15 +9,17 @@
109
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
1110
builder.Services.AddEndpointsApiExplorer();
1211
builder.Services.AddSwaggerGen();
12+
13+
1314
builder.Services.AddCors(options =>
1415
{
1516
options.AddDefaultPolicy(builder =>
1617
{
1718
builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();
1819
});
1920
});
20-
2121
var app = builder.Build();
22+
app.UseCors();
2223

2324
app.UseDefaultFiles();
2425
app.UseStaticFiles();
@@ -36,8 +37,6 @@
3637

3738
app.MapControllers();
3839

39-
app.UseCors();
40-
4140
app.MapFallbackToFile("/index.html");
4241

4342
app.Run();
Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,25 @@
11
{
2-
"$schema": "http://json.schemastore.org/launchsettings.json",
3-
"iisSettings": {
4-
"windowsAuthentication": false,
5-
"anonymousAuthentication": true,
6-
"iisExpress": {
7-
"applicationUrl": "http://localhost:48434",
8-
"sslPort": 44367
9-
}
10-
},
112
"profiles": {
123
"http": {
134
"commandName": "Project",
14-
"dotnetRunMessages": true,
155
"launchBrowser": true,
166
"launchUrl": "swagger",
17-
"applicationUrl": "http://localhost:5030",
187
"environmentVariables": {
198
"ASPNETCORE_ENVIRONMENT": "Development",
209
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy"
21-
}
10+
},
11+
"dotnetRunMessages": true,
12+
"applicationUrl": "http://localhost:5070"
2213
},
2314
"https": {
2415
"commandName": "Project",
25-
"dotnetRunMessages": true,
26-
"launchBrowser": true,
2716
"launchUrl": "swagger",
28-
"applicationUrl": "https://localhost:7112;http://localhost:5030",
2917
"environmentVariables": {
3018
"ASPNETCORE_ENVIRONMENT": "Development",
3119
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy"
32-
}
20+
},
21+
"dotnetRunMessages": true,
22+
"applicationUrl": "https://localhost:7041;http://localhost:5070"
3323
},
3424
"IIS Express": {
3525
"commandName": "IISExpress",
@@ -40,6 +30,14 @@
4030
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy"
4131
}
4232
}
33+
},
34+
"$schema": "http://json.schemastore.org/launchsettings.json",
35+
"iisSettings": {
36+
"windowsAuthentication": false,
37+
"anonymousAuthentication": true,
38+
"iisExpress": {
39+
"applicationUrl": "http://localhost:42068",
40+
"sslPort": 44311
41+
}
4342
}
44-
}
45-
43+
}

‎WebApiAdaptor.Server/WebApiAdaptor.Server.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
44
<TargetFramework>net8.0</TargetFramework>

‎WebApiAdaptor.Server/WebApiAdaptor.Server.http

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@WebApiAdaptor.Server_HostAddress = http://localhost:5030
1+
@WebApiAdaptor.Server_HostAddress = http://localhost:5070
22

33
GET {{WebApiAdaptor.Server_HostAddress}}/weatherforecast/
44
Accept: application/json

‎WebApiAdaptor.sln

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,31 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
44
VisualStudioVersion = 17.9.34701.34
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApiAdaptor.Server", "WebApiAdaptor.Server\WebApiAdaptor.Server.csproj", "{31274216-BB92-48EC-9B09-30AB4DBC04A8}"
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApiAdaptor.Server", "WebApiAdaptor.Server\WebApiAdaptor.Server.csproj", "{935249AC-5920-4AE8-A28D-1C4A82B69185}"
77
EndProject
8-
Project("{54A90642-561A-4BB1-A94E-469ADEE60C69}") = "webapiadaptor.client", "webapiadaptor.client\webapiadaptor.client.esproj", "{7672977C-8269-4F95-99A5-379604C7FD37}"
8+
Project("{54A90642-561A-4BB1-A94E-469ADEE60C69}") = "webapiadaptor.client", "webapiadaptor.client\webapiadaptor.client.esproj", "{B9E666B2-01B5-4037-871C-0EED50D528F2}"
99
EndProject
1010
Global
1111
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1212
Debug|Any CPU = Debug|Any CPU
1313
Release|Any CPU = Release|Any CPU
1414
EndGlobalSection
1515
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16-
{31274216-BB92-48EC-9B09-30AB4DBC04A8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17-
{31274216-BB92-48EC-9B09-30AB4DBC04A8}.Debug|Any CPU.Build.0 = Debug|Any CPU
18-
{31274216-BB92-48EC-9B09-30AB4DBC04A8}.Release|Any CPU.ActiveCfg = Release|Any CPU
19-
{31274216-BB92-48EC-9B09-30AB4DBC04A8}.Release|Any CPU.Build.0 = Release|Any CPU
20-
{7672977C-8269-4F95-99A5-379604C7FD37}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21-
{7672977C-8269-4F95-99A5-379604C7FD37}.Debug|Any CPU.Build.0 = Debug|Any CPU
22-
{7672977C-8269-4F95-99A5-379604C7FD37}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
23-
{7672977C-8269-4F95-99A5-379604C7FD37}.Release|Any CPU.ActiveCfg = Release|Any CPU
24-
{7672977C-8269-4F95-99A5-379604C7FD37}.Release|Any CPU.Build.0 = Release|Any CPU
25-
{7672977C-8269-4F95-99A5-379604C7FD37}.Release|Any CPU.Deploy.0 = Release|Any CPU
16+
{935249AC-5920-4AE8-A28D-1C4A82B69185}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{935249AC-5920-4AE8-A28D-1C4A82B69185}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{935249AC-5920-4AE8-A28D-1C4A82B69185}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{935249AC-5920-4AE8-A28D-1C4A82B69185}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{B9E666B2-01B5-4037-871C-0EED50D528F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{B9E666B2-01B5-4037-871C-0EED50D528F2}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{B9E666B2-01B5-4037-871C-0EED50D528F2}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
23+
{B9E666B2-01B5-4037-871C-0EED50D528F2}.Release|Any CPU.ActiveCfg = Release|Any CPU
24+
{B9E666B2-01B5-4037-871C-0EED50D528F2}.Release|Any CPU.Build.0 = Release|Any CPU
25+
{B9E666B2-01B5-4037-871C-0EED50D528F2}.Release|Any CPU.Deploy.0 = Release|Any CPU
2626
EndGlobalSection
2727
GlobalSection(SolutionProperties) = preSolution
2828
HideSolutionNode = FALSE
2929
EndGlobalSection
3030
GlobalSection(ExtensibilityGlobals) = postSolution
31-
SolutionGuid = {D2A1D38E-BAC4-4F8D-B149-A519D9D2D8D3}
31+
SolutionGuid = {0B8E7BAE-ED32-4E9A-A7E5-2F3869EC101C}
3232
EndGlobalSection
3333
EndGlobal

0 commit comments

Comments
 (0)
Please sign in to comment.