public void Update(DatabaseContext context, IConfiguration config, Lecture previous) { UpdateDirectory(context, config, previous); // reset repositories if (previous.Name != Name) { LectureContentsRepositoryPair.ResetRemoteUrl(); LectureSubmissionsRepositoryPair.ResetRemoteUrl(); } // parse teachers and students var flts = context.Users.Select(x => x.Account); var newTeachers = TeachersForEdit?.Split(' ', ',', '\t', '\n', '\r') ?? new string[0] { }; var newStudents = StudentsForEdit?.Split(' ', ',', '\t', '\n', '\r')?.Except(newTeachers) ?? new string[0] { }; var teachers = GetTeachers().Select(x => x.Account); var students = GetStudents().Select(x => x.Account); foreach (var user in context.Users) { if (newTeachers.Contains(user.Account) && !teachers.Contains(user.Account)) { var a = new LectureUser() { Lecture = this, User = user, Role = LectureUserRole.Teacher }; LectureUsers.Add(a); a.CreateNew(context, config); } if (!newTeachers.Contains(user.Account) && teachers.Contains(user.Account)) { var a = LectureUsers.Where(x => x.UserId == user.Id && x.LectureId == Id && x.Role == LectureUserRole.Teacher).FirstOrDefault(); LectureUsers.Remove(a); a.Remove(context, config); } if (newStudents.Contains(user.Account) && !students.Contains(user.Account)) { var a = new LectureUser() { Lecture = this, User = user, Role = LectureUserRole.Student }; LectureUsers.Add(a); a.CreateNew(context, config); } if (!newStudents.Contains(user.Account) && students.Contains(user.Account)) { var a = LectureUsers.Where(x => x.UserId == user.Id && x.LectureId == Id && x.Role == LectureUserRole.Student).FirstOrDefault(); LectureUsers.Remove(a); a.Remove(context, config); } } var me = GetEntityForEditOrRemove(context, config); foreach (var x in me.LectureUsers) { x.UpdateParent(context, config, this, previous); } foreach (var x in me.Sandboxes) { x.UpdateParent(context, config, this, previous); } context.Update(this); }
public void CreateNew(DatabaseContext context, IConfiguration config) { try { context.Add(this); } catch (Exception e) { Console.Error.WriteLine(e.GetBaseException().ToString()); } CreateDirectory(context, config); // parse teachers and students var flts = context.Users.Select(x => x.Account); var ts = TeachersForEdit?.Split(' ', ',', '\t', '\n', '\r') ?? new string[0] { }; var ss = StudentsForEdit?.Split(' ', ',', '\t', '\n', '\r')?.Except(ts) ?? new string[0] { }; foreach (var x in ts.Intersect(flts)) { var user = context.Users.Where(y => y.Account == x).FirstOrDefault(); var a = new LectureUser() { Lecture = this, User = user, Role = LectureUserRole.Teacher }; LectureUsers.Add(a); a.CreateNew(context, config); } foreach (var x in ss.Intersect(flts)) { var user = context.Users.Where(y => y.Account == x).FirstOrDefault(); var a = new LectureUser() { Lecture = this, User = user, Role = LectureUserRole.Student }; LectureUsers.Add(a); a.CreateNew(context, config); } // Create repositories LectureContentsRepositoryPair.Create(); Console.WriteLine($"==> {IsEmptyRepositories}"); if (!IsEmptyRepositories) { var cc = new DirectoryInfo(LectureContentsRepositoryPair.ClonedRepository.DirectoryPath); var pages = cc.CreateSubdirectory("pages"); using (var x = new StreamWriter($"{pages.FullName}/@index.md")) { x.WriteLine($"# {Subject}"); } var activities = cc.CreateSubdirectory("activities"); using (var x = new StreamWriter($"{activities.FullName}/.keep")) { x.WriteLine(""); } LectureContentsRepositoryPair.ClonedRepository.CommitChanges("Initial Commit", Owner.DisplayName, Owner.EmailAddress); LectureContentsRepositoryPair.ClonedRepository.Push(); } LectureSubmissionsRepositoryPair.Create(); using (var x = new StreamWriter($"{LectureSubmissionsRepositoryPair.ClonedRepository.DirectoryPath}/.keep")) { x.WriteLine(""); } LectureSubmissionsRepositoryPair.ClonedRepository.CommitChanges("Initial Commit", Owner.DisplayName, Owner.EmailAddress); LectureSubmissionsRepositoryPair.ClonedRepository.Push(); }