mirror of
https://github.com/Microsoft/sql-server-samples.git
synced 2025-12-08 14:58:54 +00:00
Refactored IVS, React and Todo Apps
This commit is contained in:
@@ -52,7 +52,7 @@ namespace Register.Controllers
|
||||
public async Task People()
|
||||
{
|
||||
await this
|
||||
.ODataHandler(tableSpec, this.pipe, ODataHandler.Metadata.MINIMAL)
|
||||
.OData(tableSpec, this.pipe, ODataHandler.Metadata.MINIMAL)
|
||||
.OnError(ex => Response.Body.Write(Encoding.UTF8.GetBytes(ex.Message), 0, (ex.Message).Length))
|
||||
.Get();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using Belgrade.SqlClient;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SqlServerRestApi;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
// For more information on enabling Web API for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
|
||||
@@ -38,7 +39,7 @@ namespace Register.Controllers
|
||||
{
|
||||
Response.ContentType = "application/json";
|
||||
await this
|
||||
.ODataHandler(tableSpec, pipe)
|
||||
.OData(tableSpec, pipe)
|
||||
.Process();
|
||||
}
|
||||
|
||||
@@ -52,7 +53,8 @@ namespace Register.Controllers
|
||||
public async Task Get()
|
||||
{
|
||||
await this
|
||||
.JQueryDataTablesHandler(tableSpec, pipe)
|
||||
.JQueryDataTables(tableSpec, pipe)
|
||||
.OnError(ex => Response.Body.Write(Encoding.UTF8.GetBytes(ex.Message), 0, ex.Message.Length))
|
||||
.Process();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
"Microsoft.Extensions.Logging.Console": "1.0.0",
|
||||
"Microsoft.Extensions.Logging.Debug": "1.0.0",
|
||||
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
|
||||
"Sql-Server-Rest-Api": "0.3.1"
|
||||
"Sql-Server-Rest-Api": "0.3.3"
|
||||
},
|
||||
|
||||
"tools": {
|
||||
|
||||
+57
-13
@@ -1,12 +1,58 @@
|
||||
$.fn.dataTableExt.oPagination.incremental = {
|
||||
/*
|
||||
* Function: oPagination.incremental.fnInit
|
||||
* Purpose: Initalise dom elements required for pagination with a list of the pages
|
||||
* Returns: -
|
||||
* Inputs: object:oSettings - dataTables settings object
|
||||
* node:nPaging - the DIV which contains this pagination control
|
||||
* function:fnCallbackDraw - draw function which must be called on update
|
||||
*/
|
||||
/**
|
||||
* This pagination style shows Previous/Next buttons, and page numbers only
|
||||
* for "known" pages that are visited at least once time using [Next>] button.
|
||||
* Initially only Prev/Next buttons are shown (Prev is initially disabled).
|
||||
*
|
||||
* [<Previous] [Next>]
|
||||
*
|
||||
* When user navigates through the pages using [Next>] button, navigation shows
|
||||
* the numbers for the previous pages. As an example, when user reaches page 2,
|
||||
* page numbers 1 and 2 are shown:
|
||||
*
|
||||
* [<Previous] 1 2 [Next>]
|
||||
*
|
||||
* When user reaches page 4, page numbers 1, 2, 3, and 4 are shown:
|
||||
*
|
||||
* [<Previous] 1 2 3 4 [Next>]
|
||||
*
|
||||
* When user navigates back, pagination will remember the last page number
|
||||
* he reached and the numbesr up to the last known page are shown. As an example,
|
||||
* when user returns to the page 2, page numbers 1, 2, 3, and 4 are still shown:
|
||||
*
|
||||
* [<Previous] 1 2 3 4 [Next>]
|
||||
*
|
||||
* This pagination style is designed for users who will not directly jump to
|
||||
* the random page that they have not opened before. Assumption is that users
|
||||
* will discover new pages using [Next>] button. This pagination enables users
|
||||
* to easily go back and forth to any page that they discovered.
|
||||
*
|
||||
* Key benefit: This pagination supports usual pagination pattern and does not
|
||||
* require server to return total count of items just to calculate last page and
|
||||
* all numbers. This migh be huge performance benefit because server does not
|
||||
* need to execute two queries in server-side processing mode:
|
||||
* - One to get the records that will be shown on the current page,
|
||||
* - Second to get the total count just to calculate full pagination.
|
||||
*
|
||||
* Without second query, page load time might be 2x faster, especially in cases
|
||||
* when server can quickly get top 100 records, but it would need to scan entire
|
||||
* database table just to calculate the total count and position of the last
|
||||
* page. This pagination style is reasonable trade-off between simple and fullnumbers
|
||||
* pagination.
|
||||
*
|
||||
* @name Simple Incremental navigation (Bootstrap)
|
||||
* @summary Shows forward/back buttons and all known page numbers.
|
||||
* @author [Jovan Popovic](http://github.com/JocaPC)
|
||||
*
|
||||
* @example
|
||||
* $(document).ready(function() {
|
||||
* $('#example').dataTable( {
|
||||
* "pagingType": "simple_incremental_bootstrap"
|
||||
* } );
|
||||
* } );
|
||||
*/
|
||||
|
||||
$.fn.dataTableExt.oPagination.simple_incremental_bootstrap = {
|
||||
|
||||
"fnInit": function (oSettings, nPaging, fnCallbackDraw) {
|
||||
$(nPaging).prepend($("<ul class=\"pagination\"></ul>"));
|
||||
var ul = $("ul", $(nPaging));
|
||||
@@ -21,7 +67,6 @@
|
||||
nFirst.className = "paginate_button first active";
|
||||
nPrevious.className = "paginate_button previous";
|
||||
nNext.className = "paginate_button next";
|
||||
|
||||
|
||||
ul.append(nPrevious);
|
||||
ul.append(nFirst);
|
||||
@@ -64,9 +109,8 @@
|
||||
},
|
||||
|
||||
/*
|
||||
* Function: oPagination.incremental.fnUpdate
|
||||
* Function: oPagination.simple_incremental_bootstrap.fnUpdate
|
||||
* Purpose: Update the list of page buttons shows
|
||||
* Returns: -
|
||||
* Inputs: object:oSettings - dataTables settings object
|
||||
* function:fnCallbackDraw - draw function which must be called on update
|
||||
*/
|
||||
@@ -107,4 +151,4 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
@@ -17,7 +17,7 @@
|
||||
<script src="media/js/lib/Bootstrap.js"></script>
|
||||
<script src="media/js/lib/jquery.dataTables.js"></script>
|
||||
<script src="media/js/lib/jquery.dataTables.Bootstrap.js"></script>
|
||||
<script src="media/js/lib/jquery.dataTables.incremental_pagination.js"></script>
|
||||
<script src="media/js/lib/simple_incremental_bootstrap.js"></script>
|
||||
|
||||
|
||||
<script type="text/javascript" class="init">
|
||||
@@ -33,7 +33,7 @@ $(document).ready(function() {
|
||||
var table = $('#example').DataTable({
|
||||
"serverSide": true,
|
||||
"processing": true,
|
||||
"pagingType": "incremental",
|
||||
"pagingType": "simple_incremental_bootstrap",
|
||||
"ajax": "/api/People",
|
||||
"columns": [
|
||||
{ "data": "name", "width": "10%" },
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
select *
|
||||
from Comments
|
||||
for json path
|
||||
|
||||
select *
|
||||
from Comments
|
||||
where id = 1
|
||||
for json path, without_array_wrapper
|
||||
|
||||
declare @p nvarchar(4000) = N'[{"author":"John","text":"I like it too!"},{"author":"Jane","text":"Thanks!"},{"author":"Jane","text":"Buy :)"}]'
|
||||
|
||||
select *
|
||||
from openjson(@p)
|
||||
with ( author nvarchar(20), text nvarchar(200))
|
||||
|
||||
go
|
||||
|
||||
declare @p nvarchar(4000) = N'[{"author":"John","text":"I like it too!"},{"author":"Jane","text":"Thanks!"},{"author":"Jane","text":"Buy :)"}]'
|
||||
|
||||
insert into Comments(author, text)
|
||||
select *
|
||||
from openjson(@p)
|
||||
with ( author nvarchar(20), text nvarchar(200))
|
||||
|
||||
select *
|
||||
from Comments
|
||||
|
||||
select author, count(*) comments
|
||||
from Comments
|
||||
group by author
|
||||
for json path
|
||||
|
||||
delete Comments where id > 2
|
||||
@@ -0,0 +1,15 @@
|
||||
DROP LOGIN WebLogin
|
||||
CREATE LOGIN WebLogin WITH PASSWORD = 'SQLPass1234!'
|
||||
GO
|
||||
|
||||
DROP USER IF EXISTS WebUser
|
||||
CREATE USER WebUser FROM LOGIN WebLogin
|
||||
GO
|
||||
|
||||
DROP ROLE IF EXISTS WebUserRole
|
||||
CREATE ROLE WebUserRole
|
||||
GO
|
||||
|
||||
GRANT SELECT, INSERT, UPDATE, DELETE, EXECUTE TO WebUserRole
|
||||
EXEC sp_addrolemember N'WebUserRole', N'WebUser'
|
||||
GO
|
||||
@@ -43,7 +43,7 @@ namespace TodoApp.Controllers
|
||||
@"insert into Todo
|
||||
select *
|
||||
from OPENJSON(@todo)
|
||||
WITH( Title nvarchar(30), Description nvarchar(4000), Completed bit, TargetDate datetime2)");
|
||||
WITH( title nvarchar(30), description nvarchar(4000), completed bit, dueDate datetime2)");
|
||||
cmd.Parameters.AddWithValue("todo", todo);
|
||||
await SqlCommand.ExecuteNonQuery(cmd);
|
||||
}
|
||||
@@ -55,11 +55,11 @@ namespace TodoApp.Controllers
|
||||
string todo = new StreamReader(Request.Body).ReadToEnd();
|
||||
var cmd = new SqlCommand(
|
||||
@"update Todo
|
||||
set Title = ISNULL(json.Title, Title), Description = ISNULL(json.Description, Description),
|
||||
Completed = ISNULL(json.Completed, Completed), TargetDate = ISNULL(json.TargetDate, TargetDate)
|
||||
set title = ISNULL(json.title, title), description = ISNULL(json.description, description),
|
||||
completed = ISNULL(json.completed, completed), dueDate = ISNULL(json.dueDate, dueDate)
|
||||
from OPENJSON(@todo)
|
||||
WITH( Title nvarchar(30), Description nvarchar(4000),
|
||||
Completed bit, TargetDate datetime2) AS json
|
||||
WITH( title nvarchar(30), description nvarchar(4000),
|
||||
completed bit, dueDate datetime2) AS json
|
||||
where Id = @id");
|
||||
cmd.Parameters.AddWithValue("id", id);
|
||||
cmd.Parameters.AddWithValue("todo", todo);
|
||||
@@ -73,11 +73,11 @@ namespace TodoApp.Controllers
|
||||
string todo = new StreamReader(Request.Body).ReadToEnd();
|
||||
var cmd = new SqlCommand(
|
||||
@"update Todo
|
||||
set Title = json.Title, Description = json.Description,
|
||||
Completed = json.completed, TargetDate = json.TargetDate
|
||||
set title = json.title, description = json.description,
|
||||
completed = json.completed, dueDate = json.dueDate
|
||||
from OPENJSON( @todo )
|
||||
WITH( Title nvarchar(30), Description nvarchar(4000),
|
||||
Completed bit, TargetDate datetime2) AS json
|
||||
WITH( title nvarchar(30), description nvarchar(4000),
|
||||
completed bit, dueDate datetime2) AS json
|
||||
where Id = @id");
|
||||
cmd.Parameters.AddWithValue("id", id);
|
||||
cmd.Parameters.AddWithValue("todo", todo);
|
||||
|
||||
@@ -3,16 +3,16 @@
|
||||
GO
|
||||
|
||||
CREATE TABLE Todo (
|
||||
Id int IDENTITY PRIMARY KEY,
|
||||
Title nvarchar(30) NOT NULL,
|
||||
Description nvarchar(4000),
|
||||
Completed bit,
|
||||
TargetDate datetime2
|
||||
id int IDENTITY PRIMARY KEY,
|
||||
title nvarchar(30) NOT NULL,
|
||||
description nvarchar(4000),
|
||||
completed bit,
|
||||
dueDate datetime2 default (dateadd(day, 3, getdate()))
|
||||
)
|
||||
|
||||
GO
|
||||
|
||||
INSERT INTO Todo (Title, Description, Completed, TargetDate)
|
||||
INSERT INTO Todo (title, description, completed, dueDate)
|
||||
VALUES
|
||||
('Install SQL Server 2016','Install RTM version of SQL Server 2016', 0, '2016-06-01'),
|
||||
('Get new samples','Go to github and download new samples', 0, '2016-06-02'),
|
||||
|
||||
@@ -1,22 +1,26 @@
|
||||
DROP TABLE IF EXISTS Todo
|
||||
/*
|
||||
CREATE DATABASE TodoDb;
|
||||
USE TodoDb;
|
||||
*/
|
||||
DROP TABLE IF EXISTS Todo
|
||||
DROP PROCEDURE IF EXISTS createTodo
|
||||
DROP PROCEDURE IF EXISTS updateTodo
|
||||
GO
|
||||
|
||||
CREATE TABLE Todo (
|
||||
Id int IDENTITY PRIMARY KEY,
|
||||
Title nvarchar(30) NOT NULL,
|
||||
Description nvarchar(4000),
|
||||
Completed bit,
|
||||
TargetDate datetime2
|
||||
id int IDENTITY PRIMARY KEY,
|
||||
title nvarchar(30) NOT NULL,
|
||||
description nvarchar(4000),
|
||||
completed bit,
|
||||
dueDate datetime2 default (dateadd(day, 3, getdate()))
|
||||
)
|
||||
GO
|
||||
|
||||
INSERT INTO Todo (Title, Description, Completed, TargetDate)
|
||||
INSERT INTO Todo (title, description, completed, dueDate)
|
||||
VALUES
|
||||
('Install SQL Server 2016','Install RTM version of SQL Server 2016', 0, '2016-06-01'),
|
||||
('Get new samples','Go to github and download new samples', 0, '2016-06-02'),
|
||||
('Try new samples','Install new Management Studio to try samples', 0, '2016-06-02')
|
||||
('Install SQL Server 2016','Install RTM version of SQL Server 2016', 0, '2017-03-08'),
|
||||
('Get new samples','Go to github and download new samples', 0, '2016-03-09'),
|
||||
('Try new samples','Install new Management Studio to try samples', 0, '2016-03-12')
|
||||
|
||||
GO
|
||||
|
||||
@@ -25,18 +29,21 @@ as begin
|
||||
insert into Todo
|
||||
select *
|
||||
from OPENJSON(@todo)
|
||||
WITH ( Title nvarchar(30), Description nvarchar(4000),
|
||||
Completed bit, TargetDate datetime2)
|
||||
WITH ( title nvarchar(30), description nvarchar(4000),
|
||||
completed bit, dueDate datetime2)
|
||||
end
|
||||
GO
|
||||
|
||||
create procedure updateTodo(@id int, @todo nvarchar(max))
|
||||
as begin
|
||||
update Todo
|
||||
set Title = json.Title, Description = json.Description,
|
||||
Completed = json.Completed, TargetDate = json.TargetDate
|
||||
set title = json.title, description = json.description,
|
||||
completed = json.completed, dueDate = json.dueDate
|
||||
from OPENJSON( @todo )
|
||||
WITH( Title nvarchar(30), Description nvarchar(4000),
|
||||
Completed bit, TargetDate datetime2) AS json
|
||||
where Id = @id
|
||||
WITH( title nvarchar(30), description nvarchar(4000),
|
||||
completed bit, dueDate datetime2) AS json
|
||||
where id = @id
|
||||
end
|
||||
go
|
||||
|
||||
select * from todo for json path
|
||||
Reference in New Issue
Block a user