-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
116 lines (96 loc) · 3.48 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
using ASP.NET_CA.Data;
using ASP.NET_CA.Models;
using Microsoft.EntityFrameworkCore;
using System.Text.Json;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddSession();
// Add database context
builder.Services.AddDbContext<MyDbContext>(options =>
{
var conn_str = builder.Configuration.GetConnectionString("conn_str");
options.UseLazyLoadingProxies().UseSqlServer(conn_str);
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseSession();
app.MapControllerRoute(
name: "default",
pattern: "{controller}/{action}/{id?}");
// clean database
InitDB(app.Services);
// add a user and some items to the database
getMockData(app.Services);
app.MapControllerRoute(
name: "Login",
pattern: "{controller=Login}/{action=Login}");
app.MapControllerRoute(
name: "Gallery",
pattern: "{controller=Gallery}/{action=Gallery}");
app.MapControllerRoute(
name: "MyPurchase",
pattern: "{controller=MyPurchase}/{action=MyPurchase}");
app.MapControllerRoute(
name: "ViewCart",
pattern: "{controller=ViewCart}/{action=ViewCart}");
app.Run();
void InitDB(IServiceProvider serviceProvider)
{
using var scope = serviceProvider.CreateScope();
MyDbContext db = scope.ServiceProvider.GetRequiredService<MyDbContext>();
//if (!db.Database.CanConnect())
{
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
}
}
// Mock Data to be used for testing
void getMockData(IServiceProvider serviceProvider)
{
using var scope = serviceProvider.CreateScope();
MyDbContext _db = scope.ServiceProvider.GetRequiredService<MyDbContext>();
// Mock User
User user = new User { UserID = Guid.NewGuid(), UserName = "chen", UserPassword = "yu" };
// Temp user that not logged in
User unknown = new User { UserID = Guid.NewGuid(), UserName = "unknown", UserPassword = "unknown" };
_db.Users.Add(user);
_db.Users.Add(unknown);
String[,] test_data = new String[,]
{
{
".NET Charts", "99", "Brings powerful charting capabilities to your .NET application",
"dotnet_charts.png"
},
{ ".NET Paypal", "69", "Integrate your .NET apps with your paypal the easy way", "paypal.png" },
{ ".NET ML", "299", "Supercharged .NET machine learning libraries", "mlnet.png" },
{ ".NET Analytics", "299", "Performs data mining and analytics in .NET", "analytics.png" },
{ ".NET Logger", "49", "Logs and aggregates events easily in your .NET application", "lognet.png" },
{ ".NET Numerics", "199", "Powerful numerical methods for your .NET simulations", "numerics.png" }
};
for (int i = 0; i < test_data.GetLength(0); i++)
{
Guid actCode = Guid.NewGuid();
// Mock Item
Item item = new Item
{
ItemName = test_data[i, 0],
ItemPrice = Double.Parse(test_data[i, 1]),
ItemDescription = test_data[i, 2],
ItemImage = test_data[i, 3],
};
//items_test.Add(item);
_db.Items.Add(item);
}
_db.SaveChanges();
}