コード例 #1
0
        public void CreateUser(User user)
        {
            using (AcademyContext academyDb = new AcademyContext(options))
            {
                Role userRole = academyDb.Role.FirstOrDefault(r => r.Name == "user");
                if (userRole != null)
                {
                    user.Role = userRole;
                }

                academyDb.User.Add(user);
                academyDb.SaveChanges();
            }
        }
コード例 #2
0
        public void DeleteLecturer(Lecturer lecturer)
        {
            using (AcademyContext academyDb = new AcademyContext(options))
            {
                using (var transaction = academyDb.Database.BeginTransaction())
                {
                    // delete course from lecturer-courcse table
                    var lectureCoursesForDelete = academyDb.LecturerCourse.Where(lc => lc.LecturerId == lecturer.Id);
                    foreach (var lc in lectureCoursesForDelete)
                    {
                        academyDb.LecturerCourse.Remove(lc);
                    }
                    // delete lecturer
                    academyDb.Lecturer.Remove(lecturer);
                    academyDb.SaveChanges();

                    transaction.Commit();
                }
            }
        }
コード例 #3
0
        public void DeleteHomeTask(HomeTask homeTask)
        {
            using (AcademyContext academyDb = new AcademyContext(options))
            {
                using (var transaction = academyDb.Database.BeginTransaction())
                {
                    // delete hometask assessments
                    var hometaskAssessmentsForDelete = academyDb.HomeTaskAssessment.Where(a => a.HomeTaskId == homeTask.Id);
                    foreach (var ha in hometaskAssessmentsForDelete)
                    {
                        academyDb.HomeTaskAssessment.Remove(ha);
                    }

                    // delete hometask
                    academyDb.HomeTask.Remove(homeTask);
                    academyDb.SaveChanges();

                    transaction.Commit();
                }
            }
        }
コード例 #4
0
        public void DeleteCourse(Course course)
        {
            using (AcademyContext academyDb = new AcademyContext(options))
            {
                using (var transaction = academyDb.Database.BeginTransaction())
                {
                    // delete course from lecturer-courcse table
                    var lectureCoursesForDelete = academyDb.LecturerCourse.Where(lc => lc.CourseId == course.Id);
                    foreach (var lcd in lectureCoursesForDelete)
                    {
                        academyDb.LecturerCourse.Remove(lcd);
                    }

                    // delete course from student-courcse table
                    var studentCoursesForDelete = academyDb.StudentCourse.Where(sc => sc.CourseId == course.Id);
                    foreach (var sc in studentCoursesForDelete)
                    {
                        academyDb.StudentCourse.Remove(sc);
                    }

                    // delete assessment from HomeTaskAssessment table
                    // delete hometasks from Hometask table
                    var hometasksForDelete = academyDb.HomeTask.Where(h => h.CourseId == course.Id);
                    foreach (var hd in hometasksForDelete)
                    {
                        var assessmentsForDelete = academyDb.HomeTaskAssessment.Where(ha => ha.HomeTaskId == hd.Id);
                        foreach (var ad in assessmentsForDelete)
                        {
                            academyDb.HomeTaskAssessment.Remove(ad);
                        }
                        academyDb.HomeTask.Remove(hd);
                    }
                    // delete course
                    academyDb.Course.Remove(course);
                    academyDb.SaveChanges();

                    transaction.Commit();
                }
            }
        }
コード例 #5
0
        public void AssignLecturersToCourse(int courseId, List <int> lecturersIds)
        {
            using (AcademyContext academyDb = new AcademyContext(options))
            {
                // get all course notations
                var lectCour = academyDb.LecturerCourse.Where(lc => lc.CourseId == courseId);
                foreach (var lc in lectCour)
                {
                    academyDb.LecturerCourse.Remove(lc);
                }

                foreach (var lcId in lecturersIds)
                {
                    academyDb.LecturerCourse.Add(new LecturerCourse()
                    {
                        LecturerId = lcId, CourseId = courseId
                    });
                }

                academyDb.SaveChanges();
            }
        }
コード例 #6
0
        public void CreateHomeTask(HomeTask homeTask)
        {
            // create hometask
            using (AcademyContext academyDb = new AcademyContext(options))
            {
                academyDb.HomeTask.Add(homeTask);
                academyDb.SaveChanges();
            }

            using (AcademyContext academyDb = new AcademyContext(options))
            {
                if (academyDb.StudentCourse.Where(sc => sc.CourseId == homeTask.CourseId).Count() > 0)
                {
                    // create assessments for all students in course without assessments
                    List <int> courseHometasksIds = academyDb.HomeTask.Where(h => h.CourseId == homeTask.CourseId).Select(h => h.Id).ToList();
                    List <int> courseStudentsIds  = academyDb.StudentCourse.Where(st => st.CourseId == homeTask.CourseId).Select(st => st.StudentId).ToList();
                    foreach (var hi in courseHometasksIds)
                    {
                        if (academyDb.HomeTaskAssessment.Where(ha => ha.HomeTaskId == hi).Count() == 0)
                        {
                            foreach (var csi in courseStudentsIds)
                            {
                                academyDb.HomeTaskAssessment.Add(
                                    new HomeTaskAssessment()
                                {
                                    IsComplete = false,
                                    Date       = DateTime.Now,
                                    HomeTaskId = homeTask.Id,
                                    StudentId  = csi
                                });
                            }
                        }
                    }
                    academyDb.SaveChanges();
                }
            }
        }