public IHttpActionResult GetStudentAgenda(int id) { // find instead of any: any searches the database, find searches the context if (db.People.Find(id) is Instructor) { return(NotFound()); } Student student = db.Students.Find(id); if (student == null) { return(NotFound()); } List <EnrollmentApiVM> CourseIdList = new List <EnrollmentApiVM>(); StudentAgendaApiVM studentAgendaApiVM = new StudentAgendaApiVM(); Dictionary <int, LessonApiVM> lessonsList = new Dictionary <int, LessonApiVM>(); int key = 0; foreach (Enrollment enrollment in student.Enrollments) { EnrollmentApiVM enrollmentApiVM = new EnrollmentApiVM(); enrollmentApiVM.courseId = enrollment.CourseID; List <Lessons> lessons = new List <Lessons>(); lessons = db.Lessons.Where(c => c.CourseID == enrollment.CourseID).ToList(); foreach (var item in lessons) { LessonApiVM lessonApiVM = new LessonApiVM { courseId = item.CourseID, day = item.Day.ToString(), duration = item.Duration.ToString(), startHour = item.HourStart.ToString() }; lessonsList.Add(key, lessonApiVM); key++; } CourseIdList.Add(enrollmentApiVM); } studentAgendaApiVM.id = student.ID; studentAgendaApiVM.lastname = student.LastName; studentAgendaApiVM.firstname = student.FirstMidName; studentAgendaApiVM.lessonsVMs = lessonsList; studentAgendaApiVM.enrollments = CourseIdList; //return ViewModel return(Ok(studentAgendaApiVM)); }
public IHttpActionResult GetInstructor(int id, string weeklyschedule) { if (InstructorExists(id) == false) { return(NotFound()); } SchoolContext db = new SchoolContext(); InstructorApiVM instructorApiVM = new InstructorApiVM(); instructorApiVM.instructorId = id; instructorApiVM.schedule = new List <LessonApiVM>(); List <Course> courseList = db.Courses.Where(x => x.Instructors.Any(y => y.ID == id)).ToList(); //List<Lessons> lessonsListe = db.Lessons.Where(c => c.InstructorID == id).ToList(); List <Lessons> lessonsListe = new List <Lessons>(); List <Lessons> lessonsAll = db.Lessons.ToList(); foreach (var course in courseList) { foreach (var lesson in lessonsAll.Where(l => l.CourseID == course.CourseID)) { LessonApiVM lessonApiVM = new LessonApiVM { courseId = lesson.CourseID, day = lesson.Day.ToString(), duration = lesson.Duration.ToString(), startHour = lesson.HourStart.ToString("HH'h'mm") }; instructorApiVM.schedule.Add(lessonApiVM); } } //foreach (Lessons item in lessonsListe) //{ // LessonApiVM lessonApiVM = new LessonApiVM // { // courseId = item.CourseID, // day = item.Day.ToString(), // duration = item.Duration.ToString(), // startHour = item.HourStart.ToString("HH'h'mm") // }; // instructorApiVM.schedule.Add(lessonApiVM); //} return(Ok(instructorApiVM)); }