Skip to content

Commit 4b9bc37

Browse files
documentation(914729):Updated custom adaptor sample
1 parent 7b9773d commit 4b9bc37

25 files changed

+4195
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using Custom_Adaptor.Models;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.AspNetCore.OData.Query;
4+
5+
namespace Custom_Adaptor.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 Custom_Adaptor.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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
<None Remove="src\index.ts" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="Microsoft.AspNetCore.OData" Version="9.2.1" />
15+
<PackageReference Include="Microsoft.TypeScript.MSBuild" Version="5.8.1">
16+
<PrivateAssets>all</PrivateAssets>
17+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
18+
</PackageReference>
19+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
20+
</ItemGroup>
21+
22+
<ItemGroup>
23+
<Folder Include="wwwroot\" />
24+
</ItemGroup>
25+
26+
<ItemGroup>
27+
<TypeScriptCompile Include="src\index.ts" />
28+
</ItemGroup>
29+
30+
</Project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
<ItemGroup>
7+
<TypeScriptCompile Update="src\index.ts">
8+
<SubType>Code</SubType>
9+
</TypeScriptCompile>
10+
</ItemGroup>
11+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@Custom_Adaptor_HostAddress = http://localhost:5095
2+
3+
GET {{Custom_Adaptor_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}") = "Custom_Adaptor", "Custom_Adaptor.csproj", "{1FA05D43-DAB0-4107-8346-3ECBE2DF2EBE}"
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+
{1FA05D43-DAB0-4107-8346-3ECBE2DF2EBE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{1FA05D43-DAB0-4107-8346-3ECBE2DF2EBE}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{1FA05D43-DAB0-4107-8346-3ECBE2DF2EBE}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{1FA05D43-DAB0-4107-8346-3ECBE2DF2EBE}.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 = {BE800311-B1D4-4640-81E0-6CF271FA3E3A}
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 Custom_Adaptor.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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using Custom_Adaptor.Models;
2+
using Microsoft.AspNetCore.OData;
3+
using Microsoft.OData.ModelBuilder;
4+
5+
var builder = WebApplication.CreateBuilder(args);
6+
7+
// Add services to the container.
8+
var modelBuilder = new ODataConventionModelBuilder();
9+
modelBuilder.EntitySet<OrdersDetails>("Orders");
10+
11+
var recordCount = OrdersDetails.GetAllRecords().Count;
12+
13+
builder.Services.AddControllers().AddOData(
14+
options => options
15+
.Count()
16+
.OrderBy()
17+
.Filter()
18+
.SetMaxTop(recordCount)
19+
.AddRouteComponents(
20+
"odata",
21+
modelBuilder.GetEdmModel()));
22+
23+
24+
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
25+
builder.Services.AddEndpointsApiExplorer();
26+
builder.Services.AddSwaggerGen();
27+
28+
builder.Services.AddCors(options =>
29+
{
30+
options.AddDefaultPolicy(builder =>
31+
{
32+
builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();
33+
});
34+
});
35+
var app = builder.Build();
36+
app.UseCors();
37+
38+
app.UseDefaultFiles();
39+
app.UseStaticFiles();
40+
41+
// Configure the HTTP request pipeline.
42+
if (app.Environment.IsDevelopment())
43+
{
44+
app.UseSwagger();
45+
app.UseSwaggerUI();
46+
}
47+
48+
app.UseHttpsRedirection();
49+
50+
app.UseAuthorization();
51+
52+
app.MapControllers();
53+
54+
app.MapFallbackToFile("/index.html");
55+
56+
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:23541",
8+
"sslPort": 44304
9+
}
10+
},
11+
"profiles": {
12+
"http": {
13+
"commandName": "Project",
14+
"dotnetRunMessages": true,
15+
"launchBrowser": true,
16+
"launchUrl": "swagger",
17+
"applicationUrl": "http://localhost:5095",
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:7048;http://localhost:5095",
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 Custom_Adaptor
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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "custom_adaptor",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"build": "webpack --mode=development --watch",
8+
"release": "webpack --mode=production",
9+
"publish": "npm run release && dotnet publish -c Release"
10+
},
11+
"keywords": [],
12+
"author": "",
13+
"license": "ISC",
14+
"devDependencies": {
15+
"clean-webpack-plugin": "4.0.0",
16+
"@syncfusion/ej2-data": "^26.1.35",
17+
"@syncfusion/ej2-grids": "^26.1.38",
18+
"css-loader": "7.1.2",
19+
"html-webpack-plugin": "5.6.3",
20+
"mini-css-extract-plugin": "2.9.2",
21+
"ts-loader": "9.5.2",
22+
"typescript": "5.8.3",
23+
"webpack": "5.99.0",
24+
"webpack-cli": "6.0.1"
25+
}
26+
}

0 commit comments

Comments
 (0)