From 81cd7166eb3bf528c6ae41935d060969822f4a4d Mon Sep 17 00:00:00 2001 From: Jovan Popovic Date: Sat, 4 Feb 2017 23:04:11 +0100 Subject: [PATCH] Updated product catalog demo Added Entity Framework Core Example Refactored code --- .../BelgradeProductCatalogDemo/.gitignore | 3 +- .../Controllers/CompanyController.cs | 9 +- .../Controllers/ProductCatalogController.cs | 50 ++++ .../Models/Models.cs | 74 ++++++ .../Properties/launchSettings.json | 4 +- .../BelgradeProductCatalogDemo/Startup.cs | 11 +- .../{Home => ProductCatalog}/Index.cshtml | 105 +++------ .../{Home => ProductCatalog}/Report1.cshtml | 27 +-- .../{Home => ProductCatalog}/Report2.cshtml | 4 +- .../Views/Shared/_Layout.cshtml | 10 +- .../appsettings.json | 8 +- .../BelgradeProductCatalogDemo/project.json | 1 + .../A4 structured log analysis.sql | 10 +- .../wwwroot/data/nvd3-multibar.js | 26 ++- .../wwwroot/data/nvd3-pie.js | 14 +- .../wwwroot/index.html | 26 +-- .../wwwroot/media/js/products-crud.js | 114 +++++++++ .../wwwroot/media/js/products-temporal.js | 47 +++- .../wwwroot/media/js/products.js | 125 +--------- .../wwwroot/media/js/rls.js | 12 + .../wwwroot/report-multibar.html | 35 +-- .../wwwroot/report-pie.html | 32 +-- .../wwwroot/temporal.html | 218 ++++++++++++++++++ 23 files changed, 657 insertions(+), 308 deletions(-) create mode 100644 samples/demos/BelgradeProductCatalogDemo/Controllers/ProductCatalogController.cs create mode 100644 samples/demos/BelgradeProductCatalogDemo/Models/Models.cs rename samples/demos/BelgradeProductCatalogDemo/Views/{Home => ProductCatalog}/Index.cshtml (53%) rename samples/demos/BelgradeProductCatalogDemo/Views/{Home => ProductCatalog}/Report1.cshtml (71%) rename samples/demos/BelgradeProductCatalogDemo/Views/{Home => ProductCatalog}/Report2.cshtml (91%) create mode 100644 samples/demos/BelgradeProductCatalogDemo/wwwroot/media/js/products-crud.js create mode 100644 samples/demos/BelgradeProductCatalogDemo/wwwroot/media/js/rls.js create mode 100644 samples/demos/BelgradeProductCatalogDemo/wwwroot/temporal.html diff --git a/samples/demos/BelgradeProductCatalogDemo/.gitignore b/samples/demos/BelgradeProductCatalogDemo/.gitignore index d012fcb9..e1ffc141 100644 --- a/samples/demos/BelgradeProductCatalogDemo/.gitignore +++ b/samples/demos/BelgradeProductCatalogDemo/.gitignore @@ -8,4 +8,5 @@ obj/* *.lock.json Properties/PublishProfiles/* appsettings.Development.json -appsettings.Production.json \ No newline at end of file +appsettings.Production.json +*.ndjson \ No newline at end of file diff --git a/samples/demos/BelgradeProductCatalogDemo/Controllers/CompanyController.cs b/samples/demos/BelgradeProductCatalogDemo/Controllers/CompanyController.cs index f637420e..b79ee644 100644 --- a/samples/demos/BelgradeProductCatalogDemo/Controllers/CompanyController.cs +++ b/samples/demos/BelgradeProductCatalogDemo/Controllers/CompanyController.cs @@ -35,10 +35,11 @@ namespace ProductCatalog.Controllers string referer; switch (Request.Query["page"]) { - case "index": referer = "/Home/Index"; break; - case "report1": referer = "/Home/Report1"; break; - case "report2": referer = "/Home/Report2"; break; - default: referer = "/index.html";break; + case "index": referer = "/index.html"; break; + case "report1": referer = "/report-pie.html"; break; + case "report2": referer = "/report-multibar.html"; break; + case "temporal": referer = "/temporal.html"; break; + default: referer = "/index.html"; break; } Response.Redirect(referer); } diff --git a/samples/demos/BelgradeProductCatalogDemo/Controllers/ProductCatalogController.cs b/samples/demos/BelgradeProductCatalogDemo/Controllers/ProductCatalogController.cs new file mode 100644 index 00000000..a01f9a82 --- /dev/null +++ b/samples/demos/BelgradeProductCatalogDemo/Controllers/ProductCatalogController.cs @@ -0,0 +1,50 @@ +using Microsoft.AspNetCore.Mvc; +using ProductCatalog.Models; +using System; +using System.Linq; + +namespace ProductCatalog.Controllers +{ + public class ProductCatalogController : Controller + { + private ProductCatalogContext _context; + + public ProductCatalogController (ProductCatalogContext context) + { + _context = context; + } + + [HttpGet] + public IActionResult Index() + { + ViewData["page"] = "index"; + return View(_context.Products.AsEnumerable()); + } + + // POST api/ProductCatalog/Add + public IActionResult Add(Product p) + { + try + { + _context.Products.Add(p); + _context.SaveChanges(); + return Redirect("/ProductCatalog/Index"); + } catch (Exception) + { + return Redirect("/ProductCatalog/Index"); + } + } + + public IActionResult Report1() + { + ViewData["page"] = "report1"; + return View(); + } + + public IActionResult Report2() + { + ViewData["page"] = "report2"; + return View(); + } + } +} \ No newline at end of file diff --git a/samples/demos/BelgradeProductCatalogDemo/Models/Models.cs b/samples/demos/BelgradeProductCatalogDemo/Models/Models.cs new file mode 100644 index 00000000..2c0397ef --- /dev/null +++ b/samples/demos/BelgradeProductCatalogDemo/Models/Models.cs @@ -0,0 +1,74 @@ +using Microsoft.EntityFrameworkCore; +using Newtonsoft.Json; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace ProductCatalog.Models +{ + [Table("Product")] + public class Product + { + public int ProductId { get; set; } + + public string Name { get; set; } + + public string Color { get; set; } + + public string Size { get; set; } + + public decimal Price { get; set; } + + public int Quantity { get; set; } + + public int CompanyID { get; set; } + + [NotMapped] + public string[] Tags + { + get { return _Tags == null ? null : JsonConvert.DeserializeObject(_Tags); } + set { _Tags = JsonConvert.SerializeObject(value); } + } + + internal string _Tags { get; set; } + + [NotMapped] + public Properties Data + { + get { return (this._Data == null) ? null : JsonConvert.DeserializeObject(this._Data); } + set { _Data = JsonConvert.SerializeObject(value); } + } + + internal string _Data { get; set; } + } + + public class Properties + { + public string Type { get; set; } + + public string MadeIn { get; set; } + } + + public class ProductCatalogContext : DbContext + { + public ProductCatalogContext(DbContextOptions options) + : base(options) + { } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity() + .Property(x => x.ProductId) + .HasDefaultValueSql("NEXT VALUE FOR ProductId"); + + modelBuilder.Entity() + .Property(b => b._Tags).HasColumnName("Tags"); + + modelBuilder.Entity() + .Property(b => b._Data).HasColumnName("Data"); + } + + public DbSet Products { get; set; } + } + +} \ No newline at end of file diff --git a/samples/demos/BelgradeProductCatalogDemo/Properties/launchSettings.json b/samples/demos/BelgradeProductCatalogDemo/Properties/launchSettings.json index cb56d7d1..e834e31c 100644 --- a/samples/demos/BelgradeProductCatalogDemo/Properties/launchSettings.json +++ b/samples/demos/BelgradeProductCatalogDemo/Properties/launchSettings.json @@ -11,7 +11,7 @@ "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, - "launchUrl": "Home/Index", + "launchUrl": "ProductCatalog/Index", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } @@ -19,7 +19,7 @@ "ProductCatalog": { "commandName": "Project", "launchBrowser": true, - "launchUrl": "http://localhost:5000/Home/Index", + "launchUrl": "http://localhost:5000/ProductCatalog/Index", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/samples/demos/BelgradeProductCatalogDemo/Startup.cs b/samples/demos/BelgradeProductCatalogDemo/Startup.cs index dce08384..309f4c44 100644 --- a/samples/demos/BelgradeProductCatalogDemo/Startup.cs +++ b/samples/demos/BelgradeProductCatalogDemo/Startup.cs @@ -3,9 +3,11 @@ using Belgrade.SqlClient.SqlDb.Rls; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using ProductCatalog.Models; using Serilog; #if NET46 using Serilog.Sinks.MSSqlServer; @@ -51,9 +53,10 @@ namespace ProductCatalog // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { - //string ConnString = Configuration.GetConnectionString("BelgradeDemo"); string ConnString = Configuration["ConnectionStrings:BelgradeDemo"]; - + + services.AddDbContext(options => options.UseSqlServer(new SqlConnection(ConnString))); + // Adding data access services/components. services.AddTransient( sp => new QueryPipe(new SqlConnection(ConnString)) @@ -65,7 +68,7 @@ namespace ProductCatalog .AddRls("CompanyID", () => GetCompanyIdFromSession(sp)) ); - // Add framework services. + //// Add framework services. services.AddSingleton(); services.AddLogging(); services.AddSession(); @@ -85,7 +88,7 @@ namespace ProductCatalog { routes.MapRoute( name: "default", - template: "{controller=Home}/{action=Index}"); + template: "{controller=ProductCatalog}/{action=Index}"); }); } diff --git a/samples/demos/BelgradeProductCatalogDemo/Views/Home/Index.cshtml b/samples/demos/BelgradeProductCatalogDemo/Views/ProductCatalog/Index.cshtml similarity index 53% rename from samples/demos/BelgradeProductCatalogDemo/Views/Home/Index.cshtml rename to samples/demos/BelgradeProductCatalogDemo/Views/ProductCatalog/Index.cshtml index d239df84..47d6b9c3 100644 --- a/samples/demos/BelgradeProductCatalogDemo/Views/Home/Index.cshtml +++ b/samples/demos/BelgradeProductCatalogDemo/Views/ProductCatalog/Index.cshtml @@ -6,66 +6,6 @@ - -
- - - - @@ -158,6 +113,12 @@ - - + + + + } \ No newline at end of file diff --git a/samples/demos/BelgradeProductCatalogDemo/Views/Home/Report1.cshtml b/samples/demos/BelgradeProductCatalogDemo/Views/ProductCatalog/Report1.cshtml similarity index 71% rename from samples/demos/BelgradeProductCatalogDemo/Views/Home/Report1.cshtml rename to samples/demos/BelgradeProductCatalogDemo/Views/ProductCatalog/Report1.cshtml index f5b3b8c6..d5b7544c 100644 --- a/samples/demos/BelgradeProductCatalogDemo/Views/Home/Report1.cshtml +++ b/samples/demos/BelgradeProductCatalogDemo/Views/ProductCatalog/Report1.cshtml @@ -1,5 +1,4 @@ -

Products

-
+
@section styles { @@ -29,8 +28,8 @@ -} - - - - +} \ No newline at end of file diff --git a/samples/demos/BelgradeProductCatalogDemo/Views/Home/Report2.cshtml b/samples/demos/BelgradeProductCatalogDemo/Views/ProductCatalog/Report2.cshtml similarity index 91% rename from samples/demos/BelgradeProductCatalogDemo/Views/Home/Report2.cshtml rename to samples/demos/BelgradeProductCatalogDemo/Views/ProductCatalog/Report2.cshtml index c391e9ba..d843f2a6 100644 --- a/samples/demos/BelgradeProductCatalogDemo/Views/Home/Report2.cshtml +++ b/samples/demos/BelgradeProductCatalogDemo/Views/ProductCatalog/Report2.cshtml @@ -28,8 +28,8 @@ + @RenderSection("scripts", required: false) diff --git a/samples/demos/BelgradeProductCatalogDemo/appsettings.json b/samples/demos/BelgradeProductCatalogDemo/appsettings.json index 036d345c..edbf02e3 100644 --- a/samples/demos/BelgradeProductCatalogDemo/appsettings.json +++ b/samples/demos/BelgradeProductCatalogDemo/appsettings.json @@ -1,6 +1,6 @@ -{ +{ "Logging": { - "IncludeScopes": false, + "IncludeScopes": false, "LogLevel": { "Default": "Debug", "System": "Information", @@ -8,6 +8,6 @@ } }, "ConnectionStrings": { - "BelgradeDemo": "Server=.;Database=ProductCatalog;Integrated Security=true" + "BelgradeDemo": "Server=.\\SQLEXPRESS;Database=ProductCatalog;Integrated Security=true" } -} +} \ No newline at end of file diff --git a/samples/demos/BelgradeProductCatalogDemo/project.json b/samples/demos/BelgradeProductCatalogDemo/project.json index 2bd6dad0..0935c448 100644 --- a/samples/demos/BelgradeProductCatalogDemo/project.json +++ b/samples/demos/BelgradeProductCatalogDemo/project.json @@ -6,6 +6,7 @@ "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", diff --git a/samples/demos/BelgradeProductCatalogDemo/sql-scripts/A4 structured log analysis.sql b/samples/demos/BelgradeProductCatalogDemo/sql-scripts/A4 structured log analysis.sql index 49d32929..591e58f8 100644 --- a/samples/demos/BelgradeProductCatalogDemo/sql-scripts/A4 structured log analysis.sql +++ b/samples/demos/BelgradeProductCatalogDemo/sql-scripts/A4 structured log analysis.sql @@ -1,7 +1,13 @@ -SELECT +SELECT * FROM Logs + +SELECT JSON_VALUE(LogEvent, '$.Properties.RequestPath') as Url, * +FROM Logs +WHERE Level = 'Error' + +SELECT JSON_VALUE(LogEvent, '$.Properties.RequestPath') as Url, AVG( CAST(JSON_VALUE(LogEvent, '$.Properties.ElapsedMilliseconds') as float) ) as milliseconds FROM Logs WHERE JSON_VALUE(LogEvent, '$.Properties.RequestPath') IS NOT NULL -AND Timestamp BETWEEN '2017-01-09' AND '2017-01-10' +--AND Timestamp BETWEEN '2017-01-09' AND '2017-01-10' GROUP BY JSON_VALUE(LogEvent, '$.Properties.RequestPath') diff --git a/samples/demos/BelgradeProductCatalogDemo/wwwroot/data/nvd3-multibar.js b/samples/demos/BelgradeProductCatalogDemo/wwwroot/data/nvd3-multibar.js index c19319f6..10283b88 100644 --- a/samples/demos/BelgradeProductCatalogDemo/wwwroot/data/nvd3-multibar.js +++ b/samples/demos/BelgradeProductCatalogDemo/wwwroot/data/nvd3-multibar.js @@ -1 +1,25 @@ -[{ "key": "A Datum Corporation", "values": [{ "x": "Black", "y": 31.4900 }, { "x": "Magenta", "y": 100.0000 }, { "x": "Silver", "y": 359.9900 }] }, { "key": "Consolidated Messenger", "values": [{ "x": "Magenta", "y": 28.9900 }, { "x": "Red", "y": 41.9900 }, { "x": "White", "y": 120.9900 }] }, { "key": "Contoso, Ltd.", "values": [{ "x": "White", "y": 560.9900 }, { "x": "Magenta", "y": 15.9900 }, { "x": "Black", "y": 299.0200 }] }] \ No newline at end of file +[ + { + "key": "A Datum Corporation", + "values": [ + { "x": "Black", "y": 31.4900 }, + { "x": "Magenta", "y": 100.0000 }, + { "x": "Silver", "y": 359.9900 } + ] + }, + { + "key": "Consolidated Messenger", + "values": [ + { "x": "Magenta", "y": 28.9900 }, + { "x": "Red", "y": 41.9900 }, + { "x": "White", "y": 120.9900 } + ] + }, { + "key": "Contoso, Ltd.", + "values": [ + { "x": "White", "y": 560.9900 }, + { "x": "Magenta", "y": 15.9900 }, + { "x": "Black", "y": 299.0200 } + ] + } +] \ No newline at end of file diff --git a/samples/demos/BelgradeProductCatalogDemo/wwwroot/data/nvd3-pie.js b/samples/demos/BelgradeProductCatalogDemo/wwwroot/data/nvd3-pie.js index da84f97a..0b625c11 100644 --- a/samples/demos/BelgradeProductCatalogDemo/wwwroot/data/nvd3-pie.js +++ b/samples/demos/BelgradeProductCatalogDemo/wwwroot/data/nvd3-pie.js @@ -1,9 +1,9 @@ [ - { "key": "One", "y": 5 }, - { "key": "Two", "y": 2 }, - { "key": "Three", "y": 9 }, - { "key": "Four", "y": 7 }, - { "key": "Five", "y": 4 }, - { "key": "Six", "y": 3 }, - { "key": "Seven", "y": 0.5 } + { "x": "Red", "y": 5 }, + { "x": "Silver", "y": 2 }, + { "x": "Black", "y": 9 }, + { "x": "Red", "y": 7 }, + { "x": "Magenta", "y": 4 }, + { "x": "White", "y": 3 }, + { "x": "Blue", "y": 0.5 } ] \ No newline at end of file diff --git a/samples/demos/BelgradeProductCatalogDemo/wwwroot/index.html b/samples/demos/BelgradeProductCatalogDemo/wwwroot/index.html index 644d688c..658d8f38 100644 --- a/samples/demos/BelgradeProductCatalogDemo/wwwroot/index.html +++ b/samples/demos/BelgradeProductCatalogDemo/wwwroot/index.html @@ -23,9 +23,9 @@ - - - + + + @@ -38,23 +38,23 @@ - Product Catalog Demo + Product Catalog Demo