public void init(EWeekSchedule DBSchedule) { mon = new List <EScheduleItem>(8); tue = new List <EScheduleItem>(8); wed = new List <EScheduleItem>(8); thu = new List <EScheduleItem>(8); fri = new List <EScheduleItem>(8); sat = new List <EScheduleItem>(8); SpecialityId = DBSchedule.Speciality.Id.ToString(); WeekScheduleName = DBSchedule.Name; WeekScheduleId = DBSchedule.id; for (int i = 0; i < 8; i++) { mon.Add(DBSchedule.Schedule[0].DisciplinesForDay[i]); tue.Add(DBSchedule.Schedule[1].DisciplinesForDay[i]); wed.Add(DBSchedule.Schedule[2].DisciplinesForDay[i]); thu.Add(DBSchedule.Schedule[3].DisciplinesForDay[i]); fri.Add(DBSchedule.Schedule[4].DisciplinesForDay[i]); sat.Add(DBSchedule.Schedule[5].DisciplinesForDay[i]); } }
public async Task <IActionResult> CreateSchedule(CreateScheduleViewModel model) { var spec = await(from sp in DBContext.Specialities.Include(s => s.Schedules) where sp.Id.ToString() == model.SpecialityId select sp).FirstOrDefaultAsync(); if (model.WeekScheduleName == null) { model.WeekScheduleName = "Без названия"; } var NewWeekSchedule = new EWeekSchedule() { Name = model.WeekScheduleName, Speciality = spec, Schedule = new List <EDaySchedule>(6) }; // DBContext.WeekSchedules.Add(NewWeekSchedule); NewWeekSchedule.Schedule.Add(new EDaySchedule()//mon { WeekSchedule = NewWeekSchedule, DayNumber = 0, DisciplinesForDay = new List <EScheduleItem>(8) }); //DBContext.DaySchedules.Add(NewWeekSchedule.Schedule[0]); for (int i = 0; i < 8; i++) { var discipline = model.mon[i]; discipline.DaySchedule = NewWeekSchedule.Schedule[0]; if (discipline.DisciplineId.ToString() == "-1") { discipline.Name = "Нет пары"; discipline.TeacherFIO = ""; discipline.Type = ""; } else { var disc = await(from di in DBContext.Disciplines.Include(t => t.Teacher) where di.Id == discipline.DisciplineId select di).FirstOrDefaultAsync(); discipline.Name = disc.Name; discipline.TeacherFIO = disc.Teacher.FIO; } if (discipline.Classroom == null) { discipline.Classroom = ""; } NewWeekSchedule.Schedule[0].DisciplinesForDay.Add(discipline); } NewWeekSchedule.Schedule.Add(new EDaySchedule() //tue { WeekSchedule = NewWeekSchedule, DayNumber = 1, DisciplinesForDay = new List <EScheduleItem>(8) }); //DBContext.DaySchedules.Add(NewWeekSchedule.Schedule[0]); for (int i = 0; i < 8; i++) { var discipline = model.tue[i]; discipline.DaySchedule = NewWeekSchedule.Schedule[1]; if (discipline.DisciplineId.ToString() == "-1") { discipline.Name = "Нет пары"; discipline.TeacherFIO = ""; discipline.Type = ""; } else { var disc = await(from di in DBContext.Disciplines.Include(t => t.Teacher) where di.Id == discipline.DisciplineId select di).FirstOrDefaultAsync(); discipline.Name = disc.Name; discipline.TeacherFIO = disc.Teacher.FIO; } if (discipline.Classroom == null) { discipline.Classroom = ""; } NewWeekSchedule.Schedule[1].DisciplinesForDay.Add(discipline); } NewWeekSchedule.Schedule.Add(new EDaySchedule() //wed { WeekSchedule = NewWeekSchedule, DayNumber = 2, DisciplinesForDay = new List <EScheduleItem>(8) }); //DBContext.DaySchedules.Add(NewWeekSchedule.Schedule[0]); for (int i = 0; i < 8; i++) { var discipline = model.wed[i]; discipline.DaySchedule = NewWeekSchedule.Schedule[2]; if (discipline.DisciplineId.ToString() == "-1") { discipline.Name = "Нет пары"; discipline.TeacherFIO = ""; discipline.Type = ""; } else { var disc = await(from di in DBContext.Disciplines.Include(t => t.Teacher) where di.Id == discipline.DisciplineId select di).FirstOrDefaultAsync(); discipline.Name = disc.Name; discipline.TeacherFIO = disc.Teacher.FIO; } if (discipline.Classroom == null) { discipline.Classroom = ""; } NewWeekSchedule.Schedule[2].DisciplinesForDay.Add(discipline); } NewWeekSchedule.Schedule.Add(new EDaySchedule() //thu { WeekSchedule = NewWeekSchedule, DayNumber = 3, DisciplinesForDay = new List <EScheduleItem>(8) }); //DBContext.DaySchedules.Add(NewWeekSchedule.Schedule[0]); for (int i = 0; i < 8; i++) { var discipline = model.thu[i]; discipline.DaySchedule = NewWeekSchedule.Schedule[3]; if (discipline.DisciplineId.ToString() == "-1") { discipline.Name = "Нет пары"; discipline.TeacherFIO = ""; discipline.Type = ""; } else { var disc = await(from di in DBContext.Disciplines.Include(t => t.Teacher) where di.Id == discipline.DisciplineId select di).FirstOrDefaultAsync(); discipline.Name = disc.Name; discipline.TeacherFIO = disc.Teacher.FIO; } if (discipline.Classroom == null) { discipline.Classroom = ""; } NewWeekSchedule.Schedule[3].DisciplinesForDay.Add(discipline); } NewWeekSchedule.Schedule.Add(new EDaySchedule() //fri { WeekSchedule = NewWeekSchedule, DayNumber = 4, DisciplinesForDay = new List <EScheduleItem>(8) }); //DBContext.DaySchedules.Add(NewWeekSchedule.Schedule[0]); for (int i = 0; i < 8; i++) { var discipline = model.fri[i]; discipline.DaySchedule = NewWeekSchedule.Schedule[4]; if (discipline.DisciplineId.ToString() == "-1") { discipline.Name = "Нет пары"; discipline.TeacherFIO = ""; discipline.Type = ""; } else { var disc = await(from di in DBContext.Disciplines.Include(t => t.Teacher) where di.Id == discipline.DisciplineId select di).FirstOrDefaultAsync(); discipline.Name = disc.Name; discipline.TeacherFIO = disc.Teacher.FIO; } if (discipline.Classroom == null) { discipline.Classroom = ""; } NewWeekSchedule.Schedule[4].DisciplinesForDay.Add(discipline); } NewWeekSchedule.Schedule.Add(new EDaySchedule() //sat { WeekSchedule = NewWeekSchedule, DayNumber = 5, DisciplinesForDay = new List <EScheduleItem>(8) }); //DBContext.DaySchedules.Add(NewWeekSchedule.Schedule[0]); for (int i = 0; i < 8; i++) { var discipline = model.sat[i]; discipline.DaySchedule = NewWeekSchedule.Schedule[5]; if (discipline.DisciplineId.ToString() == "-1") { discipline.Name = "Нет пары"; discipline.TeacherFIO = ""; discipline.Type = ""; } else { var disc = await(from di in DBContext.Disciplines.Include(t => t.Teacher) where di.Id == discipline.DisciplineId select di).FirstOrDefaultAsync(); discipline.Name = disc.Name; discipline.TeacherFIO = disc.Teacher.FIO; } if (discipline.Classroom == null) { discipline.Classroom = ""; } NewWeekSchedule.Schedule[5].DisciplinesForDay.Add(discipline); } spec.Schedules.Add(NewWeekSchedule); DBContext.Add(NewWeekSchedule); await DBContext.SaveChangesAsync(); return(RedirectToAction("Index", new { SpecId = spec.Id })); }