-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
188 lines (158 loc) · 6.72 KB
/
Program.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*// Import necessary namespaces
using System.Globalization;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.Facebook;
using Microsoft.AspNetCore.Localization;
using Microsoft.EntityFrameworkCore;
using SalesWebMvc.Data;
using SalesWebMvc.Services;
// Create a new web application
var builder = WebApplication.CreateBuilder(args);
// Configure the database context for MySQL
// Add the SalesWebMvcDbContext to the service container
// Use the connection string from the app's configuration
// Specify the MySQL server version and the assembly containing migrations
builder.Services.AddDbContext<SalesWebMvcDbContext>(options =>
options.UseMySql(builder.Configuration.GetConnectionString("SalesWebMvcDbContext"),
new MySqlServerVersion(new Version(5, 7, 44)), // Change this version to your MySQL version as necessary
builder => builder.MigrationsAssembly("SalesWebMvc")));
// Register SeedingService for dependency injection.
// Scoped lifetime services are created once per client request (connection).
builder.Services.AddScoped<SeedingService>();
// Register services for dependency injection.
builder.Services.AddScoped<SellerService>();
builder.Services.AddScoped<DepartmentService>();
builder.Services.AddScoped<SalesRecordService>();
// Add MVC services to the service container
builder.Services.AddControllersWithViews();
// Build the application
var app = builder.Build();
// Configure the request localization options for the application
// Set the default request culture to en-US
// Add en-US to the list of supported cultures and UI cultures
var enUS = new CultureInfo("en-US");
var localizationOptions = new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture(enUS),
SupportedCultures = new List<CultureInfo> { enUS },
SupportedUICultures = new List<CultureInfo> { enUS }
};
// Use the request localization options in the application
app.UseRequestLocalization(localizationOptions);
// Get an instance of SeedingService
using var scope = app.Services.CreateScope();
var seeder = scope.ServiceProvider.GetRequiredService<SeedingService>();
// Configure the HTTP request pipeline.
// If the app is not in development mode, use exception handler and HSTS
if (app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error"); // Use custom error handling page
app.UseHsts(); // Use HSTS for security
// Call the SeedDatabase method to populate the database
seeder.Seed();
}
// Use HTTPS redirection and static files
app.UseHttpsRedirection();
app.UseStaticFiles();
// Use routing and authorization
app.UseRouting();
app.UseAuthorization();
// Default route
// Map the default route
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
/*
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = FacebookDefaults.AuthenticationScheme;
})
.AddCookie()
.AddFacebook(facebookOptions =>
{
facebookOptions.AppId = "3920309334863877";
facebookOptions.AppSecret = "36dd33522ef34080523b9b04a4cae49d";
});
//
// Run the application
app.Run();
*/
// Import necessary namespaces
using System.Globalization;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.Facebook;
using Microsoft.AspNetCore.Localization;
using Microsoft.EntityFrameworkCore;
using SalesWebMvc.Data;
using SalesWebMvc.Services;
// Create a new web application
var builder = WebApplication.CreateBuilder(args);
// Configure the database context for MySQL
// Add the SalesWebMvcDbContext to the service container
// Use the connection string from the app's configuration
// Specify the MySQL server version and the assembly containing migrations
builder.Services.AddDbContext<SalesWebMvcDbContext>(options =>
options.UseMySql(builder.Configuration.GetConnectionString("SalesWebMvcDbContext"),
new MySqlServerVersion(new Version(5, 7, 44)), // Change this version to your MySQL version as necessary
builder => builder.MigrationsAssembly("SalesWebMvc")));
// Register SeedingService for dependency injection.
// Scoped lifetime services are created once per client request (connection).
builder.Services.AddScoped<SeedingService>();
// Register services for dependency injection.
builder.Services.AddScoped<SellerService>();
builder.Services.AddScoped<DepartmentService>();
builder.Services.AddScoped<SalesRecordService>();
// Add MVC services to the service container
builder.Services.AddControllersWithViews();
// Add authentication services to the service container
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = FacebookDefaults.AuthenticationScheme;
})
.AddCookie()
.AddFacebook(facebookOptions =>
{
});
// Build the application
var app = builder.Build();
// Configure the request localization options for the application
// Set the default request culture to en-US
// Add en-US to the list of supported cultures and UI cultures
var enUS = new CultureInfo("en-US");
var localizationOptions = new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture(enUS),
SupportedCultures = new List<CultureInfo> { enUS },
SupportedUICultures = new List<CultureInfo> { enUS }
};
// Use the request localization options in the application
app.UseRequestLocalization(localizationOptions);
// Get an instance of SeedingService
using var scope = app.Services.CreateScope();
var seeder = scope.ServiceProvider.GetRequiredService<SeedingService>();
// Configure the HTTP request pipeline.
// If the app is not in development mode, use exception handler and HSTS
if (app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error"); // Use custom error handling page
app.UseHsts(); // Use HSTS for security
// Call the SeedDatabase method to populate the database
seeder.Seed();
}
// Use HTTPS redirection and static files
app.UseHttpsRedirection();
app.UseStaticFiles();
// Use routing and authorization
app.UseRouting();
app.UseAuthorization();
// Default route
// Map the default route
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
// Run the application
app.Run();