Skip to content

Commit 10f80cd

Browse files
committed
Updated Belgrade Product Catalog Demo
1. Added Logging with serilog 2. Added reporting with NVD3 3. Refactored to use server-side MVC views instead of HTML page
1 parent 26f88d5 commit 10f80cd

File tree

20 files changed

+844
-19
lines changed

20 files changed

+844
-19
lines changed

samples/demos/BelgradeProductCatalogDemo/Controllers/CompanyController.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,19 @@ public async Task Get()
2828
[HttpGet("login")]
2929
public void Login(string id)
3030
{
31-
try
31+
if(id=="0")
32+
ControllerContext.HttpContext.Session.Remove("CompanyID");
33+
else
34+
ControllerContext.HttpContext.Session.SetString("CompanyID", id);
35+
string referer;
36+
switch (Request.Query["page"])
3237
{
33-
if(id=="0")
34-
ControllerContext.HttpContext.Session.Remove("CompanyID");
35-
else
36-
ControllerContext.HttpContext.Session.SetString("CompanyID", id);
37-
Response.Redirect("/index.html");
38-
} catch (Exception ex)
39-
{
40-
Response.WriteAsync(ex.Message);
38+
case "index": referer = "/Home/Index"; break;
39+
case "report1": referer = "/Home/Report1"; break;
40+
case "report2": referer = "/Home/Report2"; break;
41+
default: referer = "/index.html";break;
4142
}
43+
Response.Redirect(referer);
4244
}
4345
}
4446
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using System;
3+
4+
namespace ProductCatalog.Controllers
5+
{
6+
public class HomeController : Controller
7+
{
8+
[HttpGet]
9+
public IActionResult Index()
10+
{
11+
ViewData["page"] = "index";
12+
return View();
13+
}
14+
15+
public IActionResult Report1()
16+
{
17+
ViewData["page"] = "report1";
18+
return View();
19+
}
20+
21+
public IActionResult Report2()
22+
{
23+
ViewData["page"] = "report2";
24+
return View();
25+
}
26+
}
27+
}

samples/demos/BelgradeProductCatalogDemo/Controllers/ProductController.cs

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Data.SqlClient;
66
using System.IO;
7-
using System.Text;
87
using System.Threading.Tasks;
98

109
// For more information on enabling Web API for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
@@ -17,7 +16,7 @@ public class ProductController : Controller
1716
IQueryPipe sqlQuery = null;
1817
ICommand sqlCmd = null;
1918
private readonly string EMPTY_PRODUCTS_ARRAY = "{\"data\":[]}";
20-
private readonly byte[] EMPTY_PRODUCTS_ARRAY_GZIPPED = new byte[] {0x1F,0x8B,0x08,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0xAB,0x66,0x50,0x62,0x48,0x61,0x48,0x64,0x28,0x01,0x62,0x25,0x06,0x2B,0x86,0x68,0x86,0x58,0x86,0x5A,0x06,0x00,0xB3,0x4C,0x62,0xB2,0x16,0x00,0x00,0x00};
19+
private readonly byte[] EMPTY_PRODUCTS_ARRAY_GZIPPED = new byte[] { 0x1F, 0x8B, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0xAB, 0x66, 0x50, 0x62, 0x48, 0x61, 0x48, 0x64, 0x28, 0x01, 0x62, 0x25, 0x06, 0x2B, 0x86, 0x68, 0x86, 0x58, 0x86, 0x5A, 0x06, 0x00, 0xB3, 0x4C, 0x62, 0xB2, 0x16, 0x00, 0x00, 0x00 };
2120
private readonly ILogger logger;
2221

