using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Application.Domain.Entities; using Infrastructure.Data; using Infrastructure.Domain; using Infrastructure.Email; using Infrastructure.Extensions; using Infrastructure.Sms; using Infrastructure.Web; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; namespace StudyCenter { public class Startup : BaseStartup { public Startup(IConfiguration configuration, IHostingEnvironment env) : base(configuration, env) { } public override void ConfigureServices(IServiceCollection services) { services.AddTransient(); services.AddTransient(); services.AddSignalR(o => o.EnableDetailedErrors = true); base.ConfigureServices(services); } public override Task ValidatePrincipal(CookieValidatePrincipalContext arg) { return Task.Run(() => { var userRepo = arg.HttpContext.RequestServices.GetService>(); var userName = arg.Principal.Identity.Name; var userPermissions = userRepo.ReadOnlyTable().Where(o => o.UserName == userName) .SelectMany(o => o.UserRoles) .Select(o => o.Role) .SelectMany(o => o.RolePermissions) .Select(o => o.Permission.Number) .ToList(); var currentPermissions = arg.Principal.Claims.Where(o => o.Type == "Role").Select(o => o.Value).ToList(); if (!currentPermissions.SequenceEqual(userPermissions)) { arg.HttpContext.SignOutAsync(); arg.HttpContext.SignIn(userName, userPermissions, arg.Properties.IsPersistent); } }); } public override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity().HasOne(o => o.Parent).WithMany(o => o.Children).HasForeignKey(o => o.ParentId); modelBuilder.Entity().HasOne(o => o.Category).WithMany(o => o.Permissions).HasForeignKey(o => o.CategoryId); modelBuilder.Entity().HasOne(o => o.User).WithMany(o => o.UserRoles).HasForeignKey(o => o.UserId); modelBuilder.Entity().HasOne(o => o.Role).WithMany(o => o.UserRoles).HasForeignKey(o => o.RoleId); modelBuilder.Entity().HasOne(o => o.Role).WithMany(o => o.RolePermissions).HasForeignKey(o => o.RoleId); modelBuilder.Entity().HasOne(o => o.Permission).WithMany(o => o.RolePermissions).HasForeignKey(o => o.PermissionId); modelBuilder.Entity().HasIndex(o => o.UserName).IsUnique(); modelBuilder.Entity().HasIndex(o => o.Name).IsUnique(); modelBuilder.Entity().HasIndex(o => o.Number).IsUnique(); modelBuilder.Entity().HasIndex(o => o.Number).IsUnique(); modelBuilder.Entity().HasIndex(o => new { o.UserId, o.RoleId }).IsUnique(); modelBuilder.Entity().HasIndex(o => new { o.RoleId, o.PermissionId }).IsUnique(); //学校课程 //专业分类用来组织学校、学院、系等 modelBuilder.Entity().HasOne(o => o.Parent).WithMany(o => o.Children).HasForeignKey(o => o.ParentId); modelBuilder.Entity().HasIndex(o => new { o.ParentId, o.Number }).IsUnique(); //专业用来组织不同学制,如3年专科、4年本科 modelBuilder.Entity().HasOne(o => o.Category).WithMany(o => o.Majors).HasForeignKey(o => o.CategoryId); //每批次入学的学生,按照学制、就业方向等制定不同的教学计划 modelBuilder.Entity().HasOne(o => o.Major).WithMany(o => o.Plans).HasForeignKey(o => o.MajorId); //每个教学计划包含不同个学期和就业方向 modelBuilder.Entity().HasOne(o => o.Plan).WithMany(o => o.Semesters).HasForeignKey(o => o.PlanId); //每个学期包含多个学期课程 modelBuilder.Entity().HasOne(o => o.Semester).WithMany(o => o.Courses).HasForeignKey(o => o.SemesterId); //一个课程期对应多个教学计划学期中的课程,这样将学校的学期课程和课程的期统一起来 modelBuilder.Entity().HasOne(o => o.Term).WithMany(o => o.SemesterCourses).HasForeignKey(o => o.TermId); //课程分类 modelBuilder.Entity().HasOne(o => o.Parent).WithMany(o => o.Children).HasForeignKey(o => o.ParentId); modelBuilder.Entity().HasIndex(o => new { o.ParentId, o.Number }).IsUnique(); //课程分类 modelBuilder.Entity().HasOne(o => o.Parent).WithMany(o => o.Children).HasForeignKey(o => o.ParentId); modelBuilder.Entity().HasIndex(o => new { o.ParentId, o.Number }).IsUnique(); modelBuilder.Entity().HasOne(o => o.BookCategory).WithMany(o => o.Books).HasForeignKey(o => o.BookCategoryId); modelBuilder.Entity
().HasOne(o => o.Parent).WithMany(o => o.Children).HasForeignKey(o => o.ParentId); modelBuilder.Entity
().HasIndex(o => new { o.ParentId, o.Number }).IsUnique(); //每本书有多个章节 modelBuilder.Entity
().HasOne(o => o.Book).WithMany(o => o.Sections).HasForeignKey(o => o.BookId); //课程 modelBuilder.Entity().HasOne(o => o.Category).WithMany(o => o.Courses).HasForeignKey(o => o.CategoryId); //每个课程有多个期 modelBuilder.Entity().HasOne(o => o.Course).WithMany(o => o.Terms).HasForeignKey(o => o.CourseId); modelBuilder.Entity().HasOne(o => o.Book).WithMany(o => o.Terms).HasForeignKey(o => o.BookId); //每期有多个课时 modelBuilder.Entity().HasOne(o => o.Term).WithMany(o => o.Lessons).HasForeignKey(o => o.TermId); //课时和资源多对多多对多 modelBuilder.Entity().HasOne(o => o.Lesson).WithMany(o => o.LessonResources).HasForeignKey(o => o.LessionId); modelBuilder.Entity().HasOne(o => o.Resource).WithMany(o => o.LessonResources).HasForeignKey(o => o.ResourceId); modelBuilder.Entity().HasIndex(o => new { o.LessionId, o.ResourceId }).IsUnique(); //学生和课程的期多对多 modelBuilder.Entity().HasOne(o => o.Term).WithMany(o => o.TermUsers).HasForeignKey(o => o.TermId); modelBuilder.Entity().HasOne(o => o.User).WithMany(o => o.TermUsers).HasForeignKey(o => o.UserId); modelBuilder.Entity().HasIndex(o => new { o.TermId, o.UserId }).IsUnique(); modelBuilder.Entity().HasOne(o => o.Parent).WithMany(o => o.Children).HasForeignKey(o => o.ParentId); modelBuilder.Entity().HasOne(o => o.User).WithMany(o => o.Resources).HasForeignKey(o => o.UserId); modelBuilder.Entity().HasOne(o => o.Category).WithMany(o => o.Resources).HasForeignKey(o => o.CategoryId); modelBuilder.Entity().HasOne(o => o.Course).WithMany(o => o.Resources).HasForeignKey(o => o.CourseId); modelBuilder.Entity().HasOne(o => o.Section).WithMany(o => o.Resources).HasForeignKey(o => o.SectionId); modelBuilder.Entity().Property(o => o.Name).IsRequired().HasMaxLength(255); //习题 modelBuilder.Entity().HasOne(o => o.User).WithMany(o => o.Questions).HasForeignKey(o => o.UserId); modelBuilder.Entity().HasOne(o => o.Course).WithMany(o => o.Questions).HasForeignKey(o => o.CourseId); modelBuilder.Entity().HasOne(o => o.Section).WithMany(o => o.Questions).HasForeignKey(o => o.SectionId); //习题答题记录 modelBuilder.Entity().HasOne(o => o.User).WithMany(o => o.UserQuestions).HasForeignKey(o => o.UserId); modelBuilder.Entity().HasOne(o => o.Question).WithMany(o => o.UserQuestions).HasForeignKey(o => o.QuestionId); modelBuilder.Entity().HasIndex(o => new { o.UserId, o.QuestionId }).IsUnique(); //试题答题记录 modelBuilder.Entity().HasOne(o => o.User).WithMany(o => o.UserPaperQuestions).HasForeignKey(o => o.UserId); modelBuilder.Entity().HasOne(o => o.PaperQuestion).WithMany(o => o.UserPaperQuestions).HasForeignKey(o => o.PaperQuestionId); modelBuilder.Entity().HasIndex(o => new { o.UserId, o.PaperQuestionId }).IsUnique(); //试卷答题记录 modelBuilder.Entity().HasOne(o => o.User).WithMany(o => o.UserPapers).HasForeignKey(o => o.UserId); modelBuilder.Entity().HasOne(o => o.Paper).WithMany(o => o.UserPapers).HasForeignKey(o => o.PaperId); modelBuilder.Entity().HasIndex(o => new { o.UserId, o.PaperId }).IsUnique(); //考题是具有习题的属性和分值等其他和试卷相关的属性 modelBuilder.Entity().HasOne(o => o.Paper).WithMany(o => o.Questions).HasForeignKey(o => o.PaperId); } public override void Seed(DbContext dbContext, IServiceProvider serviceProvider, IConfiguration configuration) { dbContext.Set().Add(new PermissionCategory { Name = "配置", Number = "Configuration", Permissions = new List { new Permission { Name = "查询配置", Number = "ListConfiguration",DisplayOrder =1 }, new Permission { Name = "修改配置", Number = "EditConfiguration",DisplayOrder =2 } } }); int i = 1; var skipReadCollection = new string[] { "Permission" }; var skipAddCollection = new string[] { "Permission", "Setting", "Node", "Device", "Data", "Api", "Parameter" }; foreach (var item in dbContext.Model.GetEntityTypes()) { var type = item.ClrType; var name = type.GetDisplayName(); var number = type.Name; var category = new PermissionCategory { Name = name, Number = type.Name, DisplayOrder = i }; category.Permissions.Add(new Permission { Name = $"查询{name}", Number = $"List{number}", DisplayOrder = 10 * i + 1 }); if (!skipReadCollection.Contains(type.Name)) { category.Permissions.Add(new Permission { Name = $"查看{name}", Number = $"Read{number}", DisplayOrder = 10 * i + 2 }); } if (!skipAddCollection.Contains(type.Name)) { category.Permissions.Add(new Permission { Name = $"添加{name}", Number = $"Add{number}", DisplayOrder = 10 * i + 3 }); } if (!typeof(IDisableUpdate).IsAssignableFrom(type)) { category.Permissions.Add(new Permission { Name = $"修改{name}", Number = $"Edit{number}", DisplayOrder = 10 * i + 4 }); } if (!typeof(IDisableDelete).IsAssignableFrom(type)) { category.Permissions.Add(new Permission { Name = $"删除{name}", Number = $"Delete{number}", DisplayOrder = 10 * i + 5 }); } dbContext.Set().Add(category); i += 1; } dbContext.SaveChanges(); var adminRole = new Role { Name = "管理员", IsReadOnly = true }; foreach (var item in dbContext.Set()) { adminRole.RolePermissions.Add(new RolePermission { Permission = item, IsReadOnly = true }); } var user1Id = dbContext.Set().Add(new User { UserName = "admin", UserRoles = new List { new UserRole { Role = adminRole } } }).Entity.Id; dbContext.SaveChanges(); //课程分类 dbContext.Set().Add(new CourseCategory { Name = "高职高专", Number = "2", DisplayOrder = 2 }); dbContext.Set().Add(new CourseCategory { Name = "大学大专", Number = "3", DisplayOrder = 3 }); dbContext.Set().Add(new CourseCategory { Name = "大学", Number = "4", DisplayOrder = 4 }); dbContext.Set().Add(new CourseCategory { Name = "研究生", Number = "5", DisplayOrder = 5 }); dbContext.Set().Add(new CourseCategory { Name = "成人教育", Number = "6", DisplayOrder = 6 }); dbContext.Set().Add(new CourseCategory { Name = "职业培训", Number = "7", DisplayOrder = 7 }); dbContext.SaveChanges(); //教学计划 var major1 = dbContext.Set().Add(new MajorCategory { Name = "计算机学院", Number = "0", Majors = new List { new Major{ Name="计算机科学与技术", Plans = new List { new TeachingPlan { Start=DateTime.Now.Date, Years=4, Degree="学士", Career="软件工程方向", Semesters = new List { new Semester { Name="第1学期", Courses = new List { new SemesterCourse { Name="计算机应用", Number ="106090100", Category="学科基础课", Type="必修课", Score=4 } } } } } } } } }); dbContext.SaveChanges(); //资源类型初始化 var resourceTypeId1 = dbContext.Set().Add(new ResourceCategory { Name = "文档", Number = "01", DisplayOrder = 01 }).Entity.Id; dbContext.Set().Add(new ResourceCategory { Name = "音频", Number = "01", DisplayOrder = 01 }); dbContext.Set().Add(new ResourceCategory { Name = "视频", Number = "01", DisplayOrder = 01 }); dbContext.Set().Add(new ResourceCategory { Name = "动画", Number = "01", DisplayOrder = 01 }); dbContext.Set().Add(new ResourceCategory { Name = "虚拟仿真", Number = "01", DisplayOrder = 01 }); //资源分类初始化 dbContext.SaveChanges(); dbContext.Set().Add(new CourseCategory { Name = "中职中专", Number = "1", DisplayOrder = 1, Children = new List { new CourseCategory { Name="公开课", Number="1", DisplayOrder=1 }, new CourseCategory { Name="专业课", Number="2", DisplayOrder=2, Children=new List { new CourseCategory { Name = "信息技术类", Number = "10609", DisplayOrder = 106, Children = new List { new CourseCategory { Name="计算机应用", Number="106090100", DisplayOrder=106010100, Courses = new List { new Course { Name="Java基础", Number="10001", Image="/upload/1.jpg", Description="本课程是以Java语言来讲授程序设计的入门知识,而非具体教授Java语言,关于Java语言的全面知识,还需要学习本课程的后续课程——“Java语言程序设计进阶”。", Terms = new List { new Term { Name="2018-2019学年下学期", Start= new DateTime(2018,9,1), End=new DateTime(2019,1,1), Lessons=new List { new Lesson { Name="导论", DisplayOrder=1 }, new Lesson { Name="基础", DisplayOrder=2 } } } } } } } } } } } } }); dbContext.SaveChanges(); dbContext.Set().Add(new BookCategory { Name = "理科教材", Number = "0000", Books = new List { new Book { Name="计算机应用基础", Number="ISBN 0000", Sections = new List
{ new Section { Name="导论上", Number="1.1" }, new Section { Name="导论下", Number="1.2" } } } } }); dbContext.SaveChanges(); dbContext.Set().Add(new Resource { CategoryId = dbContext.Set().FirstOrDefault(o => o.Name == "视频").Id, Name = "导论", File = "/upload/1.mp4", UserId = user1Id, CourseId = dbContext.Set().FirstOrDefault(o => o.Name == "Java基础").Id, SectionId = dbContext.Set
().FirstOrDefault(o => o.Name == "导论上").Id, }); dbContext.SaveChanges(); dbContext.Set().Add(new LessonResource { Name = "视频", LessionId = dbContext.Set().FirstOrDefault(o => o.Name == "导论").Id, ResourceId = dbContext.Set().FirstOrDefault(o => o.Name == "导论").Id, }); dbContext.SaveChanges(); foreach (var item in dbContext.Set()) { item.UpdatePath(); } dbContext.SaveChanges(); } } }