-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
103 lines (82 loc) · 3.23 KB
/
Copy pathProgram.cs
File metadata and controls
103 lines (82 loc) · 3.23 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using ShepherdsPiesControllers.Data;
using ShepherdsPiesControllers.Models;
using ShepherdsPiesControllers.Repositories;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
builder.Services.AddDbContext<ShepherdsPiesDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("ShepherdsPiesDbConnectionString")));
builder.Services.AddIdentity<Employee, IdentityRole>()
.AddEntityFrameworkStores<ShepherdsPiesDbContext>()
.AddDefaultTokenProviders();
builder.Services.ConfigureApplicationCookie(options =>
{
options.Events.OnRedirectToLogin = context =>
{
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
return Task.CompletedTask;
};
options.Events.OnRedirectToAccessDenied = context =>
{
context.Response.StatusCode = StatusCodes.Status403Forbidden;
return Task.CompletedTask;
};
});
builder.Services.AddAutoMapper(cfg => { }, typeof(Program));
builder.Services.AddScoped<IOrderRepository, OrderRepository>();
builder.Services.AddScoped<IPizzaRepository, PizzaRepository>();
builder.Services.AddScoped<IEmployeeRepository, EmployeeRepository>();
builder.Services.AddScoped<ISizeRepository, SizeRepository>();
builder.Services.AddScoped<ICheeseOptionRepository, CheeseOptionRepository>();
builder.Services.AddScoped<ISauceOptionRepository, SauceOptionRepository>();
builder.Services.AddScoped<IToppingRepository, ToppingRepository>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
await SeedIdentityDataAsync(app);
app.Run();
static async Task SeedIdentityDataAsync(WebApplication app)
{
using var scope = app.Services.CreateScope();
var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<Employee>>();
foreach (var role in new[] { "Employee", "Manager" })
{
if (!await roleManager.RoleExistsAsync(role))
{
await roleManager.CreateAsync(new IdentityRole(role));
}
}
await EnsureSeedEmployeeAsync(userManager, "employee@shepherdspies.com", "Pat", "Employee", "Employee123!", "Employee");
await EnsureSeedEmployeeAsync(userManager, "manager@shepherdspies.com", "Morgan", "Manager", "Manager123!", "Manager");
}
static async Task EnsureSeedEmployeeAsync(UserManager<Employee> userManager, string email, string firstName, string lastName, string password, string role)
{
if (await userManager.FindByEmailAsync(email) is not null)
{
return;
}
var employee = new Employee
{
UserName = email,
Email = email,
FirstName = firstName,
LastName = lastName
};
var result = await userManager.CreateAsync(employee, password);
if (result.Succeeded)
{
await userManager.AddToRoleAsync(employee, role);
}
}