Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

namespace exercise.pizzashopapi.Data
{
public class DataContext : DbContext
public class DatabaseContext : DbContext
{
private string connectionString;
public DataContext()
public DatabaseContext()
{
var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
connectionString = configuration.GetValue<string>("ConnectionStrings:DefaultConnectionString");

connectionString = configuration.GetValue<string>("ConnectionStrings:DefaultConnectionString") ?? "";
this.Database.EnsureCreated();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
Expand All @@ -24,5 +24,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
public DbSet<Pizza> Pizzas { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<Topping> Toppings { get; set; }
public DbSet<OrderTopping> OrderToppings { get; set; }
}
}
25 changes: 17 additions & 8 deletions exercise.pizzashopapi/Data/Seeder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,35 @@ public static class Seeder
{
public async static void SeedPizzaShopApi(this WebApplication app)
{
using(var db = new DataContext())
using(var db = new DatabaseContext())
{
if(!db.Customers.Any())
{
db.Add(new Customer() { Name="Nigel" });
db.Add(new Customer() { Name = "Nigel" });
db.Add(new Customer() { Name = "Dave" });
db.Add(new Customer() { Name = "Jone" });
await db.SaveChangesAsync();
}
if(!db.Pizzas.Any())
{
db.Add(new Pizza() { Name = "Cheese & Pineapple" });
db.Add(new Pizza() { Name = "Vegan Cheese Tastic" });
db.Add(new Pizza() { Name = "Cheese & Pineapple", Price = 12.34m });
db.Add(new Pizza() { Name = "Vegan Cheese Tastic", Price = 23.45m });
db.Add(new Pizza() { Name = "Pepperoni", Price = 34.56m});
await db.SaveChangesAsync();

}

//order data
if(1==1)
if (!db.Toppings.Any())
{

db.Add(new Topping() { Name = "Extra Cheese", Price = 2.50m });
db.Add(new Topping() { Name = "Olives", Price = 1.75m });
db.Add(new Topping() { Name = "Bacon", Price = 3.00m });
await db.SaveChangesAsync();
}
if (!db.Orders.Any())
{
db.Add(new Order() { CustomerId = 1, PizzaId = 2 });
db.Add(new Order() { CustomerId = 2, PizzaId = 1 });
db.Add(new Order() { CustomerId = 3, PizzaId = 3 });
await db.SaveChangesAsync();
}
}
Expand Down
15 changes: 0 additions & 15 deletions exercise.pizzashopapi/EndPoints/PizzaShopApi.cs

This file was deleted.

Loading