Skip to content

Commit 3f65702

Browse files
Documentation(EJ2-890211): OdataV4adaptor
1 parent ee870a4 commit 3f65702

14 files changed

+389
-0
lines changed

ODataV4Adaptor/ODataV4Adaptor.sln

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.9.34728.123
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ODataV4Adaptor", "ODataV4Adaptor\ODataV4Adaptor.csproj", "{CAB1491F-D43B-4513-9A9D-F5A56AA8F093}"
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+
{CAB1491F-D43B-4513-9A9D-F5A56AA8F093}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{CAB1491F-D43B-4513-9A9D-F5A56AA8F093}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{CAB1491F-D43B-4513-9A9D-F5A56AA8F093}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{CAB1491F-D43B-4513-9A9D-F5A56AA8F093}.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 = {C4A3478D-2390-4B78-982F-E0AC583C0E9D}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Microsoft.AspNetCore.OData.Query;
3+
using Microsoft.AspNetCore.OData.Routing.Controllers;
4+
using ODataV4Adaptor.Models;
5+
namespace OdataV4Adaptor.Controllers
6+
{
7+
8+
public class OrdersController : Controller
9+
{
10+
/// <summary>
11+
/// Retrieves all orders.
12+
/// </summary>
13+
/// <returns>The collection of orders.</returns>
14+
[HttpGet]
15+
[EnableQuery]
16+
public IActionResult Get()
17+
{
18+
var data = OrdersDetails.GetAllRecords().AsQueryable();
19+
return Ok(data);
20+
}
21+
22+
/// <summary>
23+
/// Inserts a new order to the collection.
24+
/// </summary>
25+
/// <param name="addRecord">The order to be inserted.</param>
26+
/// <returns>It returns the newly inserted record detail.</returns>
27+
[HttpPost]
28+
[EnableQuery]
29+
public IActionResult Post([FromBody] OrdersDetails addRecord)
30+
{
31+
if (addRecord == null)
32+
{
33+
return BadRequest("Null order");
34+
}
35+
OrdersDetails.GetAllRecords().Insert(0, addRecord);
36+
return Json(addRecord);
37+
}
38+
39+
/// <summary>
40+
/// Updates an existing order.
41+
/// </summary>
42+
/// <param name="key">The ID of the order to update.</param>
43+
/// <param name="updateRecord">The updated order details.</param>
44+
/// <returns>It returns the updated order details.</returns>
45+
[HttpPatch("{key}")]
46+
public IActionResult Patch(int key, [FromBody] OrdersDetails updateRecord)
47+
{
48+
if (updateRecord == null)
49+
{
50+
return BadRequest("No records");
51+
}
52+
var existingOrder = OrdersDetails.GetAllRecords().FirstOrDefault(order => order.OrderID == key);
53+
if (existingOrder != null)
54+
{
55+
// If the order exists, update its properties
56+
existingOrder.CustomerID = updateRecord.CustomerID ?? existingOrder.CustomerID;
57+
existingOrder.ShipCity = updateRecord.ShipCity ?? existingOrder.ShipCity;
58+
existingOrder.ShipCountry = updateRecord.ShipCountry ?? existingOrder.ShipCountry;
59+
}
60+
return Json(updateRecord);
61+
}
62+
63+
/// <summary>
64+
/// Deletes an order.
65+
/// </summary>
66+
/// <param name="key">The ID of the order to delete.</param>
67+
/// <returns>It returns the deleted record detail</returns>
68+
[HttpDelete("{key}")]
69+
public IActionResult Delete(int key)
70+
{
71+
var deleteRecord = OrdersDetails.GetAllRecords().FirstOrDefault(order => order.OrderID == key);
72+
if (deleteRecord != null)
73+
{
74+
OrdersDetails.GetAllRecords().Remove(deleteRecord);
75+
}
76+
return Json(deleteRecord);
77+
}
78+
}
79+
}
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 ODataV4Adaptor.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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace ODataV4Adaptor.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, string ShipCity, string ShipCountry)
14+
{
15+
this.OrderID = OrderID;
16+
this.CustomerID = CustomerId;
17+
this.ShipCity = ShipCity;
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","Berlin", "Denmark"));
29+
order.Add(new OrdersDetails(code + 2, "ANATR", "Madrid", "Brazil"));
30+
order.Add(new OrdersDetails(code + 3, "ANTON", "Cholchester", "Germany"));
31+
order.Add(new OrdersDetails(code + 4, "BLONP", "Marseille", "Austria"));
32+
order.Add(new OrdersDetails(code + 5, "BOLID", "tsawassen", "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 string? ShipCity { get; set; }
42+
public string? ShipCountry { get; set; }
43+
}
44+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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="8.2.5" />
11+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<Folder Include="wwwroot\css\" />
16+
</ItemGroup>
17+
18+
</Project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
<Controller_SelectedScaffolderID>ApiControllerEmptyScaffolder</Controller_SelectedScaffolderID>
6+
<Controller_SelectedScaffolderCategoryPath>root/Common/Api</Controller_SelectedScaffolderCategoryPath>
7+
</PropertyGroup>
8+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@ODataV4Adaptor_HostAddress = http://localhost:5214
2+
3+
GET {{ODataV4Adaptor_HostAddress}}/weatherforecast/
4+
Accept: application/json
5+
6+
###
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using Microsoft.AspNetCore.OData;
2+
using Microsoft.OData.ModelBuilder;
3+
using ODataV4Adaptor.Models;
4+
5+
var builder = WebApplication.CreateBuilder(args);
6+
7+
// Add services to the container.
8+
9+
builder.Services.AddControllers();
10+
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
11+
builder.Services.AddEndpointsApiExplorer();
12+
builder.Services.AddSwaggerGen();
13+
14+
// Create an ODataConventionModelBuilder to build the OData model
15+
var modelBuilder = new ODataConventionModelBuilder();
16+
17+
// Register the "Orders" entity set with the OData model builder
18+
modelBuilder.EntitySet<OrdersDetails>("Orders");
19+
20+
var recordCount = OrdersDetails.GetAllRecords().Count;
21+
22+
builder.Services.AddControllers().AddOData(
23+
options => options
24+
.Count()
25+
.OrderBy()
26+
.Filter()
27+
.SetMaxTop(recordCount)
28+
.AddRouteComponents(
29+
"odata",
30+
modelBuilder.GetEdmModel()));
31+
32+
var app = builder.Build();
33+
34+
app.UseDefaultFiles();
35+
app.UseStaticFiles();
36+
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:5430",
8+
"sslPort": 44372
9+
}
10+
},
11+
"profiles": {
12+
"http": {
13+
"commandName": "Project",
14+
"dotnetRunMessages": true,
15+
"launchBrowser": true,
16+
"launchUrl": "swagger",
17+
"applicationUrl": "http://localhost:5214",
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:7047;http://localhost:5214",
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 ODataV4Adaptor
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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>ODataV4Adaptor</title>
5+
<meta charset="utf-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<meta name="description" content="Typescript Grid Control">
8+
<meta name="author" content="Syncfusion">
9+
<link href="https://cdn.syncfusion.com/ej2/26.1.35/ej2-base/styles/material.css" rel="stylesheet">
10+
<link href="https://cdn.syncfusion.com/ej2/26.1.35/ej2-grids/styles/material.css" rel="stylesheet">
11+
<link href="https://cdn.syncfusion.com/ej2/26.1.35/ej2-buttons/styles/material.css" rel="stylesheet">
12+
<link href="https://cdn.syncfusion.com/ej2/26.1.35/ej2-popups/styles/material.css" rel="stylesheet">
13+
<link href="https://cdn.syncfusion.com/ej2/26.1.35/ej2-richtexteditor/styles/material.css" rel="stylesheet">
14+
<link href="https://cdn.syncfusion.com/ej2/26.1.35/ej2-navigations/styles/material.css" rel="stylesheet">
15+
<link href="https://cdn.syncfusion.com/ej2/26.1.35/ej2-dropdowns/styles/material.css" rel="stylesheet">
16+
<link href="https://cdn.syncfusion.com/ej2/26.1.35/ej2-lists/styles/material.css" rel="stylesheet">
17+
<link href="https://cdn.syncfusion.com/ej2/26.1.35/ej2-inputs/styles/material.css" rel="stylesheet">
18+
<link href="https://cdn.syncfusion.com/ej2/26.1.35/ej2-calendars/styles/material.css" rel="stylesheet">
19+
<link href="https://cdn.syncfusion.com/ej2/26.1.35/ej2-notifications/styles/material.css" rel="stylesheet">
20+
<link href="https://cdn.syncfusion.com/ej2/26.1.35/ej2-splitbuttons/styles/material.css" rel="stylesheet">
21+
22+
<script src="https://cdn.syncfusion.com/ej2/26.1.35/dist/ej2.min.js" type="text/javascript"></script>
23+
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type="text/javascript"></script>
24+
</head>
25+
<body>
26+
27+
<div id="container">
28+
<div id="Grid"></div>
29+
</div>
30+
31+
<script src="js/index.js" type="text/javascript"></script>
32+
</body>
33+
</html>

0 commit comments

Comments
 (0)