Skip to content

Commit 42cc7a8

Browse files
authored
Updated doc
1 parent 14c95a0 commit 42cc7a8

File tree

1 file changed

+14
-25
lines changed

1 file changed

+14
-25
lines changed

README.md

+14-25
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This library enables you to easily create REST services in ASP.NET based on the
66

77
Get REST API library from NuGet:
88
```
9-
PM> Install-Package Sql-Server-Rest-Api
9+
PM> Install-Package MsSql.RestApi
1010
```
1111

1212
You will need to configure data access components in Startup class (Configure service method):
@@ -25,7 +25,7 @@ namespace MyApp {
2525
services.AddSqlClient(Configuration["ConnectionStrings:MyConnection"]);
2626
}
2727
28-
}
28+
}
2929
}
3030
```
3131
Assumption in this example is that your connection string is stored in appsettings.config file under key MyConnection.
@@ -90,7 +90,7 @@ To implement OData Service, you would need to add the TableSpec object that desc
9090
```
9191
IQueryPipe sqlQuery = null;
9292
93-
TableSpec tableSpec = new TableSpec("dbo.People", "name,surname,address,town");
93+
TableSpec tableSpec = new TableSpec(schema: "dbo", table: "People", columns: "name,surname,address,town");
9494
9595
public PeopleController(IQueryPipe sqlQueryService)
9696
{
@@ -107,13 +107,15 @@ First, you need to parse Request parameters using UriParser in order to extract
107107
public async Task OData()
108108
{
109109
await this
110-
.ODataHandler(tableSpec, pipe)
111-
.Process();
110+
.OData(tableSpec)
111+
.Process(pipe);
112112
}
113113
```
114114

115115
That's everything that you need to do. With three lines of code you can create OData service on any table. You can find more more details in [OData documentation](doc/odata.md) page.
116116

117+
You can see how to create OData services in the [SQL Server Wide World Importers sample app](https://github.com/Microsoft/sql-server-samples/blob/master/samples/databases/wide-world-importers/wwi-app/Controllers/ODataController.cs).
118+
117119
## Implement REST service that process JQuery DataTables Ajax request
118120

119121
[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:
@@ -144,7 +146,7 @@ In order to implement REST service that handles AJAX requests that JQuery DataTa
144146
```
145147
IQueryPipe sqlQuery = null;
146148
147-
TableSpec tableSpec = new TableSpec("dbo.People", "name,surname,address,town");
149+
TableSpec tableSpec = new TableSpec(schema: "dbo", table: "People", columns: "name,surname,address,town");
148150
149151
public PeopleController(IQueryPipe sqlQueryService)
150152
{
@@ -156,30 +158,17 @@ Now you need to create async method that will serve JQuery DataTables AJAX reque
156158
- UriParser that will parse Http request parameters that JQuery DataTables component sends
157159
- QueryBuilder that will create T-SQL query that will be executed.
158160

159-
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:
161+
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`:
160162

161163
```
162-
// GET api/People
163-
[HttpGet]
164-
public async Task Get(int draw, int start, int length)
165-
{
166-
var querySpec = JQueryDataTables.UriParser.Parse(tableSpec, this.Request);
167-
var sql = QueryBuilder.Build(querySpec, tableSpec).AsJson();
168-
169-
var header = System.Text.Encoding.UTF8.GetBytes(
170-
@"{
171-
""draw"":" + draw + @",
172-
""recordsTotal"": " + (start + length + 1) + @",
173-
""recordsFiltered"": " + (start + length + 1) + @",
174-
""data"":");
175-
await Response.Body.WriteAsync(header, 0, header.Length);
176-
177-
await sqlQuery.Stream(sql, Response.Body, "[]");
178-
179-
await Response.Body.WriteAsync(System.Text.Encoding.UTF8.GetBytes("}"), 0, 1);
164+
private static readonly TableSpec purchaseorders = new TableSpec("WebApi","PurchaseOrders", "OrderDate,SupplierReference,ExpectedDeliveryDate,ContactName,ContactPhone,IsOrderFinalized,PurchaseOrderID");
165+
public async Task PurchaseOrders()
166+
{
167+
await this.Table(purchaseorders).Process(this.sqlQuery);
180168
}
181169
```
182170
[JQuery DataTables](https://datatables.net/) component requires AJAX response in some pre-defined format, so you would need to wrap results from database with header that contains number of total and number of filtered records.
183171
Note that JQuery DataTables plugin uses **recordsTotal** and **recordsFiltered** to build pagination. Since you would need two additional queries . Reccomendation is to use alternative (paging plugins)[https://datatables.net/plug-ins/pagination/]
184172
that don't require these options.
185173

174+
You can see how to create services that are used by JQuery DataTables in the [SQL Server Wide World Importers sample app](https://github.com/Microsoft/sql-server-samples/blob/master/samples/databases/wide-world-importers/wwi-app/Controllers/TableController.cs).

0 commit comments

Comments
 (0)