ScientificReport
SeedData.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Threading.Tasks;
5 using Microsoft.AspNetCore.Hosting;
6 using Microsoft.AspNetCore.Identity;
7 using Microsoft.Extensions.DependencyInjection;
12 using Microsoft.Extensions.Logging;
24 
25 namespace ScientificReport.Models
26 {
32  public class SeedData
33  {
37  public static void Initialize(IServiceProvider serviceProvider, ScientificReportDbContext context)
38  {
39  var env = serviceProvider.GetService<IHostingEnvironment>();
40 
41  // prevents usage on any non-development environment
42  if (!env.IsDevelopment()) return;
43 
44  var logger = serviceProvider.GetRequiredService<ILogger<SeedData>>();
45 
46  SeedUserRoles(serviceProvider.GetRequiredService<RoleManager<UserProfileRole>>(), logger).Wait();
47  SeedUserProfile(context, serviceProvider).Wait();
48  SeedDepartments(context, serviceProvider).Wait();
49  SeedTeacherReports(context);
50  SeedScientificWorks(context);
51  SeedConference(context);
52  SeedPublications(context);
53  SeedArticle(context);
54  SeedDepartment(context);
55  SeedScientificInternship(context);
56  SeedScientificConsultation(context);
57  SeedReportThesis(context);
58  SeedPostgraduateGuidance(context);
59  SeedPostgraduateDissertationGuidance(context);
60  SeedPatentLicenseActivity(context);
61  SeedOpposition(context);
62  SeedMembership(context);
63 
64  context.SaveChanges();
65  }
66 
67  private static async Task SeedUserProfile(ScientificReportDbContext context, IServiceProvider serviceProvider)
68  {
69  if (context.UserProfiles.Any()) return;
70 
71  var users = new[]
72  {
73  new UserProfile
74  {
75  Email = "orest@gmail.com",
76  Position = "Teacher",
77  LastName = "Гопяк",
78  UserName = "orest",
79  BirthYear = 1999,
80  FirstName = "Орест",
81  IsApproved = true,
82  IsActive = true,
83  MiddleName = "Романович",
84  PhoneNumber = "+380000000001",
85  AcademicStatus = "Бакалавр",
86  ScientificDegree = "Бакалавр",
87  YearDegreeGained = 2020,
88  GraduationYear = 2020,
89  Sex = UserProfile.SexValue.Male
90  },
91  new UserProfile
92  {
93  Email = "yura@gmail.com",
94  Position = "Teacher",
95  LastName = "Лісовський",
96  UserName = "yura",
97  BirthYear = 1999,
98  FirstName = "Юрій",
99  IsApproved = true,
100  IsActive = true,
101  MiddleName = "Юрійович",
102  PhoneNumber = "+380000000002",
103  AcademicStatus = "Бакалавр",
104  ScientificDegree = "Бакалавр",
105  YearDegreeGained = 2020,
106  GraduationYear = 2020,
107  Sex = UserProfile.SexValue.Male
108  },
109  new UserProfile
110  {
111  Email = "olena@gmail.com",
112  Position = "Teacher",
113  LastName = "Шипка",
114  UserName = "olena",
115  BirthYear = 1999,
116  FirstName = "Олена",
117  IsApproved = true,
118  IsActive = true,
119  MiddleName = "Романівна",
120  PhoneNumber = "+380000000003",
121  AcademicStatus = "Бакалавр",
122  ScientificDegree = "Бакалавр",
123  YearDegreeGained = 2020,
124  GraduationYear = 2020,
125  Sex = UserProfile.SexValue.Female
126  },
127  new UserProfile
128  {
129  Email = "roman@gmail.com",
130  Position = "Teacher",
131  LastName = "Кордюк",
132  UserName = "roman",
133  BirthYear = 1999,
134  FirstName = "Роман",
135  IsApproved = true,
136  IsActive = true,
137  MiddleName = "Ігорович",
138  PhoneNumber = "+380000000004",
139  AcademicStatus = "Бакалавр",
140  ScientificDegree = "Бакалавр",
141  YearDegreeGained = 2020,
142  GraduationYear = 2020,
143  Sex = UserProfile.SexValue.Male
144  }
145  };
146  var userManager = serviceProvider.GetRequiredService<UserManager<UserProfile>>();
147  var userService = new UserProfileService(context);
148  foreach (var user in users)
149  {
150  await userManager.CreateAsync(user, "qwerty");
151  if (user.UserName != "olena")
152  {
153  var usr = userService.Get(u => u.UserName == user.UserName);
154  await userManager.AddToRolesAsync(usr, new[] {UserProfileRole.Teacher, UserProfileRole.Administrator});
155  }
156  }
157  }
158 
159  private static async Task SeedDepartments(ScientificReportDbContext context, IServiceProvider serviceProvider)
160  {
161  if (context.Departments.Any()) return;
162 
163  var head = context.UserProfiles.First(u => u.UserName == "olena");
164  var departments = new[]
165  {
166  new Department
167  {
168  Head = head,
169  Staff = new List<UserProfile>
170  {
171  head,
172  context.UserProfiles.First(u => u.UserName == "yura"),
173  context.UserProfiles.First(u => u.UserName == "roman"),
174  context.UserProfiles.First(u => u.UserName == "orest"),
175  },
176  Title = "Програмування"
177  }
178  };
179  foreach (var department in departments)
180  {
181  context.Departments.Add(department);
182  }
183 
184  var userManager = serviceProvider.GetRequiredService<UserManager<UserProfile>>();
185  await userManager.AddToRolesAsync(head, new[] {UserProfileRole.Teacher, UserProfileRole.HeadOfDepartment});
186 
187  head.Position = UserProfileRole.HeadOfDepartment;
188 
189  await context.SaveChangesAsync();
190  }
191 
192  private static void SeedTeacherReports(ScientificReportDbContext context)
193  {
194  if (context.TeacherReports.Any()) return;
195  var teacher = context.UserProfiles.First();
196 
197  context.TeacherReports.AddRange(
198  new TeacherReport
199  {
200  Teacher = teacher
201  }
202  );
203  context.SaveChanges();
204  }
205 
206  private static void SeedConference(ScientificReportDbContext context)
207  {
208  if (context.Conferences.Any()) return;
209 
210  var conferenceService = new ConferenceService(context);
211 
212  conferenceService.CreateItem(
213  new Conference
214  {
215  Topic = "Best topic ever",
216  Type = Conference.Types.Local,
217  Date = DateTime.Now
218  }
219  );
220  }
221 
222  private static void SeedArticle(ScientificReportDbContext context)
223  {
224  if (context.Articles.Any()) return;
225 
226  var articleService = new ArticleService(context);
227 
228  articleService.CreateItem(
229  new Article
230  {
231  Title = "my first Article",
232  ArticleType = Article.ArticleTypes.ImpactFactor,
233  PublishingPlace = "LNU",
234  PublishingHouseName = "Lnu oreo",
235  PublishingYear = 2019,
236  PagesAmount = 250,
237  IsPrintCanceled = true,
238  IsRecommendedToPrint = false,
239  LiabilityInfo = "some txt",
240  DocumentInfo = "doc txt"
241  }
242  );
243 
244  articleService.AddAuthor(
245  articleService.Get(a => a.Title == "my first Article"),
246  context.Users.First(u => u.UserName == "olena")
247  );
248  }
249 
250  private static void SeedPublications(ScientificReportDbContext context)
251  {
252  if (context.Publications.Any())
253  {
254  return;
255  }
256 
257  var publicationsService = new PublicationService(context);
258 
259  if (context.UserProfiles.First() == null)
260  {
261  return;
262  }
263 
264  publicationsService.CreateItem(new Publication
265  {
266  PublicationType = Publication.PublicationTypes.Monograph,
267  Title = "my first publication",
268  PublishingPlace = "my first publishing place",
269  Specification = "some first specification",
270  PublishingHouseName = "new oreo",
271  PublishingYear = 1999,
272  PagesAmount = 200,
273  PrintStatus = Publication.PrintStatuses.IsPrintCanceled
274  }
275  );
276  publicationsService.AddAuthor(
277  publicationsService.Get(a => a.Title == "my first publication"),
278  context.Users.First(u => u.UserName == "olena")
279  );
280  publicationsService.AddAuthor(
281  publicationsService.Get(a => a.Title == "my first publication"),
282  context.Users.First(u => u.UserName == "yura")
283  );
284 
285  publicationsService.CreateItem(new Publication
286  {
287  PublicationType = Publication.PublicationTypes.TextBook,
288  Title = "my second publication",
289  PublishingPlace = "my second publishing place",
290  Specification = "some second specification",
291  PublishingHouseName = "new oreo",
292  PublishingYear = 2019,
293  PagesAmount = 300,
294  PrintStatus = Publication.PrintStatuses.IsRecommendedToPrint
295  }
296  );
297 
298  publicationsService.AddAuthor(
299  publicationsService.Get(a => a.Title == "my second publication"),
300  context.Users.First(u => u.UserName == "olena")
301  );
302 
303  publicationsService.AddAuthor(
304  publicationsService.Get(a => a.Title == "my second publication"),
305  context.Users.First(u => u.UserName == "yura")
306  );
307  publicationsService.AddAuthor(
308  publicationsService.Get(a => a.Title == "my second publication"),
309  context.Users.First(u => u.UserName == "roman")
310  );
311  publicationsService.CreateItem(new Publication
312  {
313  PublicationType = Publication.PublicationTypes.Comment,
314  Title = "My comment",
315  PublishingPlace = "Dnipro",
316  Specification = "scientific",
317  PublishingHouseName = "first publish house name",
318  PublishingYear = 2015,
319  PagesAmount = 300,
320  PrintStatus = Publication.PrintStatuses.IsRecommendedToPrint
321  }
322  );
323  publicationsService.CreateItem(new Publication
324  {
325  PublicationType = Publication.PublicationTypes.HandBook,
326  Title = "My HandBook",
327  PublishingPlace = "Lviv",
328  Specification = "scientific",
329  PublishingHouseName = "n-th publish name",
330  PublishingYear = 2016,
331  PagesAmount = 1500,
332  PrintStatus = Publication.PrintStatuses.IsRecommendedToPrint
333  }
334  );
335  publicationsService.CreateItem(new Publication
336  {
337  PublicationType = Publication.PublicationTypes.BibliographicIndex,
338  Title = "My BibliographicIndex",
339  PublishingPlace = "Ukraine",
340  Specification = "bibliya",
341  PublishingHouseName = "church",
342  PublishingYear = 1,
343  PagesAmount = 2000,
344  PrintStatus = Publication.PrintStatuses.IsRecommendedToPrint
345  }
346  );
347 
348  }
349 
350  private static void SeedDepartment(ScientificReportDbContext context)
351  {
352  if (context.Departments.Any()) return;
353 
354  var departmentService = new DepartmentService(context);
355 
356  if(context.ScientificWorks.Any()) return;
357 
358  var scientificWorkService = new ScientificWorkService(context);
359  scientificWorkService.CreateItem(
360  new ScientificWork
361  {
362  Cypher = "123",
363  Title = "Test SW",
364  Category = "test",
365  Contents = "blabla"
366  }
367  );
368 
369  departmentService.CreateItem(
370  new Department
371  {
372  Title = "Programming"
373  });
374 
375  var scientificWork = scientificWorkService.GetAll().First();
376  departmentService.AddScientificWork(departmentService.GetAll().First().Id, scientificWork);
377 
378  if (!context.UserProfiles.Any()) return;
379  var departmentUser = context.UserProfiles.First();
380  departmentService.AddUser(departmentUser.Id, departmentUser);
381 
382  }
383 
384  private static void SeedScientificInternship(ScientificReportDbContext context)
385  {
386  if (context.ScientificInternships.Any()) return;
387 
388  var scientificInternshipService = new ScientificInternshipService(context);
389 
390  scientificInternshipService.CreateItem(new ScientificInternshipModel(new ScientificInternship
391  {
392  PlaceOfInternship = "America",
393  Started = DateTime.Today,
394  Ended = DateTime.Now,
395  Contents = "Good Job"
396  }));
397  scientificInternshipService.AddUser(
398  context.ScientificInternships.FirstOrDefault(m => m.PlaceOfInternship == "PlaceOfInternship"),
399  context.UserProfiles.FirstOrDefault(u => u.UserName == "yura"));
400  }
401 
402  private static void SeedScientificConsultation(ScientificReportDbContext context)
403  {
404  if (context.ScientificConsultations.Any()) return;
405 
406  var scientificConsultationsService = new ScientificConsultationService(context);
407 
408  scientificConsultationsService.CreateItem(new ScientificConsultationModel(new ScientificConsultation
409  {
410  CandidateName = "Igor",
411  DissertationTitle = "Work in Africa",
412  Guide = context.UserProfiles.First(u => u.UserName == "orest")
413  }));
414  scientificConsultationsService.CreateItem(new ScientificConsultationModel(new ScientificConsultation
415  {
416  CandidateName = "Yura",
417  DissertationTitle = "Work in Canada",
418  Guide = context.UserProfiles.First(u => u.UserName == "roman")
419  }));
420  }
421 
422  private static void SeedReportThesis(ScientificReportDbContext context)
423  {
424  if (context.ReportTheses.Any()) return;
425 
426  var reportThesesService = new ReportThesisService(context);
427  var conference = context.Conferences.First();
428 
429  reportThesesService.CreateItem(new ReportThesisModel(new ReportThesis
430  {
431  Thesis = "My first thesis",
432  Conference = conference
433  }));
434 
435  var reportThesis = reportThesesService.GetAll().First();
436  var author = context.UserProfiles.First();
437 
438  reportThesesService.AddAuthor(reportThesis.Id, author.Id);
439  }
440 
441  private static void SeedPostgraduateGuidance(ScientificReportDbContext context)
442  {
443  if (context.PostgraduateGuidances.Any()) return;
444 
445  var postgraduateGuidanceService = new PostgraduateGuidanceService(context);
446 
447  postgraduateGuidanceService.CreateItem(new PostgraduateGuidanceModel(new PostgraduateGuidance
448  {
449  PostgraduateName = "Bogdan Ivanovych",
450  PostgraduateInfo = "now is working",
451  Guide = context.UserProfiles.First(u => u.UserName == "orest")
452  }));
453  }
454 
455  private static void SeedPostgraduateDissertationGuidance(ScientificReportDbContext context)
456  {
457  if (context.PostgraduateDissertationGuidances.Any()) return;
458 
459  var postgraduateDissertationGuidanceService = new PostgraduateDissertationGuidanceService(context);
460 
461  postgraduateDissertationGuidanceService.CreateItem(new PostgraduateDissertationGuidanceModel(new PostgraduateDissertationGuidance
462  {
463  PostgraduateName = "Orest Romanovych",
464  Dissertation = "big",
465  Speciality = "math",
466  DateDegreeGained = DateTime.Today,
467  GraduationYear = 2012,
468  Guide = context.UserProfiles.First(u => u.UserName == "yura")
469  }));
470  }
471 
472  private static void SeedPatentLicenseActivity(ScientificReportDbContext context)
473  {
474  if (context.PatentLicenseActivities.Any()) return;
475 
476  var patentLicenseActivityService = new PatentLicenseActivityService(context);
477 
478  patentLicenseActivityService.CreateItem(new PatentLicenseActivityModel(new PatentLicenseActivity
479  {
480  Name = "High",
481  Number = 2,
482  Date = DateTime.Now,
483  Type = PatentLicenseActivity.Types.Application
484  }));
485 
486  patentLicenseActivityService.CreateItem(new PatentLicenseActivityModel(new PatentLicenseActivity
487  {
488  Name = "Medium",
489  Number = 4,
490  Date = DateTime.Today,
491  Type = PatentLicenseActivity.Types.Patent
492  }));
493  }
494 
495  private static void SeedOpposition(ScientificReportDbContext context)
496  {
497  if (context.Oppositions.Any()) return;
498 
499  var oppositionsService = new OppositionService(context);
500 
501  oppositionsService.CreateItem(
503  {
504  About = "Nice opposition",
505  DateOfOpposition = DateTime.Now,
506  Opponent = context.UserProfiles.First(u => u.UserName == "yura")
507  })
508  );
509 
510  oppositionsService.CreateItem(
512  {
513  About = "Bad opposition",
514  DateOfOpposition = DateTime.Today,
515  Opponent = context.UserProfiles.First(u => u.UserName == "olena")
516  })
517  );
518  }
519 
520  private static void SeedMembership(ScientificReportDbContext context)
521  {
522  if(context.Memberships.Any()) return;
523 
524  var membershipService = new MembershipService(context);
525 
526  membershipService.CreateItem(new MembershipModel(new Membership
527  {
528  Type = Membership.Types.ScientificCouncil,
529  MembershipInfo = "good helper",
530  User = context.UserProfiles.First(u => u.UserName == "yura")
531  }));
532  membershipService.CreateItem(new MembershipModel(new Membership
533  {
534  Type = Membership.Types.ExpertCouncil,
535  MembershipInfo = "best helper",
536  User = context.UserProfiles.First(u => u.UserName == "orest")
537  }));
538  membershipService.CreateItem(new MembershipModel(new Membership
539  {
540  Type = Membership.Types.EditorialBoard,
541  MembershipInfo = "normal guy",
542  User = context.UserProfiles.First(u => u.UserName == "yura")
543  }));
544  }
545 
546  private static void SeedScientificWorks(ScientificReportDbContext context)
547  {
548  if (context.ScientificWorks.Any()) return;
549 
550  var scientificWorkService = new ScientificWorkService(context);
551  scientificWorkService.CreateItem(
552  new ScientificWork
553  {
554  Cypher = "123",
555  Title = "Test SW",
556  Category = "test",
557  Contents = "blabla"
558  }
559  );
560  var scientificWork = scientificWorkService.GetAll().First();
561  var author = context.UserProfiles.First();
562 
563  scientificWorkService.AddAuthor(scientificWork.Id, author.Id);
564  }
565 
566  private static async Task SeedUserRoles(RoleManager<UserProfileRole> roleManager, ILogger logger)
567  {
568  foreach (var roleName in UserProfileRole.Roles)
569  {
570  if (await roleManager.RoleExistsAsync(roleName)) continue;
571  var taskResult = await roleManager.CreateAsync(new UserProfileRole(roleName));
572  if (!taskResult.Succeeded)
573  {
574  logger.LogWarning("Could not create role: " + roleName);
575  }
576  }
577  }
578  }
579 }
static void Initialize(IServiceProvider serviceProvider, ScientificReportDbContext context)
Initializes the basic data, the entrypoint for all seeds
Definition: SeedData.cs:37
override async Task< int > SaveChangesAsync(CancellationToken cancellationToken=default(CancellationToken))
virtual DbSet< PostgraduateDissertationGuidance > PostgraduateDissertationGuidances
virtual DbSet< PatentLicenseActivity > PatentLicenseActivities
This class is created to seed the basic data to be used in both frontend and backend and it is for de...
Definition: SeedData.cs:32
virtual DbSet< ScientificConsultation > ScientificConsultations