-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSeed.cs
329 lines (284 loc) · 13.2 KB
/
Seed.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using ClassTranscribeDatabase.Models;
namespace ClassTranscribeDatabase
{
/// <summary>
/// This class "Seeds" the database with some default values when a database in newly created.
/// </summary>
public class Seeder
{
public static readonly string UNK_UNIVERSITY_ID = "0000";
private readonly CTDbContext _context;
private readonly ILogger _logger;
public Seeder(CTDbContext context, ILogger<Seeder> logger)
{
_context = context;
_logger = logger;
}
public void Seed()
{
_logger.LogInformation("Database seeder starting");
_logger.LogInformation($"Attempting connection to server {Globals.appSettings.POSTGRES_SERVER_NAME} for database {Globals.appSettings.POSTGRES_DB} using user {Globals.appSettings.ADMIN_USER_ID}");
// This is where we first use the Database, so authentication and connection issues are likely to be discovered here.
// Or maybe the database is just at restarting
//
int maxAttempts = 100;
int retrySeconds = 15;
for (int attempt = 1; attempt <= maxAttempts; attempt++)
{
try
{
if (_context.Database.CanConnect())
{
break;
}
}
catch (Exception ex)
{
_logger.LogInformation( "Database.CanConnect() exception: {0}",ex.Message);
// https://www.postgresql.org/docs/9.4/errcodes-appendix.html
string ignore = "57P03"; // "57P03: the database system is starting up";
if (! ex.Message.Contains(ignore) ){
throw; // throw implicitly to preserve stack trace
}
}
_logger.LogError($"Attempt {attempt} of {maxAttempts}. Cannot connect to Database");
if (attempt < maxAttempts)
{
_logger.LogInformation($"Sleeping for {retrySeconds} seconds");
// Thread.Sleep(1000 * retrySeconds);
Task.Delay(TimeSpan.FromSeconds(retrySeconds)).Wait();
}
else
{
_logger.LogError("Seed() Giving up on database :-( ");
throw new Exception("Could not connect to database");
}
}
_logger.LogInformation("Migration starting");
using (var localcontext = CTDbContext.CreateDbContext())
{
// Default is 30 seconds
localcontext.Database.SetCommandTimeout(60*60); // in seconds
localcontext.Database.Migrate();
}
_logger.LogInformation("Migration complete");
_logger.LogInformation("Creating roles");
IdentityRole Instructor = new IdentityRole { Name = Globals.ROLE_INSTRUCTOR, Id = "0000", NormalizedName = Globals.ROLE_INSTRUCTOR.ToUpper() };
IdentityRole Student = new IdentityRole { Name = Globals.ROLE_STUDENT, Id = "0001", NormalizedName = Globals.ROLE_STUDENT.ToUpper() };
IdentityRole Admin = new IdentityRole { Name = Globals.ROLE_ADMIN, Id = "0002", NormalizedName = Globals.ROLE_ADMIN.ToUpper() };
IdentityRole UniversityAdmin = new IdentityRole { Name = Globals.ROLE_UNIVERSITY_ADMIN, Id = "0003", NormalizedName = Globals.ROLE_UNIVERSITY_ADMIN.ToUpper() };
IdentityRole TeachingAssistant = new IdentityRole { Name = Globals.ROLE_TEACHING_ASSISTANT, Id = "0004", NormalizedName = Globals.ROLE_TEACHING_ASSISTANT.ToUpper() };
IdentityRole Advisors = new IdentityRole { Name = Globals.ROLE_ADVISORS, Id = "0005", NormalizedName = Globals.ROLE_ADVISORS.ToUpper() };
IdentityRole MediaWorker = new IdentityRole { Name = Globals.ROLE_MEDIA_WORKER, Id = "A100", NormalizedName = Globals.ROLE_MEDIA_WORKER.ToUpper() };
List<IdentityRole> roles = new List<IdentityRole> { Instructor, Student, Admin, UniversityAdmin, TeachingAssistant, Advisors, MediaWorker };
for (int i = 0; i < roles.Count(); i++)
{
if (!_context.Roles.IgnoreQueryFilters().Any(r => r.Name == roles[i].Name))
{
_context.Roles.Add(roles[i]);
}
}
University sampleUniversity = new University
{
// University Id begins with 1
Id = "1001",
Name = "ClassTranscribe Test University",
Domain = "classtranscribe.com"
// Departments = { department1, department2 }
};
University unknownUniversity = new University
{
// University Id begins with 1
Id = UNK_UNIVERSITY_ID,
Name = "Unknown",
Domain = "UNK"
};
List<University> universities = new List<University> { sampleUniversity, unknownUniversity };
foreach (var t in universities)
{
if (!_context.Universities.IgnoreQueryFilters().Contains(t))
{
_context.Universities.Add(t);
}
}
ApplicationUser testuser =
newUserObject(Globals.TEST_USER_ID, sampleUniversity.Id, "[email protected]");
ApplicationUser mediaworkeruser = newUserObject(Globals.MEDIA_WORKER_USER_ID, sampleUniversity.Id, Globals.MEDIA_WORKER_EMAIL);
// Todo/Toreview is Password necessary?
testuser.PasswordHash = new PasswordHasher<ApplicationUser>().HashPassword(testuser, testuser.Email);
if (!_context.Users.IgnoreQueryFilters().Any(u => u.Email == testuser.Email))
{
_context.Users.Add(testuser);
_context.UserRoles.Add(new IdentityUserRole<string> { RoleId = Instructor.Id, UserId = testuser.Id });
_context.UserRoles.Add(new IdentityUserRole<string> { RoleId = Admin.Id, UserId = testuser.Id });
}
if (!_context.Users.IgnoreQueryFilters().Any( u=> u.Email == mediaworkeruser.Email))
{
_context.Users.Add(mediaworkeruser);
_context.UserRoles.Add(new IdentityUserRole<string> { RoleId = MediaWorker.Id, UserId = mediaworkeruser.Id });
}
_context.SaveChanges();
Term term1 = new Term
{
// Term Id begins with 0
Id = "0001",
Name = "Test Term",
StartDate = DateTime.Now.AddMonths(-3),
EndDate = DateTime.Now,
// Offerings, University, UniversityId
};
term1.UniversityId = sampleUniversity.Id;
Department department1 = new Department
{
// department Id begins with 2
Id = "2001",
Name = "Computer Science",
Acronym = "CS",
UniversityId = sampleUniversity.Id
};
Department department2 = new Department
{
// department Id begins with 2
Id = "2002",
Name = "Electrical and Computer Engineering",
Acronym = "ECE",
UniversityId = sampleUniversity.Id
};
Course test_course = new Course
{
Id = "test_course",
CourseNumber = "000",
DepartmentId = department1.Id,
FilePath = "0101-MOCK"
};
Offering offering2 = new Offering
{
Id = "4002",
SectionName = "AB",
CourseName = "Test Course",
TermId = term1.Id,
AccessType = AccessTypes.Public
};
CourseOffering course_offering2 = new CourseOffering
{
Id = "9002",
CourseId = test_course.Id,
OfferingId = offering2.Id,
FilePath = Path.Combine(test_course.FilePath, "0102-ABCD")
};
Directory.CreateDirectory(Path.Combine(Globals.appSettings.DATA_DIRECTORY, course_offering2.FilePath));
List<Term> terms = new List<Term> { term1 };
List<Department> departments = new List<Department> { department1, department2 };
List<Course> courses = new List<Course> { test_course };
List<Offering> offerings = new List<Offering> { offering2 };
List<CourseOffering> courseOfferings = new List<CourseOffering> { course_offering2 };
foreach (var t in terms)
{
if (!_context.Terms.IgnoreQueryFilters().Contains(t))
{
_context.Terms.Add(t);
}
}
foreach (var t in departments)
{
if (!_context.Departments.IgnoreQueryFilters().Contains(t))
{
_context.Departments.Add(t);
}
}
foreach (var t in courses)
{
if (!_context.Courses.IgnoreQueryFilters().Contains(t))
{
_context.Courses.Add(t);
}
}
foreach (var t in offerings)
{
if (!_context.Offerings.IgnoreQueryFilters().Contains(t))
{
_context.Offerings.Add(t);
}
}
foreach (var t in courseOfferings)
{
if (_context.CourseOfferings.IgnoreQueryFilters().Where(u => u.OfferingId == t.OfferingId && u.CourseId == t.CourseId).Count() == 0)
{
_context.CourseOfferings.Add(t);
}
}
_context.SaveChanges();
UserOffering userOffering2 = new UserOffering
{
OfferingId = offering2.Id,
ApplicationUserId = testuser.Id,
IdentityRoleId = Instructor.Id
};
List<UserOffering> userOfferings = new List<UserOffering> { userOffering2 };
foreach (var t in userOfferings)
{
if (!_context.UserOfferings.IgnoreQueryFilters().Any(u => u.OfferingId == t.OfferingId && u.ApplicationUserId == t.ApplicationUserId && u.IdentityRoleId == t.IdentityRoleId))
{
_context.UserOfferings.Add(t);
}
}
_context.SaveChanges();
// The purpose of ClassTranscribe is to provide legal access to copyrighted materials.
// The playlist below includes two lectures by UIUC CS Professor Chengxiang Zhai.
// He has given permission by email for this content to be used by ClassTranscribe.
// "As part of our testing of ClassTranscribe we like to check that we can download and transcribe videos
// from a Youtube channel and playlist. My RA created a Youtube channel with two sample videos using
// two UIUC videos. These videos are by you - they are
// “Lecture 1 - Natural Language Content and Analysis” and
// “Lecture 2- Text Retrieval and Search Engines”
// May we have your permission to use these videos for this purpose?" - Prof. Angrave
// "Yes, sure!" - Prof. Chengxiang Zhai.
Playlist youtubePlaylist = new Playlist
{
Id = "CT_Test_Playlist",
PlaylistIdentifier = "PL9Q1c4uIAoWSc8_VV9JZSMDYdS5xzMWtU",
SourceType = SourceType.Youtube,
Name = "Youtube Sample"
};
List<Playlist> playlists = new List<Playlist> { youtubePlaylist };
foreach (var t in playlists)
{
if (!_context.Playlists.IgnoreQueryFilters().Contains(t))
{
_context.Playlists.Add(t);
}
}
youtubePlaylist.OfferingId = offering2.Id;
_context.SaveChanges();
_logger.LogInformation("Seeded");
}
private ApplicationUser newUserObject(string id, string universityId, string email)
{
string lower = email.ToLower();
string upper = email.ToUpper();
ApplicationUser user = new ApplicationUser
{
Id = id,
UserName = lower,
Email = lower,
FirstName = email.Substring(0, email.IndexOf("@")),
LastName = "Account",
UniversityId = universityId,
NormalizedEmail = upper,
NormalizedUserName = upper,
EmailConfirmed = true,
LockoutEnabled = false,
SecurityStamp = Guid.NewGuid().ToString()
};
return user;
}
}
}