2322
public ProductController(IQueryPipe sqlQueryService, ICommand sqlCommandService, ILogger<ProductController> logger)
@@ -32,10 +31,12 @@ public async Task Get()
3231
{
3332
await sqlQuery
3433
.OnError(
35-
ex => { logger.LogError("Error while trying to get products: {Error}\n{StackTrace}", ex.Message, ex.StackTrace);
34+
ex =>
35+
{
36+
logger.LogError("Error while trying to get products: {Error}\n{StackTrace}", ex.Message, ex.StackTrace);
3637
this.Response.StatusCode = 500;
3738
throw ex;
38-
})
39+
})
3940
.Stream(@"
4041
select ProductID, Name, Color, Price, Quantity,
4142
JSON_VALUE(Data, '$.MadeIn') as MadeIn, JSON_QUERY(Tags) as Tags
@@ -73,7 +74,7 @@ from Product
7374
cmd.Parameters.AddWithValue("id", id);
7475
await sqlQuery.Stream(cmd, Response.Body, "{}");
7576
}
76-
77+
7778
// POST api/Product
7879
[HttpPost]
7980
public async Task Post()
@@ -127,12 +128,45 @@ public void RestoreVersion(int ProductId, DateTime DateModified)
127128
cmd.Parameters.AddWithValue("@date", DateModified);
128129
this.sqlCmd
129130
.OnError(
130-
ex => {
131+
ex =>
132+
{
131133
logger.LogError("Error while trying to restore product with id {ProductID} from time {DateModified}.\n{Error}\n{StackTrace}", ProductId, DateModified, ex.Message, ex.StackTrace);
132134
this.Response.StatusCode = 500;
133135
throw ex;
134136
})
135137
.ExecuteNonQuery(cmd);
136138
}
139+
140+
141+
[HttpGet("Report1")]
142+
[Produces("application/json")]
143+
public async Task Report1()
144+
{
145+
await sqlQuery
146+
.Stream(@"
147+
select color as x, sum(quantity) as y
148+
from product
149+
where color is not null
150+
group by color
151+
for json path", Response.Body, EMPTY_PRODUCTS_ARRAY);
152+
}
153+
154+
155+
[HttpGet("Report2")]
156+
[Produces("application/json")]
157+
public async Task Report2()
158+
{
159+
await sqlQuery
160+
.Stream(@"
161+
select name as [key], [values].x, [values].y
162+
from company
163+
join (select companyid, color as x, sum(quantity) as y
164+
from product
165+
where color is not null
166+
group by companyid, color
167+
) as [values] on company.companyid = [values].companyid
168+
order by company.companyid
169+
for json auto", Response.Body, EMPTY_PRODUCTS_ARRAY);
170+
}
137171
}
138172
}

samples/demos/BelgradeProductCatalogDemo/Properties/launchSettings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
"IIS Express": {
1212
"commandName": "IISExpress",
1313
"launchBrowser": true,
14-
"launchUrl": "index.html",
14+
"launchUrl": "Home/Index",
1515
"environmentVariables": {
1616
"ASPNETCORE_ENVIRONMENT": "Development"
1717
}
1818
},
1919
"ProductCatalog": {
2020
"commandName": "Project",
2121
"launchBrowser": true,
22-
"launchUrl": "http://localhost:5000/index.html",
22+
"launchUrl": "http://localhost:5000/Home/Index",
2323
"environmentVariables": {
2424
"ASPNETCORE_ENVIRONMENT": "Development"
2525
}

samples/demos/BelgradeProductCatalogDemo/Startup.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,14 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
8080
loggerFactory.AddSerilog();
8181

8282
app.UseSession();
83-
app.UseMvc();
8483
app.UseStaticFiles();
84+
app.UseMvc(routes =>
85+
{
86+
routes.MapRoute(
87+
name: "default",
88+
template: "{controller=Home}/{action=Index}");
89+
});
90+
8591
}
8692

