Skip to content

Commit e2f29ab

Browse files
documentation(914729):Updated custom adaptor sample
1 parent 47e7dff commit e2f29ab

16 files changed

+436
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using CustomAdaptor.Models;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.AspNetCore.OData.Query;
4+
5+
namespace CustomAdaptor.Controllers
6+
{
7+
public class OrdersController : Controller
8+
{
9+
/// <summary>
10+
/// Retrieves all orders.
11+
/// </summary>
12+
/// <returns>The collection of orders.</returns>
13+
[HttpGet]
14+
[EnableQuery]
15+
public IActionResult Get()
16+
{
17+
var data = OrdersDetails.GetAllRecords().AsQueryable();
18+
return Ok(data);
19+
}
20+
21+
/// <summary>
22+
/// Inserts a new order to the collection.
23+
/// </summary>
24+
/// <param name="addRecord">The order to be inserted.</param>
25+
/// <returns>It returns the newly inserted record detail.</returns>
26+
[HttpPost]
27+
[EnableQuery]
28+
public IActionResult Post([FromBody] OrdersDetails addRecord)
29+
{
30+
if (addRecord == null)
31+
{
32+
return BadRequest("Null order");
33+
}
34+
OrdersDetails.GetAllRecords().Insert(0, addRecord);
35+
return Json(addRecord);
36+
}
37+
38+
/// <summary>
39+
/// Updates an existing order.
40+
/// </summary>
41+
/// <param name="key">The ID of the order to update.</param>
42+
/// <param name="updateRecord">The updated order details.</param>
43+
/// <returns>It returns the updated order details.</returns>
44+
[HttpPatch("{key}")]
45+
public IActionResult Patch(int key, [FromBody] OrdersDetails updateRecord)
46+
{
47+
if (updateRecord == null)
48+
{
49+
return BadRequest("No records");
50+
}
51+
var existingOrder = OrdersDetails.GetAllRecords().FirstOrDefault(order => order.OrderID == key);
52+
if (existingOrder != null)
53+
{
54+
// If the order exists, update its properties
55+
existingOrder.CustomerID = updateRecord.CustomerID ?? existingOrder.CustomerID;
56+
existingOrder.EmployeeID = updateRecord.EmployeeID ?? existingOrder.EmployeeID;
57+
existingOrder.ShipCountry = updateRecord.ShipCountry ?? existingOrder.ShipCountry;
58+
}
59+
return Json(updateRecord);
60+
}
61+
62+
/// <summary>
63+
/// Deletes an order.
64+
/// </summary>
65+
/// <param name="key">The ID of the order to delete.</param>
66+
/// <returns>It returns the deleted record detail</returns>
67+
[HttpDelete("{key}")]
68+
public IActionResult Delete(int key)
69+
{
70+
var deleteRecord = OrdersDetails.GetAllRecords().FirstOrDefault(order => order.OrderID == key);
71+
if (deleteRecord != null)
72+
{
73+
OrdersDetails.GetAllRecords().Remove(deleteRecord);
74+
}
75+
return Json(deleteRecord);
76+
}
77+
}
78+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
3+
namespace CustomAdaptor.Controllers
4+
{
5+
[ApiController]
6+
[Route("[controller]")]
7+
public class WeatherForecastController : ControllerBase
8+
{
9+
private static readonly string[] Summaries = new[]
10+
{
11+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
12+
};
13+
14+
private readonly ILogger<WeatherForecastController> _logger;
15+
16+
public WeatherForecastController(ILogger<WeatherForecastController> logger)
17+
{
18+
_logger = logger;
19+
}
20+
21+
[HttpGet(Name = "GetWeatherForecast")]
22+
public IEnumerable<WeatherForecast> Get()
23+
{
24+
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
25+
{
26+
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
27+
TemperatureC = Random.Shared.Next(-20, 55),
28+
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
29+
})
30+
.ToArray();
31+
}
32+
}
33+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNetCore.OData" Version="9.2.1" />
11+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<ActiveDebugProfile>https</ActiveDebugProfile>
5+
</PropertyGroup>
6+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@CustomAdaptor_HostAddress = http://localhost:5286
2+
3+
GET {{CustomAdaptor_HostAddress}}/weatherforecast/
4+
Accept: application/json
5+
6+
###
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.13.35919.96 d17.13
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CustomAdaptor", "CustomAdaptor.csproj", "{94144981-A808-4936-9BE1-BABA4C056FA8}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{94144981-A808-4936-9BE1-BABA4C056FA8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{94144981-A808-4936-9BE1-BABA4C056FA8}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{94144981-A808-4936-9BE1-BABA4C056FA8}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{94144981-A808-4936-9BE1-BABA4C056FA8}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {5F94210D-948B-4031-A1AC-5D39E7CEEB4C}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace CustomAdaptor.Models
4+
{
5+
public class OrdersDetails
6+
{
7+
public static List<OrdersDetails> order = new List<OrdersDetails>();
8+
public OrdersDetails()
9+
{
10+
11+
}
12+
public OrdersDetails(
13+
int OrderID, string CustomerId, int EmployeeId, string ShipCountry)
14+
{
15+
this.OrderID = OrderID;
16+
this.CustomerID = CustomerId;
17+
this.EmployeeID = EmployeeId;
18+
this.ShipCountry = ShipCountry;
19+
}
20+
21+
public static List<OrdersDetails> GetAllRecords()
22+
{
23+
if (order.Count() == 0)
24+
{
25+
int code = 10000;
26+
for (int i = 1; i < 10; i++)
27+
{
28+
order.Add(new OrdersDetails(code + 1, "ALFKI", i + 0, "Denmark"));
29+
order.Add(new OrdersDetails(code + 2, "ANATR", i + 2, "Brazil"));
30+
order.Add(new OrdersDetails(code + 3, "ANTON", i + 1, "Germany"));
31+
order.Add(new OrdersDetails(code + 4, "BLONP", i + 3, "Austria"));
32+
order.Add(new OrdersDetails(code + 5, "BOLID", i + 4, "Switzerland"));
33+
code += 5;
34+
}
35+
}
36+
return order;
37+
}
38+
[Key]
39+
public int? OrderID { get; set; }
40+
public string? CustomerID { get; set; }
41+
public int? EmployeeID { get; set; }
42+
public string? ShipCountry { get; set; }
43+
}
44+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using CustomAdaptor.Models;
2+
using Microsoft.AspNetCore.OData;
3+
using Microsoft.OData.ModelBuilder;
4+
5+
6+
var builder = WebApplication.CreateBuilder(args);
7+
8+
// Add services to the container.
9+
10+
builder.Services.AddControllers();
11+
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
12+
builder.Services.AddEndpointsApiExplorer();
13+
builder.Services.AddSwaggerGen();
14+
15+
// Create an ODataConventionModelBuilder to build the OData model
16+
var modelBuilder = new ODataConventionModelBuilder();
17+
18+
// Register the "Orders" entity set with the OData model builder
19+
modelBuilder.EntitySet<OrdersDetails>("Orders");
20+
21+
// Add controllers with OData support to the service collection
22+
23+
var recordCount = OrdersDetails.GetAllRecords().Count;
24+
builder.Services.AddControllers().AddOData(
25+
options => options
26+
.Count()
27+
.Filter() //searching and filtering
28+
.Select()
29+
.Expand()
30+
.OrderBy()
31+
.SetMaxTop(recordCount)
32+
.AddRouteComponents("odata", modelBuilder.GetEdmModel()));
33+
34+
var app = builder.Build();
35+
app.UseDefaultFiles();
36+
app.UseStaticFiles();
37+
// Configure the HTTP request pipeline.
38+
if (app.Environment.IsDevelopment())
39+
{
40+
app.UseSwagger();
41+
app.UseSwaggerUI();
42+
}
43+
44+
app.UseHttpsRedirection();
45+
46+
app.UseAuthorization();
47+
48+
app.MapControllers();
49+
50+
app.Run();
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:26717",
8+
"sslPort": 44324
9+
}
10+
},
11+
"profiles": {
12+
"http": {
13+
"commandName": "Project",
14+
"dotnetRunMessages": true,
15+
"launchBrowser": true,
16+
"launchUrl": "swagger",
17+
"applicationUrl": "http://localhost:5286",
18+
"environmentVariables": {
19+
"ASPNETCORE_ENVIRONMENT": "Development"
20+
}
21+
},
22+
"https": {
23+
"commandName": "Project",
24+
"dotnetRunMessages": true,
25+
"launchBrowser": true,
26+
"launchUrl": "swagger",
27+
"applicationUrl": "https://localhost:7295;http://localhost:5286",
28+
"environmentVariables": {
29+
"ASPNETCORE_ENVIRONMENT": "Development"
30+
}
31+
},
32+
"IIS Express": {
33+
"commandName": "IISExpress",
34+
"launchBrowser": true,
35+
"launchUrl": "swagger",
36+
"environmentVariables": {
37+
"ASPNETCORE_ENVIRONMENT": "Development"
38+
}
39+
}
40+
}
41+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace CustomAdaptor
2+
{
3+
public class WeatherForecast
4+
{
5+
public DateOnly Date { get; set; }
6+
7+
public int TemperatureC { get; set; }
8+
9+
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
10+
11+
public string? Summary { get; set; }
12+
}
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>EJ2 Grid</title>
5+
<meta charset="utf-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<meta name="description" content="Javascript Grid Control">
8+
<meta name="author" content="Syncfusion">
9+
<link href="https://cdn.syncfusion.com/ej2/26.1.35/ej2-base/styles/bootstrap5.css" rel="stylesheet">
10+
<link href="https://cdn.syncfusion.com/ej2/26.1.35/ej2-grids/styles/bootstrap5.css" rel="stylesheet">
11+
<link href="https://cdn.syncfusion.com/ej2/26.1.35/ej2-buttons/styles/bootstrap5.css" rel="stylesheet">
12+
<link href="https://cdn.syncfusion.com/ej2/26.1.35/ej2-popups/styles/bootstrap5.css" rel="stylesheet">
13+
<link href="https://cdn.syncfusion.com/ej2/26.1.35/ej2-richtexteditor/styles/bootstrap5.css" rel="stylesheet">
14+
<link href="https://cdn.syncfusion.com/ej2/26.1.35/ej2-navigations/styles/bootstrap5.css" rel="stylesheet">
15+
<link href="https://cdn.syncfusion.com/ej2/26.1.35/ej2-dropdowns/styles/bootstrap5.css" rel="stylesheet">
16+
<link href="https://cdn.syncfusion.com/ej2/26.1.35/ej2-lists/styles/bootstrap5.css" rel="stylesheet">
17+
<link href="https://cdn.syncfusion.com/ej2/26.1.35/ej2-inputs/styles/bootstrap5.css" rel="stylesheet">
18+
<link href="https://cdn.syncfusion.com/ej2/26.1.35/ej2-calendars/styles/bootstrap5.css" rel="stylesheet">
19+
<link href="https://cdn.syncfusion.com/ej2/26.1.35/ej2-notifications/styles/bootstrap5.css" rel="stylesheet">
20+
<link href="https://cdn.syncfusion.com/ej2/26.1.35/ej2-splitbuttons/styles/bootstrap5.css" rel="stylesheet">
21+
<script src="https://cdn.syncfusion.com/ej2/26.1.35/dist/ej2.min.js" type="text/javascript"></script>
22+
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type="text/javascript"></script>
23+
</head>
24+
<body>
25+
<div id="container">
26+
<div id="Grid"></div>
27+
</div>
28+
<script type="module" src="js/index.js"></script>
29+
</body>
30+
</html>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export class CustomAdaptor extends ej.data.ODataV4Adaptor {
2+
processQuery(dm, query) {
3+
dm.dataSource.url = 'https://localhost:7295/odata/orders'; //Change the url.
4+
query.addParams('Syncfusion Grid', 'true'); // Add the additional parameter.
5+
return super.processQuery.apply(this, arguments);
6+
}
7+
8+
beforeSend(dm, request, settings) {
9+
request.headers.set('Authorization', `true`);
10+
super.beforeSend(dm, request, settings);
11+
}
12+
13+
processResponse() {
14+
let i = 0;
15+
const original = super.processResponse.apply(this, arguments);
16+
// Adding serial number.
17+
if (original.result) {
18+
original.result.forEach((item) => ej.base.setValue('SNo', ++i, item));
19+
}
20+
return original;
21+
}
22+
}

0 commit comments

Comments
 (0)