Esempio n. 1
0
        public void CreateNew(DatabaseContext context, IConfiguration config)
        {
            context.Add(this);
            context.SaveChanges();

            var newStudents = (StudentAccounts ?? "").Split(new string[] { " ", "\t", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var s in newStudents)
            {
                Student student = context.Students.Where(x => x.Account == s).FirstOrDefault();
                if (student == null)
                {
                    student = new Student()
                    {
                        Account = s,
                        Name    = s,
                        Hash    = ""
                    };
                    context.Add(student);
                }

                var a = new CourseStudentAssignment()
                {
                    CourseId = Id,
                    Student  = student
                };
                context.Add(a);
            }
        }
Esempio n. 2
0
        public void Update(DatabaseContext context, IConfiguration config, Course previous)
        {
            var newStudents    = (StudentAccounts ?? "").Split(new string[] { " ", "\t", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
            var oldStudentd    = context.CourseStudentAssignments.Include(x => x.Student).Where(x => x.CourseId == Id).Select(x => x.Student.Account).AsNoTracking().ToArray();
            var addStudents    = newStudents.Except(oldStudentd);
            var removeStudents = oldStudentd.Except(newStudents);

            foreach (var s in addStudents)
            {
                Student student = context.Students.Where(x => x.Account == s).FirstOrDefault();
                if (student == null)
                {
                    student = new Student()
                    {
                        Account = s,
                        Name    = s,
                        Hash    = ""
                    };
                    context.Add(student);
                }

                var a = new CourseStudentAssignment()
                {
                    CourseId = Id,
                    Student  = student
                };
                context.Add(a);
            }
            foreach (var s in removeStudents)
            {
                var a = context.CourseStudentAssignments.Include(x => x.Student).Where(x => x.Student.Account == s).FirstOrDefault();
                if (a != null)
                {
                    StudentAssignments.Remove(a);
                }
            }


            context.Update(this);
        }