8793
/// <summary>
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<div class="container">
2+
<div class="clearfix">
3+
<h1 class="pull-left">Products<span id="snapshot"></span></h1>
4+
<button id="addProduct" type="button" class="btn btn-primary pull-right" data-toggle="modal" data-target="#modalAddProduct">
5+
<span class="glyphicon glyphicon-plus"></span> Add
6+
</button>
7+
</div>
8+
9+
<!-- JQuery slider for temporal -->
10+
<div id="slider" class="pull-left" style="float:left;display:inline-block;margin-bottom:20px;margin-right:20px;"></div>
11+
<!-- End JQuery slider for temporal -->
12+
<!-- Bootstrap Modal -->
13+
<div class="modal fade" id="modalEditProduct" tabindex="-1" role="dialog" aria-labelledby="myModalEditLabel">
14+
<div class="modal-dialog" role="document">
15+
<div class="modal-content">
16+
<div class="modal-header">
17+
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
18+
<h4 class="modal-title" id="myModalEditLabel">Edit Product Details</h4>
19+
</div>
20+
<div class="modal-body">
21+
22+
<!-- Bootstrap form -->
23+
<form id="EditProductForm">
24+
<input type="hidden" id="ProductID" value="" />
25+
<div class="form-group">
26+
<label for="Name">Title</label>
27+
<input type="text" class="form-control" name="Name" placeholder="Title">
28+
</div>
29+
<div class="form-group">
30+
<label for="Color">Color</label>
31+
<select class="form-control" name="Color">
32+
<option value="">N/A</option>
33+
<option value="White">White</option>
34+
<option value="Silver">Silver</option>
35+
<option value="Magenta">Magenta</option>
36+
<option value="Red">Red</option>
37+
<option value="Multi">Multi</option>
38+
<option value="Black">Black</option>
39+
</select>
40+
</div>
41+
<div class="form-group">
42+
<label for="Price" class="field-label">Price</label>
43+
<input type="text" id="Price" name="Price" class="form-control">
44+
</div>
45+
<div class="form-group">
46+
<label for="Quantity" class="field-label">Quantity</label>
47+
<input type="text" id="Quantity" name="Quantity" class="form-control">
48+
</div>
49+
<div class="form-group">
50+
<h3 id="Company"></h3>
51+
<p id="Address"></p>
52+
<p>Contact: <span id="Email"></span>, <span id="Phone"></span></p>
53+
</div>
54+
<!-- Allow form submission with keyboard without duplicating the dialog button -->
55+
<input type="submit" tabindex="-1" style="position:absolute; top:-1000px">
56+
57+
</form>
58+
<!-- End Bootstrap form -->
59+
60+
</div>
61+
<div class="modal-footer">
62+
<button id="cancelEditButton" type="reset" class="btn btn-link" data-dismiss="modal">Close</button>
63+
<button id="submitEditButton" type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-floppy-save"></span> Save</button>
64+
</div>
65+
</div>
66+
</div>
67+
</div>
68+
<!-- End Bootstrap modal -->
69+
<!-- Bootstrap Modal (ADD) -->
70+
<div class="modal fade" id="modalAddProduct" tabindex="-1" role="dialog" aria-labelledby="myModalAddProductLabel">
71+
<div class="modal-dialog" role="document">
72+
<div class="modal-content">
73+
<div class="modal-header">
74+
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
75+
<h4 class="modal-title" id="myModalAddProductLabel">Add new product</h4>
76+
</div>
77+
<div class="modal-body">
78+
79+
<!-- Bootstrap form -->
80+
<form id="AddProductForm">
81+
<!--<input type="hidden" id="ProductID" value="" />-->
82+
<div class="form-group">
83+
<label for="Name">Title</label>
84+
<input type="text" class="form-control" name="Name" placeholder="Title">
85+
</div>
86+
<div class="form-group">
87+
<label for="Color">Color</label>
88+
<select class="form-control" name="Color">
89+
<option value="">N/A</option>
90+
<option value="White">White</option>
91+
<option value="Silver">Silver</option>
92+
<option value="Magenta">Magenta</option>
93+
<option value="Red">Red</option>
94+
<option value="Multi">Multi</option>
95+
<option value="Black">Black</option>
96+
</select>
97+
</div>
98+
<div class="form-group">
99+
<label for="Price" class="field-label">Price</label>
100+
<input type="text" name="Price" class="form-control">
101+
</div>
102+
<div class="form-group">
103+
<label for="Quantity" class="field-label">Quantity</label>
104+
<input type="text" name="Quantity" class="form-control">
105+
</div>
106+
<div class="form-group">
107+
<label for="Company">Company</label>
108+
<select class="form-control" name="CompanyID" id="CompanyList"></select>
109+
</div>
110+
111+
<!-- Allow form submission with keyboard without duplicating the dialog button -->
112+
<input type="submit" tabindex="-1" style="position:absolute; top:-1000px">
113+
114+
</form>
115+
<!-- End Bootstrap form -->
116+
117+
</div>
118+
<div class="modal-footer">
119+
<button id="cancelAddButton" type="reset" class="btn btn-link" data-dismiss="modal">Close</button>
120+
<button id="submitAddButton" type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-floppy-save"></span> Save</button>
121+
</div>
122+
</div>
123+
</div>
124+
</div>
125+
<!-- End Bootstrap modal -->
126+
<!-- JQuery DataTable -->
127+
<table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
128+
<thead>
129+
<tr>
130+
<th></th>
131+
<th>Product</th>
132+
<th>Color</th>
133+
<th>Price</th>
134+
<th>Quantity</th>
135+
<th>Made in</th>
136+
<th>Tags</th>
137+
<th>Edit</th>
138+
<th>Delete</th>
139+
<th>Restore</th>
140+
</tr>
141+
</thead>
142+
<tbody></tbody>
143+
</table>
144+
<!-- End JQuery DataTable -->
145+
</div>
146+
147+
@section styles {
148+
<link href="~/media/css/jquery-ui/jquery-ui.css" rel="stylesheet" />
149+
<link href="~/media/css/dataTables.bootstrap.css" rel="stylesheet" />
150+
<link href="~/media/css/toastr.min.css" rel="stylesheet" />
151+
<link href="~/media/css/products.css" rel="stylesheet" />
152+
}
153+
154+
@section scripts {
155+
<script src="~/media/js/lib/jquery-ui.js"></script>
156+
<script src="~/media/js/lib/jquery.dataTables.js"></script>
157+
<script src="~/media/js/lib/jquery.dataTables.Bootstrap.js"></script>
158+
<script src="~/media/js/lib/jquery.html-template.js"></script>
159+
<script src="~/media/js/lib/jquery.serializejson.js"></script>
160+
<script src="~/media/js/lib/toastr.min.js"></script>
161+
<script src="~/media/js/products.js"></script>
162+
<script src="~/media/js/products-temporal.js"></script>
163+
}

0 commit comments

Comments
 (0)