Skip to content

Commit 0d1daa5

Browse files
Merge pull request #210 from JocaPC/master
Refactored JSON projects
2 parents 09a5451 + 1e8f76d commit 0d1daa5

File tree

10 files changed

+152
-51
lines changed

10 files changed

+152
-51
lines changed

samples/demos/ivs-people-register/Controllers/ODataController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public string Metadata()
5252
public async Task People()
5353
{
5454
await this
55-
.ODataHandler(tableSpec, this.pipe, ODataHandler.Metadata.MINIMAL)
55+
.OData(tableSpec, this.pipe, ODataHandler.Metadata.MINIMAL)
5656
.OnError(ex => Response.Body.Write(Encoding.UTF8.GetBytes(ex.Message), 0, (ex.Message).Length))
5757
.Get();
5858
}

samples/demos/ivs-people-register/Controllers/PeopleController.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Belgrade.SqlClient;
22
using Microsoft.AspNetCore.Mvc;
33
using SqlServerRestApi;
4+
using System.Text;
45
using System.Threading.Tasks;
56

67
// For more information on enabling Web API for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
@@ -38,7 +39,7 @@ public async Task OData()
3839
{
3940
Response.ContentType = "application/json";
4041
await this
41-
.ODataHandler(tableSpec, pipe)
42+
.OData(tableSpec, pipe)
4243
.Process();
4344
}
4445

@@ -52,7 +53,8 @@ await this
5253
public async Task Get()
5354
{
5455
await this
55-
.JQueryDataTablesHandler(tableSpec, pipe)
56+
.JQueryDataTables(tableSpec, pipe)
57+
.OnError(ex => Response.Body.Write(Encoding.UTF8.GetBytes(ex.Message), 0, ex.Message.Length))
5658
.Process();
5759
}
5860
}

samples/demos/ivs-people-register/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"Microsoft.Extensions.Logging.Console": "1.0.0",
1717
"Microsoft.Extensions.Logging.Debug": "1.0.0",
1818
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
19-
"Sql-Server-Rest-Api": "0.3.1"
19+
"Sql-Server-Rest-Api": "0.3.3"
2020
},
2121

2222
"tools": {
Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,58 @@
1-
$.fn.dataTableExt.oPagination.incremental = {
2-
/*
3-
* Function: oPagination.incremental.fnInit
4-
* Purpose: Initalise dom elements required for pagination with a list of the pages
5-
* Returns: -
6-
* Inputs: object:oSettings - dataTables settings object
7-
* node:nPaging - the DIV which contains this pagination control
8-
* function:fnCallbackDraw - draw function which must be called on update
9-
*/
1+
/**
2+
* This pagination style shows Previous/Next buttons, and page numbers only
3+
* for "known" pages that are visited at least once time using [Next>] button.
4+
* Initially only Prev/Next buttons are shown (Prev is initially disabled).
5+
*
6+
* [<Previous] [Next>]
7+
*
8+
* When user navigates through the pages using [Next>] button, navigation shows
9+
* the numbers for the previous pages. As an example, when user reaches page 2,
10+
* page numbers 1 and 2 are shown:
11+
*
12+
* [<Previous] 1 2 [Next>]
13+
*
14+
* When user reaches page 4, page numbers 1, 2, 3, and 4 are shown:
15+
*
16+
* [<Previous] 1 2 3 4 [Next>]
17+
*
18+
* When user navigates back, pagination will remember the last page number
19+
* he reached and the numbesr up to the last known page are shown. As an example,
20+
* when user returns to the page 2, page numbers 1, 2, 3, and 4 are still shown:
21+
*
22+
* [<Previous] 1 2 3 4 [Next>]
23+
*
24+
* This pagination style is designed for users who will not directly jump to
25+
* the random page that they have not opened before. Assumption is that users
26+
* will discover new pages using [Next>] button. This pagination enables users
27+
* to easily go back and forth to any page that they discovered.
28+
*
29+
* Key benefit: This pagination supports usual pagination pattern and does not
30+
* require server to return total count of items just to calculate last page and
31+
* all numbers. This migh be huge performance benefit because server does not
32+
* need to execute two queries in server-side processing mode:
33+
* - One to get the records that will be shown on the current page,
34+
* - Second to get the total count just to calculate full pagination.
35+
*
36+
* Without second query, page load time might be 2x faster, especially in cases
37+
* when server can quickly get top 100 records, but it would need to scan entire
38+
* database table just to calculate the total count and position of the last
39+
* page. This pagination style is reasonable trade-off between simple and fullnumbers
40+
* pagination.
41+
*
42+
* @name Simple Incremental navigation (Bootstrap)
43+
* @summary Shows forward/back buttons and all known page numbers.
44+
* @author [Jovan Popovic](http://github.com/JocaPC)
45+
*
46+
* @example
47+
* $(document).ready(function() {
48+
* $('#example').dataTable( {
49+
* "pagingType": "simple_incremental_bootstrap"
50+
* } );
51+
* } );
52+
*/
53+
54+
$.fn.dataTableExt.oPagination.simple_incremental_bootstrap = {
55+
1056
"fnInit": function (oSettings, nPaging, fnCallbackDraw) {
1157
$(nPaging).prepend($("<ul class=\"pagination\"></ul>"));
1258
var ul = $("ul", $(nPaging));
@@ -21,7 +67,6 @@
2167
nFirst.className = "paginate_button first active";
2268
nPrevious.className = "paginate_button previous";
2369
nNext.className = "paginate_button next";
24-
2570

2671
ul.append(nPrevious);
2772
ul.append(nFirst);
@@ -64,9 +109,8 @@
64109
},
65110

66111
/*
67-
* Function: oPagination.incremental.fnUpdate
112+
* Function: oPagination.simple_incremental_bootstrap.fnUpdate
68113
* Purpose: Update the list of page buttons shows
69-
* Returns: -
70114
* Inputs: object:oSettings - dataTables settings object
71115
* function:fnCallbackDraw - draw function which must be called on update
72116
*/
@@ -107,4 +151,4 @@
107151
}
108152
}
109153
}
110-
};
154+
};

