in Hands-on lab/lab-files/src/src/PartsUnlimitedWebsite/Models/SampleData.cs [161:223]
public static void PopulateOrderHistory(IServiceProvider serviceProvider, IEnumerable<Product> products)
{
var random = new Random(1234);
var recomendationCombinations = new[] {
new{ Transactions = new []{1, 3, 8}, Multiplier = 60 },
new{ Transactions = new []{2, 6}, Multiplier = 10 },
new{ Transactions = new []{4, 11}, Multiplier = 20 },
new{ Transactions = new []{5, 14}, Multiplier = 10 },
new{ Transactions = new []{6, 16, 18}, Multiplier = 20 },
new{ Transactions = new []{7, 17}, Multiplier = 25 },
new{ Transactions = new []{8, 1}, Multiplier = 5 },
new{ Transactions = new []{10, 17,9}, Multiplier = 15 },
new{ Transactions = new []{11, 5}, Multiplier = 15 },
new{ Transactions = new []{12, 8}, Multiplier = 5 },
new{ Transactions = new []{13, 15}, Multiplier = 50 },
new{ Transactions = new []{14, 15}, Multiplier = 30 },
new{ Transactions = new []{16, 18}, Multiplier = 80 }
};
IConfigurationSection configuration = GetAdminRoleConfiguration(serviceProvider);
string userName = configuration[DefaultAdminNameKey];
using (var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
var db = serviceScope.ServiceProvider.GetService<PartsUnlimitedContext>();
var orders = new List<Order>();
foreach (var combination in recomendationCombinations)
{
for (int i = 0; i < combination.Multiplier; i++)
{
var order = new Order
{
Username = userName,
OrderDate = DateTime.Now,
Name = $"John Smith{random.Next()}",
Address = "15010 NE 36th St",
City = "Redmond",
State = "WA",
PostalCode = "98052",
Country = "United States",
Phone = "425-703-6214",
Email = userName
};
db.Orders.Add(order);
decimal total = 0;
foreach (var id in combination.Transactions)
{
var product = products.Single(x => x.RecommendationId == id);
var orderDetail = GetOrderDetail(product, order);
// ENHANCEMENT: Add Order Details data.
// db.OrderDetails.Add(orderDetail);
total += orderDetail.UnitPrice;
}
order.Total = total;
}
}
db.SaveChanges();
}
}