From 649efa4eadd94da4108eed7cc10a5caac67fb2ef Mon Sep 17 00:00:00 2001 From: Jovan Popovic Date: Sun, 30 Sep 2018 17:48:35 +0200 Subject: [PATCH] Upgraded BelgradeProductCatalog to .csproj --- .../Controllers/CompanyController.cs | 2 +- .../Controllers/ProductController.cs | 69 ++++++++++--------- .../ProductCatalog.xproj | 19 ----- .../belgrade-product-catalog-demo/Startup.cs | 11 +-- .../appsettings.json | 2 +- .../belgrade-product-catalog-demo.csproj | 57 +++++++++++++++ .../project.json | 63 ----------------- .../sql-scripts/1 setup.sql | 17 ++--- 8 files changed, 108 insertions(+), 132 deletions(-) delete mode 100644 samples/demos/belgrade-product-catalog-demo/ProductCatalog.xproj create mode 100644 samples/demos/belgrade-product-catalog-demo/belgrade-product-catalog-demo.csproj delete mode 100644 samples/demos/belgrade-product-catalog-demo/project.json diff --git a/samples/demos/belgrade-product-catalog-demo/Controllers/CompanyController.cs b/samples/demos/belgrade-product-catalog-demo/Controllers/CompanyController.cs index 84003f3f..0cbf0b85 100644 --- a/samples/demos/belgrade-product-catalog-demo/Controllers/CompanyController.cs +++ b/samples/demos/belgrade-product-catalog-demo/Controllers/CompanyController.cs @@ -22,7 +22,7 @@ namespace ProductCatalog.Controllers [HttpGet] public async Task Get() { - await sqlQuery.Stream("select CompanyID as [value], Name as [text] from Company FOR JSON PATH", Response.Body); + await sqlQuery.Sql("select CompanyID as [value], Name as [text] from Company FOR JSON PATH").Stream(Response.Body); } [HttpGet("login")] diff --git a/samples/demos/belgrade-product-catalog-demo/Controllers/ProductController.cs b/samples/demos/belgrade-product-catalog-demo/Controllers/ProductController.cs index 79e33881..929233ad 100644 --- a/samples/demos/belgrade-product-catalog-demo/Controllers/ProductController.cs +++ b/samples/demos/belgrade-product-catalog-demo/Controllers/ProductController.cs @@ -37,11 +37,11 @@ namespace ProductCatalog.Controllers this.Response.StatusCode = 500; throw ex; }) - .Stream(@" + .Sql(@" select ProductID, Name, Color, Price, Quantity, JSON_VALUE(Data, '$.MadeIn') as MadeIn, JSON_QUERY(Tags) as Tags from Product - FOR JSON PATH, ROOT('data')", Response.Body, EMPTY_PRODUCTS_ARRAY); + FOR JSON PATH, ROOT('data')").Stream(Response.Body); } // GET api/Product/compressed @@ -50,29 +50,28 @@ namespace ProductCatalog.Controllers { Response.Headers.Add("Content-Type", "application/json;charset=utf-16"); Response.Headers.Add("Content-Encoding", "gzip"); - await sqlQuery.Stream(@" + await sqlQuery.Sql(@" select COMPRESS( (select ProductID, Name, Color, Price, Quantity, JSON_VALUE(Data, '$.MadeIn') as MadeIn, JSON_QUERY(Tags) as Tags from Product FOR JSON PATH, ROOT('data') ) -)", Response.Body, EMPTY_PRODUCTS_ARRAY_GZIPPED); +)").Stream(Response.Body, EMPTY_PRODUCTS_ARRAY_GZIPPED); } // GET api/Product/5 [HttpGet("{id}")] public async Task Get(int id) { - var cmd = new SqlCommand( + await sqlQuery.Sql( @"select ProductID, Product.Name, Color, Price, Quantity, Company.Name as Company, Company.Address, Company.Email, Company.Phone from Product join Company on Product.CompanyID = Company.CompanyID where ProductID = @id -FOR JSON PATH, WITHOUT_ARRAY_WRAPPER"); - - cmd.Parameters.AddWithValue("id", id); - await sqlQuery.Stream(cmd, Response.Body, "{}"); +FOR JSON PATH, WITHOUT_ARRAY_WRAPPER") + .Param("id", id) + .Stream(Response.Body, "{}"); } // POST api/Product @@ -80,9 +79,10 @@ FOR JSON PATH, WITHOUT_ARRAY_WRAPPER"); public async Task Post() { string product = new StreamReader(Request.Body).ReadToEnd(); - var cmd = new SqlCommand("EXEC InsertProductFromJson @ProductJson"); - cmd.Parameters.AddWithValue("ProductJson", product); - await sqlCmd.ExecuteNonQuery(cmd); + await sqlCmd + .Sql("EXEC InsertProductFromJson @ProductJson") + .Param("ProductJson", product) + .Exec(); } // PUT api/Product/5 @@ -90,19 +90,20 @@ FOR JSON PATH, WITHOUT_ARRAY_WRAPPER"); public async Task Put(int id) { string product = new StreamReader(Request.Body).ReadToEnd(); - var cmd = new SqlCommand("EXEC UpdateProductFromJson @ProductID, @ProductJson"); - cmd.Parameters.AddWithValue("ProductID", id); - cmd.Parameters.AddWithValue("ProductJson", product); - await sqlCmd.ExecuteNonQuery(cmd); + await sqlCmd + .Sql("EXEC UpdateProductFromJson @ProductID, @ProductJson") + .Param("ProductID", id) + .Param("ProductJson", product) + .Exec(); } // DELETE api/Product/5 [HttpDelete("{id}")] public async Task Delete(int id) { - var cmd = new SqlCommand(@"delete Product where ProductID = @id"); - cmd.Parameters.AddWithValue("id", id); - await sqlCmd.ExecuteNonQuery(cmd); + await sqlCmd.Sql(@"delete Product where ProductID = @id") + .Param("id", id) + .Exec(); } [HttpGet("temporal")] @@ -110,23 +111,23 @@ FOR JSON PATH, WITHOUT_ARRAY_WRAPPER"); public async Task Get(DateTime? date) { if (date == null) - await this.sqlQuery.Stream("EXEC GetProducts", this.Response.Body, EMPTY_PRODUCTS_ARRAY); + await this.sqlQuery + .Sql("EXEC GetProducts") + .Stream(this.Response.Body, EMPTY_PRODUCTS_ARRAY); else - { - var cmd = new SqlCommand("EXEC GetProductsAsOf @date"); - cmd.Parameters.AddWithValue("@date", date); - await this.sqlQuery.Stream(cmd, this.Response.Body, EMPTY_PRODUCTS_ARRAY); - } + await this.sqlQuery.Sql("EXEC GetProductsAsOf @date") + .Param("date", date) + .Stream(this.Response.Body, EMPTY_PRODUCTS_ARRAY); } [HttpGet("restore")] [Produces("application/json")] public void RestoreVersion(int ProductId, DateTime DateModified) { - var cmd = new SqlCommand("EXEC RestoreProduct @productid, @date"); - cmd.Parameters.AddWithValue("@productid", ProductId); - cmd.Parameters.AddWithValue("@date", DateModified); this.sqlCmd + .Sql("EXEC RestoreProduct @productid, @date") + .Param("productid", ProductId) + .Param("date", DateModified) .OnError( ex => { @@ -134,7 +135,7 @@ FOR JSON PATH, WITHOUT_ARRAY_WRAPPER"); this.Response.StatusCode = 500; throw ex; }) - .ExecuteNonQuery(cmd); + .Exec(); } @@ -143,12 +144,13 @@ FOR JSON PATH, WITHOUT_ARRAY_WRAPPER"); public async Task Report1() { await sqlQuery - .Stream(@" + .Sql(@" select color as x, sum(quantity) as y from product where color is not null group by color -for json path", Response.Body, EMPTY_PRODUCTS_ARRAY); +for json path") + .Stream(Response.Body, EMPTY_PRODUCTS_ARRAY); } @@ -157,7 +159,7 @@ for json path", Response.Body, EMPTY_PRODUCTS_ARRAY); public async Task Report2() { await sqlQuery - .Stream(@" + .Sql(@" select name as [key], [values].x, [values].y from company join (select companyid, color as x, sum(quantity) as y @@ -166,7 +168,8 @@ select name as [key], [values].x, [values].y group by companyid, color ) as [values] on company.companyid = [values].companyid order by company.companyid -for json auto", Response.Body, EMPTY_PRODUCTS_ARRAY); +for json auto") + .Stream(Response.Body, EMPTY_PRODUCTS_ARRAY); } } } \ No newline at end of file diff --git a/samples/demos/belgrade-product-catalog-demo/ProductCatalog.xproj b/samples/demos/belgrade-product-catalog-demo/ProductCatalog.xproj deleted file mode 100644 index d6cd6870..00000000 --- a/samples/demos/belgrade-product-catalog-demo/ProductCatalog.xproj +++ /dev/null @@ -1,19 +0,0 @@ - - - - 14.0 - $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) - - - - 7e230e5a-b0b6-4f56-9561-942fd1817b80 - ProductCatalog - .\obj - .\bin\ - v4.6 - - - 2.0 - - - \ No newline at end of file diff --git a/samples/demos/belgrade-product-catalog-demo/Startup.cs b/samples/demos/belgrade-product-catalog-demo/Startup.cs index 4e944823..d686ed3c 100644 --- a/samples/demos/belgrade-product-catalog-demo/Startup.cs +++ b/samples/demos/belgrade-product-catalog-demo/Startup.cs @@ -9,10 +9,9 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using ProductCatalog.Models; -using Serilog; -#if NET46 +using Serilog; +using Serilog.Sinks; using Serilog.Sinks.MSSqlServer; -#endif using System; using System.Data.SqlClient; using System.Linq; @@ -29,7 +28,7 @@ namespace ProductCatalog .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddEnvironmentVariables(); Configuration = builder.Build(); -#if NETCOREAPP1_0 +#if NETCOREAPP2_0 Log.Logger = new LoggerConfiguration() .WriteTo.RollingFile(new Serilog.Formatting.Json.JsonFormatter(), System.IO.Path.Combine(env.ContentRootPath, "logs\\log-{Date}.ndjson")) .CreateLogger(); @@ -37,14 +36,16 @@ namespace ProductCatalog #if NET46 var columnOptions = new ColumnOptions(); // Don't include the Properties XML column. + columnOptions.Store.Remove(StandardColumn.Id); columnOptions.Store.Remove(StandardColumn.Properties); columnOptions.Store.Remove(StandardColumn.MessageTemplate); columnOptions.Store.Remove(StandardColumn.Exception); + columnOptions.TimeStamp.ColumnName = "EventTime"; // Do include the log event data as JSON. columnOptions.Store.Add(StandardColumn.LogEvent); Log.Logger = new LoggerConfiguration() - .WriteTo.MSSqlServer(Configuration["ConnectionStrings:BelgradeDemo"], "dbo.Logs", columnOptions: columnOptions) + .WriteTo.MSSqlServer(Configuration["ConnectionStrings:BelgradeDemo"], "Logs", columnOptions: columnOptions, autoCreateSqlTable: false) .CreateLogger(); #endif } diff --git a/samples/demos/belgrade-product-catalog-demo/appsettings.json b/samples/demos/belgrade-product-catalog-demo/appsettings.json index edbf02e3..df35eb1e 100644 --- a/samples/demos/belgrade-product-catalog-demo/appsettings.json +++ b/samples/demos/belgrade-product-catalog-demo/appsettings.json @@ -8,6 +8,6 @@ } }, "ConnectionStrings": { - "BelgradeDemo": "Server=.\\SQLEXPRESS;Database=ProductCatalog;Integrated Security=true" + "BelgradeDemo": "Server=.\\SQLEXPRESS;Database=ProductCatalog;Integrated Security=true;Application Name=Belgrade" } } \ No newline at end of file diff --git a/samples/demos/belgrade-product-catalog-demo/belgrade-product-catalog-demo.csproj b/samples/demos/belgrade-product-catalog-demo/belgrade-product-catalog-demo.csproj new file mode 100644 index 00000000..dff40519 --- /dev/null +++ b/samples/demos/belgrade-product-catalog-demo/belgrade-product-catalog-demo.csproj @@ -0,0 +1,57 @@ + + + + true + belgrade-product-catalog-demo + Exe + belgrade-product-catalog-demo + .NETFramework + v4.6 + + + + + + PreserveNewest + + + + + + + + + + + + + + + + + + + + + + + + + + + + True + True + bcp.sql.tt + + + TextTemplatingFileGenerator + bcp.sql.sql + + + + + + + + diff --git a/samples/demos/belgrade-product-catalog-demo/project.json b/samples/demos/belgrade-product-catalog-demo/project.json deleted file mode 100644 index 4f3b71e3..00000000 --- a/samples/demos/belgrade-product-catalog-demo/project.json +++ /dev/null @@ -1,63 +0,0 @@ -{ - "dependencies": { - "Belgrade.Sql.Client": "0.7", - "Microsoft.AspNetCore.Mvc": "1.0.0", - "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0", - "Microsoft.AspNetCore.Server.Kestrel": "1.0.0", - "Microsoft.AspNetCore.Session": "1.0.0", - "Microsoft.AspNetCore.StaticFiles": "1.0.0", - "Microsoft.EntityFrameworkCore.SqlServer": "1.0.1", - "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0", - "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0", - "Microsoft.Extensions.Configuration.Json": "1.0.0", - "Microsoft.Extensions.Logging": "1.0.0", - "Microsoft.Extensions.Logging.Console": "1.0.0", - "Microsoft.Extensions.Logging.Debug": "1.0.0", - "Serilog": "2.3.0", - "Serilog.Extensions.Logging": "1.3.1", - "Serilog.Sinks.PeriodicBatching": "2.1.0", - "Serilog.Sinks.RollingFile": "3.3.0", - "System.Data.SqlClient": "4.1.0" - }, - - "tools": { - "Microsoft.AspNetCore.Server.IISIntegration.Tools": { - "version": "1.0.0-preview2-final", - "imports": "portable-net45+win8+dnxcore50" - } - }, - - "frameworks": { - "netcoreapp1.0": { - "dependencies": { - "Microsoft.NETCore.App": { - "version": "1.0.0", - "type": "platform" - } - } - }, - "net46": { - "dependencies": { - "Serilog.Sinks.MSSqlServer": "4.2.0" - } - } - }, - - "buildOptions": { - "emitEntryPoint": true, - "preserveCompilationContext": true - }, - - "publishOptions": { - "include": [ - "wwwroot", - "Views", - "appsettings.json", - "web.config" - ] - }, - - "scripts": { - "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ] - } -} diff --git a/samples/demos/belgrade-product-catalog-demo/sql-scripts/1 setup.sql b/samples/demos/belgrade-product-catalog-demo/sql-scripts/1 setup.sql index e34b7e97..f7b2a1b1 100644 --- a/samples/demos/belgrade-product-catalog-demo/sql-scripts/1 setup.sql +++ b/samples/demos/belgrade-product-catalog-demo/sql-scripts/1 setup.sql @@ -115,13 +115,10 @@ END GO DROP TABLE IF EXISTS Logs; GO -CREATE TABLE Logs ( - Id int IDENTITY PRIMARY KEY, - Message nvarchar(max) NULL, - MessageTemplate nvarchar(max) NULL, - Level nvarchar(128) NULL, - TimeStamp datetimeoffset(7) NOT NULL, - Exception nvarchar(max) NULL, - Properties xml NULL, - LogEvent nvarchar(max) NULL -); \ No newline at end of file + +CREATE TABLE Logs( + [Message] NVARCHAR(4000) NOT NULL, + [Level] VARCHAR(16) NOT NULL, + [EventTime] DATETIME2 (7) NOT NULL, + [LogEvent] NVARCHAR(max) NULL +) \ No newline at end of file