Updated data seeding to re-populate cart when items run out!

This commit is contained in:
Jon Hilton 2020-06-24 11:07:11 +01:00
parent d65b916a94
commit 5af6c2d8b5
5 changed files with 60 additions and 20 deletions

View file

@ -0,0 +1,5 @@
@page "/cart"
<h3>Your Shopping Cart</h3>
<Item />

View file

@ -0,0 +1,13 @@
<div class="row border-bottom py-4">
<div class="col-sm">
<div class="media">
<img src="/images/test-image.jpg" altText="smart speaker" class="mr-3" alt="Smart Speaker"/>
<div class="media-body">
<h3>Smart Speaker</h3>
</div>
</div>
</div>
<div class="col-sm-2">
£22.00
</div>
</div>

View file

@ -16,4 +16,17 @@
<ProjectReference Include="..\Shared\ShoppingCartStarter.Shared.csproj" /> <ProjectReference Include="..\Shared\ShoppingCartStarter.Shared.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Content Update="wwwroot\images\test-image.jpg">
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
<Content Update="Pages\Cart\Cart.razor">
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</Content>
<Content Update="Pages\Cart\Item.razor">
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</Content>
</ItemGroup>
</Project> </Project>

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

View file

@ -13,30 +13,39 @@ namespace ShoppingCartStarter.Server.Data
/// <summary> /// <summary>
/// Seed dummy cart data /// Seed dummy cart data
/// </summary> /// </summary>
/// <param name="app"></param>
/// <param name="env"></param>
public static void InitialiseDatabase(IApplicationBuilder app, IWebHostEnvironment env) public static void InitialiseDatabase(IApplicationBuilder app, IWebHostEnvironment env)
{ {
using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope()) using var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope();
using (var context = serviceScope.ServiceProvider.GetRequiredService<StoreContext>()) using var context = serviceScope.ServiceProvider.GetRequiredService<StoreContext>();
{
context.Database.Migrate(); context.Database.Migrate();
if (!context.Carts.Any()) ShoppingCart cart;
if (context.Carts.Any())
{ {
var cart = new ShoppingCart cart = context.Carts
.Include(x=>x.LineItems)
.FirstOrDefault();
}
else
{ {
LineItems = new List<LineItem> 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 = "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 = "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 = "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.Carts.Add(cart);
context.SaveChanges(); context.SaveChanges();
} }
} }
} }
}
}