public GroupsInFaculty AddGroupsInFaculty(GroupsInFaculty groupsInFaculty)
        {
            using (var context = new ScheduleContext(ConnectionString))
            {
                groupsInFaculty.GroupsInFacultyId = 0;

                groupsInFaculty.StudentGroup = context.StudentGroups.FirstOrDefault(gif => gif.StudentGroupId == groupsInFaculty.StudentGroup.StudentGroupId);
                groupsInFaculty.Faculty = context.Faculties.FirstOrDefault(gif => gif.FacultyId == groupsInFaculty.Faculty.FacultyId);

                context.GroupsInFaculties.Add(groupsInFaculty);
                context.SaveChanges();

                return groupsInFaculty;
            }
        }
 public ConfigOption FindConfigOption(string key)
 {
     using (var context = new ScheduleContext(ConnectionString))
     {
         return context.Config.FirstOrDefault(op => op.Key == key);
     }
 }
 public Faculty FindFaculty(string name)
 {
     using (var context = new ScheduleContext(ConnectionString))
     {
         return context.Faculties.FirstOrDefault(f => f.Name == name);
     }
 }
        public void ClearAllExams()
        {
            using (var context = new ScheduleContext(ConnectionString))
            {
                var examIds = context.Exams.Select(e => e.ExamId).ToList();

                foreach (var examId in examIds)
                {
                    RemoveExam(examId);
                }
            }
        }
 public Auditorium FindAuditorium(string name)
 {
     using (var context = new ScheduleContext(ConnectionString))
     {
         return context.Auditoriums.FirstOrDefault(a => a.Name == name);
     }
 }
        public Teacher AddTeacher(Teacher teacher)
        {
            using (var context = new ScheduleContext(ConnectionString))
            {
                teacher.TeacherId = 0;

                context.Teachers.Add(teacher);
                context.SaveChanges();

                return teacher;
            }
        }
        public void AddTeacherForDisciplineRange(IEnumerable<TeacherForDiscipline> teacherForDisciplineList)
        {
            using (var context = new ScheduleContext(ConnectionString))
            {
                foreach (var teacherForDiscipline in teacherForDisciplineList)
                {
                    teacherForDiscipline.TeacherForDisciplineId = 0;
                    context.TeacherForDiscipline.Add(teacherForDiscipline);
                }

                context.SaveChanges();
            }
        }
        public Lesson AddLessonWOLog(Lesson lesson)
        {
            using (var context = new ScheduleContext(ConnectionString))
            {
                lesson.LessonId = 0;

                lesson.TeacherForDiscipline = context.TeacherForDiscipline.FirstOrDefault(tfd => tfd.TeacherForDisciplineId == lesson.TeacherForDiscipline.TeacherForDisciplineId);
                lesson.Calendar = context.Calendars.FirstOrDefault(c => c.CalendarId == lesson.Calendar.CalendarId);
                lesson.Ring = context.Rings.FirstOrDefault(r => r.RingId == lesson.Ring.RingId);
                lesson.Auditorium = context.Auditoriums.FirstOrDefault(a => a.AuditoriumId == lesson.Auditorium.AuditoriumId);

                context.Lessons.Add(lesson);

                context.SaveChanges();

                return lesson;
            }
        }
        public LogEvent AddLogEvent(LogEvent logEvent)
        {
            using (var context = new ScheduleContext(ConnectionString))
            {
                logEvent.OldExam = context.Exams.FirstOrDefault(e => e.ExamId == logEvent.OldExam.ExamId);
                logEvent.NewExam = context.Exams.FirstOrDefault(e => e.ExamId == logEvent.NewExam.ExamId);

                context.EventLog.Add(logEvent);
                context.SaveChanges();

                return logEvent;
            }
        }
        public void AddLessonLogEventRange(IEnumerable<LessonLogEvent> lessonLogEventList)
        {
            using (var context = new ScheduleContext(ConnectionString))
            {
                foreach (var lessonLogEvent in lessonLogEventList)
                {
                    lessonLogEvent.LessonLogEventId = 0;

                    lessonLogEvent.OldLesson = context.Lessons.FirstOrDefault(l => l.LessonId == lessonLogEvent.OldLesson.LessonId);
                    lessonLogEvent.NewLesson = context.Lessons.FirstOrDefault(l => l.LessonId == lessonLogEvent.NewLesson.LessonId);

                    context.LessonLog.Add(lessonLogEvent);
                }

                context.SaveChanges();
            }
        }
        public void AddLessonRange(IEnumerable<Lesson> lessonList)
        {
            using (var context = new ScheduleContext(ConnectionString))
            {
                foreach (var lesson in lessonList)
                {
                    lesson.LessonId = 0;

                    lesson.TeacherForDiscipline = context.TeacherForDiscipline.FirstOrDefault(tfd => tfd.TeacherForDisciplineId == lesson.TeacherForDiscipline.TeacherForDisciplineId);
                    lesson.Calendar = context.Calendars.FirstOrDefault(c => c.CalendarId == lesson.Calendar.CalendarId);
                    lesson.Ring = context.Rings.FirstOrDefault(r => r.RingId == lesson.Ring.RingId);
                    lesson.Auditorium = context.Auditoriums.FirstOrDefault(a => a.AuditoriumId == lesson.Auditorium.AuditoriumId);

                    context.Lessons.Add(lesson);
                }

                context.SaveChanges();
            }
        }
        public LessonLogEvent AddLessonLogEvent(LessonLogEvent lessonLogEvent)
        {
            using (var context = new ScheduleContext(ConnectionString))
            {
                lessonLogEvent.LessonLogEventId = 0;
                lessonLogEvent.OldLesson = (lessonLogEvent.OldLesson == null) ? null : context.Lessons.FirstOrDefault(l => l.LessonId == lessonLogEvent.OldLesson.LessonId);
                lessonLogEvent.NewLesson = (lessonLogEvent.NewLesson == null) ? null : context.Lessons.FirstOrDefault(l => l.LessonId == lessonLogEvent.NewLesson.LessonId);

                context.LessonLog.Add(lessonLogEvent);
                context.SaveChanges();

                return lessonLogEvent;
            }
        }
        public Lesson AddLesson(Lesson lesson, string publicComment = "", string hiddenComment = "")
        {
            using (var context = new ScheduleContext(ConnectionString))
            {
                lesson.LessonId = 0;

                lesson.TeacherForDiscipline = context.TeacherForDiscipline.FirstOrDefault(tfd => tfd.TeacherForDisciplineId == lesson.TeacherForDiscipline.TeacherForDisciplineId);
                lesson.Calendar = context.Calendars.FirstOrDefault(c => c.CalendarId == lesson.Calendar.CalendarId);
                lesson.Ring = context.Rings.FirstOrDefault(r => r.RingId == lesson.Ring.RingId);
                lesson.Auditorium = context.Auditoriums.FirstOrDefault(a => a.AuditoriumId == lesson.Auditorium.AuditoriumId);

                context.Lessons.Add(lesson);

                context.LessonLog.Add(
                    new LessonLogEvent
                    {
                        OldLesson = null,
                        NewLesson = lesson,
                        DateTime = DateTime.Now,
                        PublicComment = publicComment,
                        HiddenComment = hiddenComment
                    }
                );
                context.SaveChanges();

                return lesson;
            }
        }
        public void AddGroupsInFacultyRange(IEnumerable<GroupsInFaculty> groupsInFacultiesList)
        {
            using (var context = new ScheduleContext(ConnectionString))
            {
                foreach (var groupsInFaculty in groupsInFacultiesList)
                {
                    groupsInFaculty.GroupsInFacultyId = 0;

                    groupsInFaculty.StudentGroup = context.StudentGroups.FirstOrDefault(gif => gif.StudentGroupId == groupsInFaculty.StudentGroup.StudentGroupId);
                    groupsInFaculty.Faculty = context.Faculties.FirstOrDefault(gif => gif.FacultyId == groupsInFaculty.Faculty.FacultyId);

                    context.GroupsInFaculties.Remove(groupsInFaculty);
                }

                context.SaveChanges();
            }
        }
        public StudentsInGroups AddStudentsInGroups(StudentsInGroups studentsInGroups)
        {
            using (var context = new ScheduleContext(ConnectionString))
            {
                studentsInGroups.StudentsInGroupsId = 0;

                studentsInGroups.Student = context.Students.FirstOrDefault(s => s.StudentId == studentsInGroups.Student.StudentId);
                studentsInGroups.StudentGroup = context.StudentGroups.FirstOrDefault(sg => sg.StudentGroupId == studentsInGroups.StudentGroup.StudentGroupId);

                context.StudentsInGroups.Add(studentsInGroups);
                context.SaveChanges();

                return studentsInGroups;
            }
        }
        public Ring AddRing(Ring ring)
        {
            using (var context = new ScheduleContext(ConnectionString))
            {
                ring.RingId = 0;

                context.Rings.Add(ring);
                context.SaveChanges();

                return ring;
            }
        }
        public void AddStudentsInGroupsRange(IEnumerable<StudentsInGroups> studentsInGroupsList)
        {
            using (var context = new ScheduleContext(ConnectionString))
            {
                foreach (var studentsInGroups in studentsInGroupsList)
                {
                    studentsInGroups.StudentsInGroupsId = 0;
                    context.StudentsInGroups.Add(studentsInGroups);
                }

                context.SaveChanges();
            }
        }
        public void AddRingRange(IEnumerable<Ring> ringList)
        {
            using (var context = new ScheduleContext(ConnectionString))
            {
                foreach (var ring in ringList)
                {
                    ring.RingId = 0;
                    context.Rings.Add(ring);
                }

                context.SaveChanges();
            }
        }
        public TeacherForDiscipline AddTeacherForDiscipline(TeacherForDiscipline teacherForDiscipline)
        {
            using (var context = new ScheduleContext(ConnectionString))
            {
                teacherForDiscipline.TeacherForDisciplineId = 0;

                teacherForDiscipline.Teacher = context.Teachers.FirstOrDefault(t => t.TeacherId == teacherForDiscipline.Teacher.TeacherId);
                teacherForDiscipline.Discipline = context.Disciplines.FirstOrDefault(d => d.DisciplineId == teacherForDiscipline.Discipline.DisciplineId);

                context.TeacherForDiscipline.Add(teacherForDiscipline);
                context.SaveChanges();

                return teacherForDiscipline;
            }
        }
        public ScheduleNote AddScheduleNote(ScheduleNote sNote)
        {
            using (var context = new ScheduleContext(ConnectionString))
            {
                sNote.ScheduleNoteId = 0;

                sNote.Lesson.TeacherForDiscipline = context.TeacherForDiscipline.FirstOrDefault(tfd => tfd.TeacherForDisciplineId == sNote.Lesson.TeacherForDiscipline.TeacherForDisciplineId);
                sNote.Lesson.Calendar = context.Calendars.FirstOrDefault(c => c.CalendarId == sNote.Lesson.Calendar.CalendarId);
                sNote.Lesson.Ring = context.Rings.FirstOrDefault(r => r.RingId == sNote.Lesson.Ring.RingId);
                sNote.Lesson.Auditorium = context.Auditoriums.FirstOrDefault(a => a.AuditoriumId == sNote.Lesson.Auditorium.AuditoriumId);

                context.ScheduleNotes.Add(sNote);
                context.SaveChanges();

                return sNote;
            }
        }
        public void AddTeacherRange(IEnumerable<Teacher> teacherList)
        {
            using (var context = new ScheduleContext(ConnectionString))
            {
                foreach (var teacher in teacherList)
                {
                    teacher.TeacherId = 0;
                    context.Teachers.Add(teacher);
                }

                context.SaveChanges();
            }
        }
        public void AddScheduleNoteRange(IEnumerable<ScheduleNote> scheduleNoteList)
        {
            using (var context = new ScheduleContext(ConnectionString))
            {
                foreach (var sNote in scheduleNoteList)
                {
                    sNote.ScheduleNoteId = 0;

                    context.ScheduleNotes.Add(sNote);
                }

                context.SaveChanges();
            }
        }
        public void ClearExamLogs()
        {
            using (var context = new ScheduleContext(ConnectionString))
            {
                var logIds = context.EventLog.Select(le => le.LogEventId).ToList();

                foreach (var logId in logIds)
                {
                    RemoveLogEvent(logId);
                }
            }
        }
        public Student AddStudent(Student student)
        {
            using (var context = new ScheduleContext(ConnectionString))
            {
                student.StudentId = 0;

                context.Students.Add(student);
                context.SaveChanges();

                return student;
            }
        }
 public Calendar FindCalendar(DateTime date)
 {
     using (var context = new ScheduleContext(ConnectionString))
     {
         return context.Calendars.FirstOrDefault(c => c.Date == date);
     }
 }
        public StudentGroup AddStudentGroup(StudentGroup studentGroup)
        {
            using (var context = new ScheduleContext(ConnectionString))
            {
                studentGroup.StudentGroupId = 0;

                context.StudentGroups.Add(studentGroup);
                context.SaveChanges();

                return studentGroup;
            }
        }
 public Discipline FindDiscipline(string name, int semester, int attestation, int auditoriumHours, int lectureHours, int practicalHours, string groupName)
 {
     using (var context = new ScheduleContext(ConnectionString))
     {
         return context.Disciplines.Include(d => d.StudentGroup).FirstOrDefault(
             d => d.Name == name &&
                  d.Semester == semester &&
                  d.Attestation == attestation &&
                  d.AuditoriumHours == auditoriumHours &&
                  d.LectureHours == lectureHours &&
                  d.PracticalHours == practicalHours &&
                  d.StudentGroup.Name == groupName);
     }
 }
        public void AddStudentRange(IEnumerable<Student> studentList)
        {
            using (var context = new ScheduleContext(ConnectionString))
            {
                foreach (var student in studentList)
                {
                    student.StudentId = 0;
                    context.Students.Add(student);
                }

                context.SaveChanges();
            }
        }
 public GroupsInFaculty FindGroupsInFaculty(StudentGroup sg, Faculty f)
 {
     using (var context = new ScheduleContext(ConnectionString))
     {
         return context.GroupsInFaculties
             .Include(gif => gif.StudentGroup)
             .Include(gif => gif.Faculty)
             .FirstOrDefault(gif => gif.StudentGroup.StudentGroupId == sg.StudentGroupId &&
                                    gif.Faculty.FacultyId == f.FacultyId);
     }
 }
        public void AddFacultyRange(IEnumerable<Faculty> facultyList)
        {
            using (var context = new ScheduleContext(ConnectionString))
            {
                foreach (var faculty in facultyList)
                {
                    faculty.FacultyId = 0;
                    context.Faculties.Add(faculty);
                }

                context.SaveChanges();
            }
        }