-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
82 lines (64 loc) · 2.5 KB
/
Program.cs
File metadata and controls
82 lines (64 loc) · 2.5 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
73
74
75
76
77
78
79
80
81
82
using Microsoft.EntityFrameworkCore; // For DbContextOptionsBuilder and UseInMemoryDatabase
using ProjectManagementSystem.Data; // For ApplicationDbContext
using ProjectManagementSystem.Models; // For User, Project, ProjectUser
using ProjectManagementSystem.Services; // For IProjectService, ProjectService, IProjectTaskService, ProjectTaskService, IUserService, UserService
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseInMemoryDatabase("ProjectManagementDb"));
builder.Services.AddScoped<IProjectService, ProjectService>();
builder.Services.AddScoped<IProjectTaskService, ProjectTaskService>();
builder.Services.AddScoped<IUserService, UserService>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapControllerRoute(
name: "projects",
pattern: "Projects/{action=Index}/{id?}",
defaults: new { controller = "Projects" });
app.MapControllerRoute(
name: "tasks",
pattern: "Tasks/{action=Index}/{id?}",
defaults: new { controller = "Tasks" });
app.MapControllerRoute(
name: "users",
pattern: "Users/{action=Index}/{id?}",
defaults: new { controller = "Users" });
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
var context = services.GetRequiredService<ApplicationDbContext>();
context.Database.EnsureCreated();
// Seed data
if (!context.Users.Any())
{
context.Users.AddRange(
new User { Name = "Alice", Email = "alice@example.com" },
new User { Name = "Bob", Email = "bob@example.com" }
);
await context.SaveChangesAsync();
}
if (!context.Projects.Any())
{
var project = new Project { Name = "Sample Project", Description = "Test", Status = "Open" };
context.Projects.Add(project);
await context.SaveChangesAsync();
// Add participants
var alice = await context.Users.FirstAsync(u => u.Name == "Alice");
context.ProjectUsers.Add(new ProjectUser { ProjectId = project.Id, UserId = alice.Id });
await context.SaveChangesAsync();
}
}
app.Run();