public bool AddScheduleSubjectsByClassId(ScheduleSubjectOfClassDto dto) { try { if (!IsValidInput(dto)) { return(false); } foreach (var item in dto.ScheduleSubjects) { var schedule = new ScheduleSubject { TeacherId = item.TeacherId, ClassId = dto.ClassId, SubjectId = item.SubjectId, SemesterId = dto.SemesterId, DayLessonId = GetDayLessonId(item.DayOfWeek, item.Lesson) }; db.ScheduleSubjects.Add(schedule); } db.SaveChanges(); return(true); } catch (Exception) { return(false); } }
private bool IsValidInput(ScheduleSubjectOfClassDto dto) { var scheduleSubjects = db.ScheduleSubjects.Include(x => x.DayOfWeekLesson).ToList(); foreach (var schedule in dto.ScheduleSubjects) { if (scheduleSubjects.Any(x => !x.DelFlag && (x.ClassId == dto.ClassId || x.TeacherId == schedule.TeacherId) && x.SemesterId == dto.SemesterId && x.DayOfWeekLesson.DayOfWeek == schedule.DayOfWeek && x.DayOfWeekLesson.Lesson == schedule.Lesson)) { return(false); } } return(true); }