public void UpdateCourse(Course course)
        {
            using (AcademyContext academyDb = new AcademyContext(options))
            {
                academyDb.Course.Update(course);

                academyDb.SaveChanges();
            }
        }
 // User table
 public User GetUser(User user)
 {
     using (AcademyContext academyDb = new AcademyContext(options))
     {
         return(academyDb.User
                .Include(u => u.Role)
                .FirstOrDefault(u => u.Email == user.Email && u.Password == user.Password));
     }
 }
 public void DeleteHomeTaskAssessment(int id)
 {
     using (AcademyContext academyDb = new AcademyContext(options))
     {
         HomeTaskAssessment homeTaskAssessment = GetHomeTaskAssessment(id);
         DeleteHomeTaskAssessment(homeTaskAssessment);
         academyDb.SaveChanges();
     }
 }
 public void DeleteStudent(int id)
 {
     using (AcademyContext academyDb = new AcademyContext(options))
     {
         Student student = GetStudent(id);
         DeleteStudent(student);
         academyDb.SaveChanges();
     }
 }
 public void DeleteHomeTask(int id)
 {
     using (AcademyContext academyDb = new AcademyContext(options))
     {
         HomeTask homeTask = GetHomeTask(id);
         DeleteHomeTask(homeTask);
         academyDb.SaveChanges();
     }
 }
 public void DeleteLecturer(int id)
 {
     using (AcademyContext academyDb = new AcademyContext(options))
     {
         Lecturer lecturer = GetLecturer(id);
         DeleteLecturer(lecturer);
         academyDb.SaveChanges();
     }
 }
        public void AssignStudentsToCourse(int courseId, IEnumerable <int> assignedStudentsIds)
        {
            using (AcademyContext academyDb = new AcademyContext(options))
            {
                using (var transaction = academyDb.Database.BeginTransaction())
                {
                    List <int> previouslyAssignedstudentsIds = academyDb.StudentCourse.Where(sc => sc.CourseId == courseId).Select(sc => sc.StudentId).ToList();
                    List <int> disassignedStudentsIds        = previouslyAssignedstudentsIds.Except(assignedStudentsIds).ToList();
                    List <int> newAssignedStudentsIds        = assignedStudentsIds.Except(previouslyAssignedstudentsIds).ToList();

                    // delete disassigned students assessments
                    foreach (var disStId in disassignedStudentsIds)
                    {
                        // todo remove range not necessary
                        academyDb.HomeTaskAssessment.RemoveRange(
                            academyDb.HomeTaskAssessment.Where(ha => ha.StudentId == disStId));
                    }

                    // disassigne disassigned students
                    foreach (var disStId in disassignedStudentsIds)
                    {
                        // todo remove range not necessary
                        academyDb.StudentCourse.RemoveRange(
                            academyDb.StudentCourse.Where(sc => sc.StudentId == disStId));
                    }

                    // assign new assigned students
                    foreach (var asi in newAssignedStudentsIds)
                    {
                        academyDb.StudentCourse.Add(new StudentCourse()
                        {
                            StudentId = asi, CourseId = courseId
                        });
                    }

                    // create assessments for new assigned students
                    List <int> courseHometasksIds = academyDb.HomeTask.Where(h => h.CourseId == courseId).Select(h => h.Id).ToList();
                    foreach (var studentId in newAssignedStudentsIds)
                    {
                        foreach (var hometaskId in courseHometasksIds)
                        {
                            HomeTaskAssessment assessment = new HomeTaskAssessment()
                            {
                                IsComplete = false,
                                Date       = DateTime.Now,
                                HomeTaskId = hometaskId,
                                StudentId  = studentId
                            };
                            academyDb.HomeTaskAssessment.Add(assessment);
                        }
                    }
                    academyDb.SaveChanges();

                    transaction.Commit();
                }
            }
        }
 public void DeleteCourse(int id)
 {
     using (AcademyContext academyDb = new AcademyContext(options))
     {
         Course course = GetCourse(id);
         DeleteCourse(course);
         academyDb.SaveChanges();
     }
 }
        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();
            }
        }
        public List <HomeTaskAssessment> GetHomeTaskAssessmentsByStudentId(int studentId)
        {
            using (AcademyContext academyDb = new AcademyContext(options))
            {
                List <HomeTaskAssessment> studentAssessments = academyDb.HomeTaskAssessment.Where(a => a.StudentId == studentId).ToList();

                foreach (var assessment in studentAssessments)
                {
                    assessment.HomeTask        = academyDb.HomeTask.FirstOrDefault(h => h.Id == assessment.HomeTaskId);
                    assessment.HomeTask.Course = academyDb.Course.FirstOrDefault(c => c.Id == assessment.HomeTask.CourseId);
                }

                return(studentAssessments);
            }
        }
        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();
                }
            }
        }
        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();
                }
            }
        }
        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();
                }
            }
        }
        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();
            }
        }
        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();
                }
            }
        }