samples/demos/ivs-people-register/wwwroot/server.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<script src="media/js/lib/Bootstrap.js"></script>
1818
<script src="media/js/lib/jquery.dataTables.js"></script>
1919
<script src="media/js/lib/jquery.dataTables.Bootstrap.js"></script>
20-
<script src="media/js/lib/jquery.dataTables.incremental_pagination.js"></script>
20+
<script src="media/js/lib/simple_incremental_bootstrap.js"></script>
2121

2222

2323
<script type="text/javascript" class="init">
@@ -33,7 +33,7 @@
3333
var table = $('#example').DataTable({
3434
"serverSide": true,
3535
"processing": true,
36-
"pagingType": "incremental",
36+
"pagingType": "simple_incremental_bootstrap",
3737
"ajax": "/api/People",
3838
"columns": [
3939
{ "data": "name", "width": "10%" },
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
select *
2+
from Comments
3+
for json path
4+
5+
select *
6+
from Comments
7+
where id = 1
8+
for json path, without_array_wrapper
9+
10+
declare @p nvarchar(4000) = N'[{"author":"John","text":"I like it too!"},{"author":"Jane","text":"Thanks!"},{"author":"Jane","text":"Buy :)"}]'
11+
12+
select *
13+
from openjson(@p)
14+
with ( author nvarchar(20), text nvarchar(200))
15+
16+
go
17+
18+
declare @p nvarchar(4000) = N'[{"author":"John","text":"I like it too!"},{"author":"Jane","text":"Thanks!"},{"author":"Jane","text":"Buy :)"}]'
19+
20+
insert into Comments(author, text)
21+
select *
22+
from openjson(@p)
23+
with ( author nvarchar(20), text nvarchar(200))
24+
25+
select *
26+
from Comments
27+
28+
select author, count(*) comments
29+
from Comments
30+
group by author
31+
for json path
32+
33+
delete Comments where id > 2
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
DROP LOGIN WebLogin
2+
CREATE LOGIN WebLogin WITH PASSWORD = 'SQLPass1234!'
3+
GO
4+
5+
DROP USER IF EXISTS WebUser
6+
CREATE USER WebUser FROM LOGIN WebLogin
7+
GO
8+
9+
DROP ROLE IF EXISTS WebUserRole
10+
CREATE ROLE WebUserRole
11+
GO
12+
13+
GRANT SELECT, INSERT, UPDATE, DELETE, EXECUTE TO WebUserRole
14+
EXEC sp_addrolemember N'WebUserRole', N'WebUser'
15+
GO

samples/features/json/todo-app/dotnet-rest-api/Controllers/TodoController.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public async Task Post()
4343
@"insert into Todo
4444
select *
4545
from OPENJSON(@todo)
46-
WITH( Title nvarchar(30), Description nvarchar(4000), Completed bit, TargetDate datetime2)");
46+
WITH( title nvarchar(30), description nvarchar(4000), completed bit, dueDate datetime2)");
4747
cmd.Parameters.AddWithValue("todo", todo);
4848
await SqlCommand.ExecuteNonQuery(cmd);
4949
}
@@ -55,11 +55,11 @@ public async Task Patch(int id)
5555
string todo = new StreamReader(Request.Body).ReadToEnd();
5656
var cmd = new SqlCommand(
5757
@"update Todo
58-
set Title = ISNULL(json.Title, Title), Description = ISNULL(json.Description, Description),
59-
Completed = ISNULL(json.Completed, Completed), TargetDate = ISNULL(json.TargetDate, TargetDate)
58+
set title = ISNULL(json.title, title), description = ISNULL(json.description, description),
59+
completed = ISNULL(json.completed, completed), dueDate = ISNULL(json.dueDate, dueDate)
6060
from OPENJSON(@todo)
61-
WITH( Title nvarchar(30), Description nvarchar(4000),
62-
Completed bit, TargetDate datetime2) AS json
61+
WITH( title nvarchar(30), description nvarchar(4000),
62+
completed bit, dueDate datetime2) AS json
6363
where Id = @id");
6464
cmd.Parameters.AddWithValue("id", id);
6565
cmd.Parameters.AddWithValue("todo", todo);
@@ -73,11 +73,11 @@ public async Task Put(int id)
7373
string todo = new StreamReader(Request.Body).ReadToEnd();
7474
var cmd = new SqlCommand(
7575
@"update Todo
76-
set Title = json.Title, Description = json.Description,
77-
Completed = json.completed, TargetDate = json.TargetDate
76+
set title = json.title, description = json.description,
77+
completed = json.completed, dueDate = json.dueDate
7878
from OPENJSON( @todo )
79-
WITH( Title nvarchar(30), Description nvarchar(4000),
80-
Completed bit, TargetDate datetime2) AS json
79+
WITH( title nvarchar(30), description nvarchar(4000),
80+
completed bit, dueDate datetime2) AS json
8181
where Id = @id");
8282
cmd.Parameters.AddWithValue("id", id);
8383
cmd.Parameters.AddWithValue("todo", todo);

samples/features/json/todo-app/dotnet-rest-api/setup/setup.sql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
GO
44

55
CREATE TABLE Todo (
6-
Id int IDENTITY PRIMARY KEY,
7-
Title nvarchar(30) NOT NULL,
8-
Description nvarchar(4000),
9-
Completed bit,
10-
TargetDate datetime2
6+
id int IDENTITY PRIMARY KEY,
7+
title nvarchar(30) NOT NULL,
8+
description nvarchar(4000),
9+
completed bit,
10+
dueDate datetime2 default (dateadd(day, 3, getdate()))
1111
)
1212

1313
GO
1414

15-
INSERT INTO Todo (Title, Description, Completed, TargetDate)
15+
INSERT INTO Todo (title, description, completed, dueDate)
1616
VALUES
1717
('Install SQL Server 2016','Install RTM version of SQL Server 2016', 0, '2016-06-01'),
1818
('Get new samples','Go to github and download new samples', 0, '2016-06-02'),
Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
DROP TABLE IF EXISTS Todo
1+
/*
2+
CREATE DATABASE TodoDb;
3+
USE TodoDb;
4+
*/
5+
DROP TABLE IF EXISTS Todo
26
DROP PROCEDURE IF EXISTS createTodo
37
DROP PROCEDURE IF EXISTS updateTodo
48
GO
59

610
CREATE TABLE Todo (
7-
Id int IDENTITY PRIMARY KEY,
8-
Title nvarchar(30) NOT NULL,
9-
Description nvarchar(4000),
10-
Completed bit,
11-
TargetDate datetime2
11+
id int IDENTITY PRIMARY KEY,
12+
title nvarchar(30) NOT NULL,
13+
description nvarchar(4000),
14+
completed bit,
15+
dueDate datetime2 default (dateadd(day, 3, getdate()))
1216
)
1317
GO
1418

15-
INSERT INTO Todo (Title, Description, Completed, TargetDate)
19+
INSERT INTO Todo (title, description, completed, dueDate)
1620
VALUES
17-
('Install SQL Server 2016','Install RTM version of SQL Server 2016', 0, '2016-06-01'),
18-
('Get new samples','Go to github and download new samples', 0, '2016-06-02'),
19-
('Try new samples','Install new Management Studio to try samples', 0, '2016-06-02')
21+
('Install SQL Server 2016','Install RTM version of SQL Server 2016', 0, '2017-03-08'),
22+
('Get new samples','Go to github and download new samples', 0, '2016-03-09'),
23+
('Try new samples','Install new Management Studio to try samples', 0, '2016-03-12')
2024

2125
GO
2226

@@ -25,18 +29,21 @@ as begin
2529
insert into Todo
2630
select *
2731
from OPENJSON(@todo)
28-
WITH ( Title nvarchar(30), Description nvarchar(4000),
29-
Completed bit, TargetDate datetime2)
32+
WITH ( title nvarchar(30), description nvarchar(4000),
33+
completed bit, dueDate datetime2)
3034
end
3135
GO
3236

3337
create procedure updateTodo(@id int, @todo nvarchar(max))
3438
as begin
3539
update Todo
36-
set Title = json.Title, Description = json.Description,
37-
Completed = json.Completed, TargetDate = json.TargetDate
40+
set title = json.title, description = json.description,
41+
completed = json.completed, dueDate = json.dueDate
3842
from OPENJSON( @todo )
39-
WITH( Title nvarchar(30), Description nvarchar(4000),
40-
Completed bit, TargetDate datetime2) AS json
41-
where Id = @id
43+
WITH( title nvarchar(30), description nvarchar(4000),
44+
completed bit, dueDate datetime2) AS json
45+
where id = @id
4246
end
47+
go
48+
49+
select * from todo for json path

0 commit comments

Comments
 (0)