Skip to content

Commit 22ffb3b

Browse files
Adding sample for WebApiAdaptor
1 parent 557e67f commit 22ffb3b

22 files changed

+4181
-0
lines changed

WebApiAdaptor/WebApiAdaptor.sln

+25
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}") = "WebApiAdaptor", "WebApiAdaptor\WebApiAdaptor.csproj", "{B49DB687-BD93-4E24-81A7-BC203C7A013C}"
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+
{B49DB687-BD93-4E24-81A7-BC203C7A013C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{B49DB687-BD93-4E24-81A7-BC203C7A013C}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{B49DB687-BD93-4E24-81A7-BC203C7A013C}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{B49DB687-BD93-4E24-81A7-BC203C7A013C}.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 = {9B172FC0-45F9-4429-9368-D37F6CFE6DAE}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
using Microsoft.AspNetCore.Http;
2+
using Microsoft.AspNetCore.Mvc;
3+
using WebApiAdaptor.Models;
4+
5+
namespace WebApiAdaptor.Controllers
6+
{
7+
[Route("api/[controller]")]
8+
9+
[ApiController]
10+
public class GridController : ControllerBase
11+
{
12+
// GET: api/Orders
13+
[HttpGet]
14+
15+
// Action to retrieve orders
16+
public object Get()
17+
{
18+
var queryString = Request.Query;
19+
var data = OrdersDetails.GetAllRecords().ToList();
20+
21+
string? sort = queryString["$orderby"]; // Get sorting parameter
22+
string? filter = queryString["$filter"]; // // Get filtering parameter
23+
24+
//Peform sort operation
25+
if (!string.IsNullOrEmpty(sort))
26+
{
27+
var sortConditions = sort.Split(',');
28+
29+
var orderedData = data.OrderBy(x => 0); // Start with a stable sort
30+
31+
foreach (var sortCondition in sortConditions)
32+
{
33+
var sortParts = sortCondition.Trim().Split(' ');
34+
var sortBy = sortParts[0];
35+
var sortOrder = sortParts.Length > 1 && sortParts[1].ToLower() == "desc";
36+
37+
switch (sortBy)
38+
{
39+
case "OrderID":
40+
orderedData = sortOrder ? orderedData.ThenByDescending(x => x.OrderID) : orderedData.ThenBy(x => x.OrderID);
41+
break;
42+
case "CustomerID":
43+
orderedData = sortOrder ? orderedData.ThenByDescending(x => x.CustomerID) : orderedData.ThenBy(x => x.CustomerID);
44+
break;
45+
case "ShipCity":
46+
orderedData = sortOrder ? orderedData.ThenByDescending(x => x.ShipCity) : orderedData.ThenBy(x => x.ShipCity);
47+
break;
48+
}
49+
}
50+
51+
data = [.. orderedData];
52+
}
53+
if (filter != null)
54+
{
55+
var filters = filter.Split(new string[] { " and " }, StringSplitOptions.RemoveEmptyEntries);
56+
57+
foreach (var filterItem in filters)
58+
{
59+
if (filterItem.Contains("substringof"))
60+
{
61+
// Performing Search operation
62+
63+
var searchParts = filterItem.Split('(', ')', '\'');
64+
var searchValue = searchParts[3];
65+
66+
// Apply the search value to all searchable fields
67+
data = data.Where(cust =>
68+
cust != null &&
69+
((cust.OrderID?.ToString()?.Contains(searchValue) ?? false) ||
70+
(cust.CustomerID?.ToLower()?.Contains(searchValue) ?? false) ||
71+
(cust.ShipCity?.ToLower()?.Contains(searchValue) ?? false))).ToList();
72+
}
73+
else
74+
{
75+
// Performing filter operation
76+
77+
var filterfield = "";
78+
var filtervalue = "";
79+
var filterParts = filterItem.Split('(', ')', '\'');
80+
if (filterParts.Length != 9)
81+
{
82+
var filterValueParts = filterParts[1].Split();
83+
filterfield = filterValueParts[0];
84+
filtervalue = filterValueParts[2];
85+
}
86+
else
87+
{
88+
filterfield = filterParts[3];
89+
filtervalue = filterParts[5];
90+
}
91+
switch (filterfield)
92+
{
93+
case "OrderID":
94+
data = data.Where(cust => cust != null && cust.OrderID?.ToString() == filtervalue.ToString()).ToList();
95+
break;
96+
case "CustomerID":
97+
data = data.Where(cust => cust != null && cust.CustomerID?.ToLower().StartsWith(filtervalue.ToString()) == true).ToList();
98+
break;
99+
case "ShipCity":
100+
data = data.Where(cust => cust != null && cust.ShipCity?.ToLower().StartsWith(filtervalue.ToString()) == true).ToList();
101+
break;
102+
// Add more cases for other searchable fields if needed
103+
}
104+
105+
}
106+
}
107+
}
108+
109+
int TotalRecordsCount = data.Count;
110+
111+
//Perform page operation
112+
113+
int skip = Convert.ToInt32(queryString["$skip"]);
114+
int take = Convert.ToInt32(queryString["$top"]);
115+
if (take != 0)
116+
{
117+
data = data.Skip(skip).Take(take).ToList();
118+
119+
}
120+
121+
return new { result = data, count = TotalRecordsCount };
122+
123+
}
124+
125+
126+
// POST: api/Orders
127+
[HttpPost]
128+
/// <summary>
129+
/// Inserts a new data item into the data collection.
130+
/// </summary>
131+
/// <param name="value">It holds new record detail which is need to be inserted.</param>
132+
/// <returns>Returns void</returns>
133+
public void Post([FromBody] OrdersDetails newRecord)
134+
{
135+
// Insert a new record into the OrdersDetails model
136+
OrdersDetails.GetAllRecords().Insert(0, newRecord);
137+
}
138+
139+
// PUT: api/Orders/5
140+
[HttpPut]
141+
/// <summary>
142+
/// Update a existing data item from the data collection.
143+
/// </summary>
144+
/// <param name="order">It holds updated record detail which is need to be updated.</param>
145+
/// <returns>Returns void</returns>
146+
public void Put(int id, [FromBody] OrdersDetails order)
147+
{
148+
// Find the existing order by ID
149+
var existingOrder = OrdersDetails.GetAllRecords().FirstOrDefault(o => o.OrderID == id);
150+
if (existingOrder != null)
151+
{
152+
// If the order exists, update its properties
153+
existingOrder.OrderID = order.OrderID;
154+
existingOrder.CustomerID = order.CustomerID;
155+
existingOrder.ShipCity = order.ShipCity;
156+
}
157+
}
158+
159+
// DELETE: api/Orders/5
160+
[HttpDelete("{id}")]
161+
/// <summary>
162+
/// Remove a specific data item from the data collection.
163+
/// </summary>
164+
/// <param name="id">It holds specific record detail id which is need to be removed.</param>
165+
/// <returns>Returns void</returns>
166+
public void Delete(int id)
167+
{
168+
// Find the order to remove by ID
169+
var orderToRemove = OrdersDetails.GetAllRecords().FirstOrDefault(order => order.OrderID == id);
170+
// If the order exists, remove it
171+
if (orderToRemove != null)
172+
{
173+
OrdersDetails.GetAllRecords().Remove(orderToRemove);
174+
}
175+
}
176+
}
177+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
3+
namespace WebApiAdaptor.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,58 @@
1+
namespace WebApiAdaptor.Models
2+
{
3+
public class OrdersDetails
4+
{
5+
public static List<OrdersDetails> order = new List<OrdersDetails>();
6+
public OrdersDetails()
7+
{
8+
9+
}
10+
public OrdersDetails(
11+
int OrderID, string CustomerId, int EmployeeId, double Freight, bool Verified,
12+
DateTime OrderDate, string ShipCity, string ShipName, string ShipCountry,
13+
DateTime ShippedDate, string ShipAddress)
14+
{
15+
this.OrderID = OrderID;
16+
this.CustomerID = CustomerId;
17+
this.EmployeeID = EmployeeId;
18+
this.Freight = Freight;
19+
this.ShipCity = ShipCity;
20+
this.Verified = Verified;
21+
this.OrderDate = OrderDate;
22+
this.ShipName = ShipName;
23+
this.ShipCountry = ShipCountry;
24+
this.ShippedDate = ShippedDate;
25+
this.ShipAddress = ShipAddress;
26+
}
27+
28+
public static List<OrdersDetails> GetAllRecords()
29+
{
30+
if (order.Count == 0)
31+
{
32+
int code = 10000;
33+
for (int i = 1; i < 10; i++)
34+
{
35+
order.Add(new OrdersDetails(code + 1, "ALFKI", i + 0, 2.3 * i, false, new DateTime(1991, 05, 15), "Berlin", "Simons bistro", "Denmark", new DateTime(1996, 7, 16), "Kirchgasse 6"));
36+
order.Add(new OrdersDetails(code + 2, "ANATR", i + 2, 3.3 * i, true, new DateTime(1990, 04, 04), "Madrid", "Queen Cozinha", "Brazil", new DateTime(1996, 9, 11), "Avda. Azteca 123"));
37+
order.Add(new OrdersDetails(code + 3, "ANTON", i + 1, 4.3 * i, true, new DateTime(1957, 11, 30), "Cholchester", "Frankenversand", "Germany", new DateTime(1996, 10, 7), "Carrera 52 con Ave. Bolívar #65-98 Llano Largo"));
38+
order.Add(new OrdersDetails(code + 4, "BLONP", i + 3, 5.3 * i, false, new DateTime(1930, 10, 22), "Marseille", "Ernst Handel", "Austria", new DateTime(1996, 12, 30), "Magazinweg 7"));
39+
order.Add(new OrdersDetails(code + 5, "BOLID", i + 4, 6.3 * i, true, new DateTime(1953, 02, 18), "Tsawassen", "Hanari Carnes", "Switzerland", new DateTime(1997, 12, 3), "1029 - 12th Ave. S."));
40+
code += 5;
41+
}
42+
}
43+
return order;
44+
}
45+
46+
public int? OrderID { get; set; }
47+
public string? CustomerID { get; set; }
48+
public int? EmployeeID { get; set; }
49+
public double? Freight { get; set; }
50+
public string? ShipCity { get; set; }
51+
public bool? Verified { get; set; }
52+
public DateTime OrderDate { get; set; }
53+
public string? ShipName { get; set; }
54+
public string? ShipCountry { get; set; }
55+
public DateTime ShippedDate { get; set; }
56+
public string? ShipAddress { get; set; }
57+
}
58+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var builder = WebApplication.CreateBuilder(args);
2+
3+
// Add services to the container.
4+
5+
builder.Services.AddControllers();
6+
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
7+
builder.Services.AddEndpointsApiExplorer();
8+
builder.Services.AddSwaggerGen();
9+
10+
var app = builder.Build();
11+
12+
app.UseDefaultFiles();
13+
app.UseStaticFiles();
14+
// Configure the HTTP request pipeline.
15+
if (app.Environment.IsDevelopment())
16+
{
17+
app.UseSwagger();
18+
app.UseSwaggerUI();
19+
}
20+
21+
app.UseHttpsRedirection();
22+
23+
app.UseAuthorization();
24+
25+
app.MapControllers();
26+
27+
app.Run();
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:3916",
8+
"sslPort": 44313
9+
}
10+
},
11+
"profiles": {
12+
"http": {
13+
"commandName": "Project",
14+
"dotnetRunMessages": true,
15+
"launchBrowser": true,
16+
"launchUrl": "swagger",
17+
"applicationUrl": "http://localhost:5161",
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:7096;http://localhost:5161",
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace WebApiAdaptor
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+
}

0 commit comments

Comments
 (0)