-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathCatalogDbInitializer.cs
115 lines (91 loc) · 6.04 KB
/
CatalogDbInitializer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System.Diagnostics;
using Microsoft.EntityFrameworkCore;
using AspireShop.CatalogDb;
namespace AspireShop.CatalogDbManager;
internal class CatalogDbInitializer(IServiceProvider serviceProvider, ILogger<CatalogDbInitializer> logger)
: BackgroundService
{
public const string ActivitySourceName = "Migrations";
private readonly ActivitySource _activitySource = new(ActivitySourceName);
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
{
using var scope = serviceProvider.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<CatalogDbContext>();
using var activity = _activitySource.StartActivity("Initializing catalog database", ActivityKind.Client);
await InitializeDatabaseAsync(dbContext, cancellationToken);
}
public async Task InitializeDatabaseAsync(CatalogDbContext dbContext, CancellationToken cancellationToken = default)
{
var sw = Stopwatch.StartNew();
var strategy = dbContext.Database.CreateExecutionStrategy();
await strategy.ExecuteAsync(dbContext.Database.MigrateAsync, cancellationToken);
await SeedAsync(dbContext, cancellationToken);
logger.LogInformation("Database initialization completed after {ElapsedMilliseconds}ms", sw.ElapsedMilliseconds);
}
private async Task SeedAsync(CatalogDbContext dbContext, CancellationToken cancellationToken)
{
logger.LogInformation("Seeding database");
static List<CatalogBrand> GetPreconfiguredCatalogBrands()
{
return [
new() { Brand = "Azure" },
new() { Brand = ".NET" },
new() { Brand = "Visual Studio" },
new() { Brand = "SQL Server" },
new() { Brand = "Other" }
];
}
static List<CatalogType> GetPreconfiguredCatalogTypes()
{
return [
new() { Type = "Mug" },
new() { Type = "T-Shirt" },
new() { Type = "Sheet" },
new() { Type = "USB Memory Stick" }
];
}
static List<CatalogItem> GetPreconfiguredItems(DbSet<CatalogBrand> catalogBrands, DbSet<CatalogType> catalogTypes)
{
var dotNet = catalogBrands.First(b => b.Brand == ".NET");
var other = catalogBrands.First(b => b.Brand == "Other");
var mug = catalogTypes.First(c => c.Type == "Mug");
var tshirt = catalogTypes.First(c => c.Type == "T-Shirt");
var sheet = catalogTypes.First(c => c.Type == "Sheet");
return [
new() { CatalogType = tshirt, CatalogBrand = dotNet, AvailableStock = 100, Description = ".NET Bot Black Hoodie", Name = ".NET Bot Black Hoodie", Price = 19.5M, PictureFileName = "1.png" },
new() { CatalogType = mug, CatalogBrand = dotNet, AvailableStock = 100, Description = ".NET Black & White Mug", Name = ".NET Black & White Mug", Price= 8.50M, PictureFileName = "2.png" },
new() { CatalogType = tshirt, CatalogBrand = other, AvailableStock = 100, Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureFileName = "3.png" },
new() { CatalogType = tshirt, CatalogBrand = dotNet, AvailableStock = 100, Description = ".NET Foundation T-shirt", Name = ".NET Foundation T-shirt", Price = 12, PictureFileName = "4.png" },
new() { CatalogType = sheet, CatalogBrand = other, AvailableStock = 100, Description = "Roslyn Red Sheet", Name = "Roslyn Red Sheet", Price = 8.5M, PictureFileName = "5.png" },
new() { CatalogType = tshirt, CatalogBrand = dotNet, AvailableStock = 100, Description = ".NET Blue Hoodie", Name = ".NET Blue Hoodie", Price = 12, PictureFileName = "6.png" },
new() { CatalogType = tshirt, CatalogBrand = other, AvailableStock = 100, Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureFileName = "7.png" },
new() { CatalogType = tshirt, CatalogBrand = other, AvailableStock = 100, Description = "Kudu Purple Hoodie", Name = "Kudu Purple Hoodie", Price = 8.5M, PictureFileName = "8.png" },
new() { CatalogType = mug, CatalogBrand = other, AvailableStock = 100, Description = "Cup<T> White Mug", Name = "Cup<T> White Mug", Price = 12, PictureFileName = "9.png" },
new() { CatalogType = sheet, CatalogBrand = dotNet, AvailableStock = 100, Description = ".NET Foundation Sheet", Name = ".NET Foundation Sheet", Price = 12, PictureFileName = "10.png" },
new() { CatalogType = sheet, CatalogBrand = dotNet, AvailableStock = 100, Description = "Cup<T> Sheet", Name = "Cup<T> Sheet", Price = 8.5M, PictureFileName = "11.png" },
new() { CatalogType = tshirt, CatalogBrand = other, AvailableStock = 100, Description = "Prism White TShirt", Name = "Prism White TShirt", Price = 12, PictureFileName = "12.png" }
];
}
if (!dbContext.CatalogBrands.Any())
{
var brands = GetPreconfiguredCatalogBrands();
await dbContext.CatalogBrands.AddRangeAsync(brands, cancellationToken);
logger.LogInformation("Seeding {CatalogBrandCount} catalog brands", brands.Count);
await dbContext.SaveChangesAsync(cancellationToken);
}
if (!dbContext.CatalogTypes.Any())
{
var types = GetPreconfiguredCatalogTypes();
await dbContext.CatalogTypes.AddRangeAsync(types, cancellationToken);
logger.LogInformation("Seeding {CatalogTypeCount} catalog item types", types.Count);
await dbContext.SaveChangesAsync(cancellationToken);
}
if (!dbContext.CatalogItems.Any())
{
var items = GetPreconfiguredItems(dbContext.CatalogBrands, dbContext.CatalogTypes);
await dbContext.CatalogItems.AddRangeAsync(items, cancellationToken);
logger.LogInformation("Seeding {CatalogItemCount} catalog items", items.Count);
await dbContext.SaveChangesAsync(cancellationToken);
}
}
}