From 5af6c2d8b58b42c210e69f6cbe194e6059100d1e Mon Sep 17 00:00:00 2001 From: Jon Hilton Date: Wed, 24 Jun 2020 11:07:11 +0100 Subject: [PATCH] Updated data seeding to re-populate cart when items run out! --- Client/Pages/Cart/Cart.razor | 5 +++ Client/Pages/Cart/Item.razor | 13 ++++++ Client/ShoppingCartStarter.Client.csproj | 13 ++++++ Client/wwwroot/images/test-image.jpg | Bin 0 -> 1373 bytes Server/Data/Database.cs | 49 ++++++++++++++--------- 5 files changed, 60 insertions(+), 20 deletions(-) create mode 100644 Client/Pages/Cart/Cart.razor create mode 100644 Client/Pages/Cart/Item.razor create mode 100644 Client/wwwroot/images/test-image.jpg diff --git a/Client/Pages/Cart/Cart.razor b/Client/Pages/Cart/Cart.razor new file mode 100644 index 0000000..f4b2d89 --- /dev/null +++ b/Client/Pages/Cart/Cart.razor @@ -0,0 +1,5 @@ +@page "/cart" + +

Your Shopping Cart

+ + \ No newline at end of file diff --git a/Client/Pages/Cart/Item.razor b/Client/Pages/Cart/Item.razor new file mode 100644 index 0000000..b65c3e3 --- /dev/null +++ b/Client/Pages/Cart/Item.razor @@ -0,0 +1,13 @@ +
+
+
+ Smart Speaker +
+

Smart Speaker

+
+
+
+
+ £22.00 +
+
\ No newline at end of file diff --git a/Client/ShoppingCartStarter.Client.csproj b/Client/ShoppingCartStarter.Client.csproj index dad7fb9..e304ab2 100644 --- a/Client/ShoppingCartStarter.Client.csproj +++ b/Client/ShoppingCartStarter.Client.csproj @@ -16,4 +16,17 @@ + + + true + PreserveNewest + + + true + + + true + + + diff --git a/Client/wwwroot/images/test-image.jpg b/Client/wwwroot/images/test-image.jpg new file mode 100644 index 0000000000000000000000000000000000000000..8e29493975cec43467669cf52313418478160c89 GIT binary patch literal 1373 zcmex=_1P|rX?qqI0P zFI~aY%U!`Mz|~!$%*;qrMU_b4?usLlYAdd38%$3nLpnV-q8g zA&i`yoIKn-61=<;Mv5|uMkIs(2N(o77^X2yV`fxhU=n0x7G(T?gh8HxfsqyHTabsL zfRTxrg_Vt+gOiIJs9>uA0}~@NGZPClD=P~NP<1U(o`FS>RY=j$kxe)-kzJ`!#HexN zLJno8jR!@8E`CrkPAY2R|p1*kc>f@)+U%r0({^RE_kiQrim?7SR z`wY!rf#!;fTYe5T~lV1kI?33!0gt&Lt<0WvxAucU@me zg73t!&}05V_55qizn2SHpJ>k97PO$R;ko^#2K|d6cQ#(S=B(&0xcc4s0^_rGM{h;& ztUI!KqiXT@>ZAspJn5zSe-u8AJkI zG4XZRNoMYnVG_!>=DIRC>#i~0kSVOfQ>v_d;n$_pxjeUPP8_^tU|sm~%gT%eY&M=s zt)-`$IOP4NsANi81~xq}F1~X4#R9e|!3vY*4Okt&S^Q^U4D60zxtG!Z{9N(mo?nlb hWo2nFX}n$7<*cN@Q~jS|-c((Y07loLgW3Pz1OTBB&kFzm literal 0 HcmV?d00001 diff --git a/Server/Data/Database.cs b/Server/Data/Database.cs index 2183663..6eff56b 100644 --- a/Server/Data/Database.cs +++ b/Server/Data/Database.cs @@ -13,30 +13,39 @@ namespace ShoppingCartStarter.Server.Data /// /// Seed dummy cart data /// - /// - /// public static void InitialiseDatabase(IApplicationBuilder app, IWebHostEnvironment env) { - using (var serviceScope = app.ApplicationServices.GetService().CreateScope()) - using (var context = serviceScope.ServiceProvider.GetRequiredService()) - { - context.Database.Migrate(); + using var serviceScope = app.ApplicationServices.GetService().CreateScope(); + using var context = serviceScope.ServiceProvider.GetRequiredService(); + + context.Database.Migrate(); - if (!context.Carts.Any()) - { - var cart = new ShoppingCart - { - LineItems = new List - { - new LineItem{ Image = "test-image.jpg", Name="Big T-shirt", Price=39.50m, Quantity = 2 }, - new LineItem{ Image = "test-image.jpg", Name="Small White T-shirt", Price=19.50m, Quantity = 1 }, - new LineItem{ Image = "test-image.jpg", Name="Smart Speaker", Price=23.00m, Quantity = 1 }, - } - }; - context.Carts.Add(cart); - context.SaveChanges(); - } + ShoppingCart cart; + if (context.Carts.Any()) + { + cart = context.Carts + .Include(x=>x.LineItems) + .FirstOrDefault(); } + else + { + cart = new ShoppingCart(); + context.Carts.Add(cart); + } + + if (cart?.LineItems.Count == 0) + { + cart.LineItems.AddRange(new[] + { + new LineItem {Image = "test-image.jpg", Name = "Big T-shirt", Price = 39.50m, Quantity = 2}, + new LineItem {Image = "test-image.jpg", Name = "Small White T-shirt", Price = 19.50m, Quantity = 1}, + new LineItem {Image = "test-image.jpg", Name = "Smart Speaker", Price = 23.00m, Quantity = 1}, + new LineItem {Image = "test-image.jpg", Name = "Dumb Speaker", Price = 99.00m, Quantity = 3}, + new LineItem {Image = "test-image.jpg", Name = "Giraffe Poster", Price = 9.00m, Quantity = 20} + }); + } + + context.SaveChanges(); } } } \ No newline at end of file