Updated data seeding to re-populate cart when items run out!
This commit is contained in:
parent
d65b916a94
commit
5af6c2d8b5
5 changed files with 60 additions and 20 deletions
5
Client/Pages/Cart/Cart.razor
Normal file
5
Client/Pages/Cart/Cart.razor
Normal file
|
@ -0,0 +1,5 @@
|
|||
@page "/cart"
|
||||
|
||||
<h3>Your Shopping Cart</h3>
|
||||
|
||||
<Item />
|
13
Client/Pages/Cart/Item.razor
Normal file
13
Client/Pages/Cart/Item.razor
Normal 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>
|
|
@ -16,4 +16,17 @@
|
|||
<ProjectReference Include="..\Shared\ShoppingCartStarter.Shared.csproj" />
|
||||
</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>
|
||||
|
|
BIN
Client/wwwroot/images/test-image.jpg
Normal file
BIN
Client/wwwroot/images/test-image.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.3 KiB |
|
@ -13,30 +13,39 @@ namespace ShoppingCartStarter.Server.Data
|
|||
/// <summary>
|
||||
/// Seed dummy cart data
|
||||
/// </summary>
|
||||
/// <param name="app"></param>
|
||||
/// <param name="env"></param>
|
||||
public static void InitialiseDatabase(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
{
|
||||
using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
|
||||
using (var context = serviceScope.ServiceProvider.GetRequiredService<StoreContext>())
|
||||
{
|
||||
using var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope();
|
||||
using var context = serviceScope.ServiceProvider.GetRequiredService<StoreContext>();
|
||||
|
||||
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 = "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.Carts.Add(cart);
|
||||
|
||||
context.SaveChanges();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue