Skip to content

Commit dfb1d33

Browse files
committed
Minor refactoring
1 parent 8814df5 commit dfb1d33

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed

README.md

+71-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ PM> Install-Package Sql-Server-Rest-Api
1212
You will need to configure data access components in Startup class (Configure service method):
1313

1414
```
15-
using SqlServerRestApi.SQL;
15+
using SqlServerRestApi;
1616
17+
namespace MyApp {
1718
1819
public class Startup
1920
{
@@ -24,8 +25,26 @@ using SqlServerRestApi.SQL;
2425
services.AddSqlClient(Configuration["ConnectionStrings:MyConnection"]);
2526
}
2627
28+
}
29+
}
30+
```
31+
Assumption in this example is that your connection string is stored in appsettings.config file under key MyConnection.
32+
33+
```
34+
{
35+
"Logging": {
36+
"IncludeScopes": false,
37+
"LogLevel": {
38+
"Default": "Debug",
39+
"System": "Information",
40+
"Microsoft": "Information"
41+
}
42+
},
43+
"ConnectionStrings": {
44+
"MyConnection": "Server=.;Database=MyDatabase;Integrated Security=true"
45+
}
46+
}
2747
```
28-
Assumption in this example is that your connection string is stored in application.config file under key MyConnection.
2948

3049
# Create ASP.NET Controller that will serve Http requests
3150

@@ -95,4 +114,53 @@ First, you need to parse Request parameters using UriParser in order to extract
95114
}
96115
```
97116

98-
That's everything that you need to do. With three lines of code you can get OData service on any table.
117+
That's everything that you need to do. With three lines of code you can get OData service on any table.
118+
119+
120+
## Implement REST service that process JQuery DataTables Ajax request
121+
122+
[JQuery DataTables](https://datatables.net/) is JQuery component that enhances HTML tables and adds rich client-side functionalities such as filtering, pagination, ordering by columns, etc. JQuery DataTables component might work in two modes:
123+
- Client-side mode where rows are loaded into the table in browser, and then all sorting, filering and pagination operations are done via JavaScript.
124+
- Server-side mode where AJAX request with informaiton about the curent page, sort/filter condition, is sent to the server, and REST API should return results that should be shown in the table.
125+
126+
In order to configure JQuery DataTables in server-side processing mode, you need to put an empty HTML table in your HTML page, and specify that DataTables plugin should be applied on this page with the following options:
127+
```
128+
$('#mytable').DataTable({
129+
"serverSide": true,
130+
"ajax": "/api/People",
131+
"columns": [
132+
{ "data": "name", "width": "10%" },
133+
{ "data": "surname", "width": "10%" },
134+
{ "data": "address", "width": "50%" },
135+
{ "data": "town", "width": "10%" }
136+
]
137+
});
138+
```
139+
Option "serverSide" will tell DataTables plugin to send AJAX request to the service that will return results that should be shown. Url of the service is defined in "ajax" option. The last option is list of the columns that shoudl be shown.
140+
To implement REST service that handles AJAX requests that JQuery DataTables sends in server-side mode, you would need to add the TableSpec object that describes the structure of the table that will be queried (name and columns). An example is shown in the following code:
141+
```
142+
IQueryPipe sqlQuery = null;
143+
144+
TableSpec tableSpec = new TableSpec("dbo.People", "name,surname,address,town");
145+
146+
public PeopleController(IQueryPipe sqlQueryService)
147+
{
148+
this.sqlQuery = sqlQueryService;
149+
}
150+
```
151+
152+
Now you need to create async method that will serve JQuery DataTables AJAX requests with following classes:
153+
- UriParser that will parse Http request parameters that JQuery DataTables component sends
154+
- QueryBuilder that will create T-SQL query that will be executed.
155+
First, you need to parse Request parameters using UriParser in order to extract the definition of query (QuerySpec object). Then you need to use QueryBuilder to create SQL query using the QuerySpec. Then you need to provide sql query to QueryPipe that will stream results to JQuery DataTables using Response.Body:
156+
157+
```
158+
// GET api/People
159+
[HttpGet]
160+
public async Task Get()
161+
{
162+
var querySpec = JQueryDataTables.UriParser.Parse(tableSpec, this.Request);
163+
var sql = QueryBuilder.Build(querySpec, tableSpec).AsJson();
164+
await sqlQuery.Stream(sql, Response.Body, "[]");
165+
}
166+
```

StartUp.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using Microsoft.Extensions.DependencyInjection;
44
using System.Data.SqlClient;
55

6-
namespace SqlServerRestApi.SQL
6+
namespace SqlServerRestApi
77
{
88
public static class StartUp
99
{

0 commit comments

Comments
 (0)