Skip to content

Commit f16e976

Browse files
939923: Added the custom adaptor sample in Binding data from remote service to vue data grid repository
1 parent fbdf2d9 commit f16e976

38 files changed

+1063
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Microsoft.AspNetCore.OData.Query;
3+
using Microsoft.AspNetCore.OData.Routing.Controllers;
4+
using ODataV4Adaptor.Server.Models;
5+
6+
namespace OdataV4Adaptor.Server.Controllers
7+
{
8+
9+
public class OrdersController : Controller
10+
{
11+
/// <summary>
12+
/// Retrieves all orders.
13+
/// </summary>
14+
/// <returns>The collection of orders.</returns>
15+
[HttpGet]
16+
[EnableQuery]
17+
public IActionResult Get()
18+
{
19+
var data = OrdersDetails.GetAllRecords().AsQueryable();
20+
return Ok(data);
21+
}
22+
23+
/// <summary>
24+
/// Inserts a new order to the collection.
25+
/// </summary>
26+
/// <param name="addRecord">The order to be inserted.</param>
27+
/// <returns>It returns the newly inserted record detail.</returns>
28+
[HttpPost]
29+
[EnableQuery]
30+
public IActionResult Post([FromBody] OrdersDetails addRecord)
31+
{
32+
if (addRecord == null)
33+
{
34+
return BadRequest("Null order");
35+
}
36+
OrdersDetails.GetAllRecords().Insert(0, addRecord);
37+
return Json(addRecord);
38+
}
39+
40+
/// <summary>
41+
/// Updates an existing order.
42+
/// </summary>
43+
/// <param name="key">The ID of the order to update.</param>
44+
/// <param name="updateRecord">The updated order details.</param>
45+
/// <returns>It returns the updated order details.</returns>
46+
[HttpPatch("{key}")]
47+
public IActionResult Patch(int key, [FromBody] OrdersDetails updateRecord)
48+
{
49+
if (updateRecord == null)
50+
{
51+
return BadRequest("No records");
52+
}
53+
var existingOrder = OrdersDetails.GetAllRecords().FirstOrDefault(order => order.OrderID == key);
54+
if (existingOrder != null)
55+
{
56+
// If the order exists, update its properties
57+
existingOrder.CustomerID = updateRecord.CustomerID ?? existingOrder.CustomerID;
58+
existingOrder.EmployeeID = updateRecord.EmployeeID ?? existingOrder.EmployeeID;
59+
existingOrder.ShipCountry = updateRecord.ShipCountry ?? existingOrder.ShipCountry;
60+
}
61+
return Json(updateRecord);
62+
}
63+
64+
/// <summary>
65+
/// Deletes an order.
66+
/// </summary>
67+
/// <param name="key">The ID of the order to delete.</param>
68+
/// <returns>It returns the deleted record detail</returns>
69+
[HttpDelete("{key}")]
70+
public IActionResult Delete(int key)
71+
{
72+
var deleteRecord = OrdersDetails.GetAllRecords().FirstOrDefault(order => order.OrderID == key);
73+
if (deleteRecord != null)
74+
{
75+
OrdersDetails.GetAllRecords().Remove(deleteRecord);
76+
}
77+
return Json(deleteRecord);
78+
}
79+
}
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
3+
namespace ODataV4Adaptor.Server.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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace ODataV4Adaptor.Server.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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<SpaRoot>..\odatav4adaptor.client</SpaRoot>
8+
<SpaProxyLaunchCommand>npm run dev</SpaProxyLaunchCommand>
9+
<SpaProxyServerUrl>https://localhost:5173</SpaProxyServerUrl>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="Microsoft.AspNetCore.OData" Version="8.2.5" />
14+
<PackageReference Include="Microsoft.AspNetCore.SpaProxy">
15+
<Version>8.*-*</Version>
16+
</PackageReference>
17+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<ProjectReference Include="..\odatav4adaptor.client\odatav4adaptor.client.esproj">
22+
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
23+
</ProjectReference>
24+
</ItemGroup>
25+
26+
</Project>
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>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@ODataV4Adaptor.Server_HostAddress = http://localhost:5241
2+
3+
GET {{ODataV4Adaptor.Server_HostAddress}}/weatherforecast/
4+
Accept: application/json
5+
6+
###
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using Microsoft.AspNetCore.OData;
2+
using Microsoft.OData.ModelBuilder;
3+
using ODataV4Adaptor.Server.Models;
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();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:17310",
8+
"sslPort": 44346
9+
}
10+
},
11+
"profiles": {
12+
"http": {
13+
"commandName": "Project",
14+
"dotnetRunMessages": true,
15+
"launchBrowser": true,
16+
"launchUrl": "swagger",
17+
"applicationUrl": "http://localhost:5241",
18+
"environmentVariables": {
19+
"ASPNETCORE_ENVIRONMENT": "Development",
20+
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy"
21+
}
22+
},
23+
"https": {
24+
"commandName": "Project",
25+
"dotnetRunMessages": true,
26+
"launchBrowser": true,
27+
"launchUrl": "swagger",
28+
"applicationUrl": "https://localhost:7215;http://localhost:5241",
29+
"environmentVariables": {
30+
"ASPNETCORE_ENVIRONMENT": "Development",
31+
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy"
32+
}
33+
},
34+
"IIS Express": {
35+
"commandName": "IISExpress",
36+
"launchBrowser": true,
37+
"launchUrl": "swagger",
38+
"environmentVariables": {
39+
"ASPNETCORE_ENVIRONMENT": "Development",
40+
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy"
41+
}
42+
}
43+
}
44+
}
45+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace ODataV4Adaptor.Server
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+
}
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+
}
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+
}

CustomAdaptor/CustomAdaptor.sln

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.Server", "ODataV4Adaptor.Server\ODataV4Adaptor.Server.csproj", "{1C30C5B8-D458-4654-AAC3-D1865BA6B333}"
7+
EndProject
8+
Project("{54A90642-561A-4BB1-A94E-469ADEE60C69}") = "odatav4adaptor.client", "odatav4adaptor.client\odatav4adaptor.client.esproj", "{08AB8A24-B687-420F-824A-830C3AFA5F87}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{1C30C5B8-D458-4654-AAC3-D1865BA6B333}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{1C30C5B8-D458-4654-AAC3-D1865BA6B333}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{1C30C5B8-D458-4654-AAC3-D1865BA6B333}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{1C30C5B8-D458-4654-AAC3-D1865BA6B333}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{08AB8A24-B687-420F-824A-830C3AFA5F87}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{08AB8A24-B687-420F-824A-830C3AFA5F87}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{08AB8A24-B687-420F-824A-830C3AFA5F87}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
23+
{08AB8A24-B687-420F-824A-830C3AFA5F87}.Release|Any CPU.ActiveCfg = Release|Any CPU
24+
{08AB8A24-B687-420F-824A-830C3AFA5F87}.Release|Any CPU.Build.0 = Release|Any CPU
25+
{08AB8A24-B687-420F-824A-830C3AFA5F87}.Release|Any CPU.Deploy.0 = Release|Any CPU
26+
EndGlobalSection
27+
GlobalSection(SolutionProperties) = preSolution
28+
HideSolutionNode = FALSE
29+
EndGlobalSection
30+
GlobalSection(ExtensibilityGlobals) = postSolution
31+
SolutionGuid = {57156135-2E52-491B-AE1F-B50B2C97612A}
32+
EndGlobalSection
33+
EndGlobal

0 commit comments

Comments
 (0)