-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
72 lines (62 loc) · 2.27 KB
/
Program.cs
File metadata and controls
72 lines (62 loc) · 2.27 KB
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
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// EF Core InMemory
builder.Services.AddDbContext<BookDb>(opt => opt.UseInMemoryDatabase("books"));
builder.Services.AddControllers();
// Swagger
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Seed äàííûõ
await SeedDb(app);
app.MapControllers();
// Âêëþ÷àåì Swagger
app.UseSwagger();
app.UseSwaggerUI();
app.Run();
static async Task SeedDb(WebApplication app)
{
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<BookDb>();
if (!await db.Books.AnyAsync())
{
db.Books.AddRange(
new Book
{
Id = 1,
Name = "Clean Code",
Publisher = "Prentice Hall",
RegDate = DateTime.UtcNow.AddDays(-10),
Authors = { new Author { FirstName = "Robert", MiddleName = "C.", LastName = "Martin" } }
},
new Book
{
Id = 12,
Name = "CLR via C#",
Publisher = "Microsoft Press",
RegDate = DateTime.UtcNow.AddDays(-20),
Authors = { new Author { FirstName = "Jeffrey", MiddleName = "", LastName = "Richter" } }
},
new Book
{
Id = 123,
Name = "The Pragmatic Programmer",
Publisher = "Addison-Wesley",
RegDate = DateTime.UtcNow.AddDays(-5),
Authors = { new Author { FirstName = "Andrew", MiddleName = "", LastName = "Hunt" },
new Author { FirstName = "David", MiddleName = "", LastName = "Thomas" } }
},
new Book
{
Id = 9876,
Name = "Domain-Driven Design",
Publisher = "Addison-Wesley",
RegDate = DateTime.UtcNow.AddDays(-1),
Authors = { new Author { FirstName = "Eric", MiddleName = "", LastName = "Evans" } }
}
);
await db.SaveChangesAsync();
}
}
}