public IHttpActionResult Get(int id) { Instructor instructor = db.Instructors.FirstOrDefault(s => s.ID == id); //TODO : send a not found and not a ok if (instructor == null) { Dictionary <string, string> ErrorDict = new Dictionary <string, string>(); ErrorDict["Status"] = "Error"; ErrorDict["Message"] = "There is no instructor with that Id"; return(Ok(ErrorDict)); } InstructorApiViewModel instructorToSend = new InstructorApiViewModel(); instructorToSend.instructorId = instructor.ID; instructorToSend.schedule = new List <LessonApiViewModel>(); foreach (Lesson lesson in instructor.Lessons) { LessonApiViewModel enrollment = new LessonApiViewModel(); enrollment.courseId = lesson.CourseID; enrollment.day = lesson.Day.ToString(); enrollment.startHour = lesson.StartHour.ToString() + "h00"; enrollment.duration = lesson.Length * 60; instructorToSend.schedule.Add(enrollment); } return(Ok(instructorToSend)); }
public IHttpActionResult GetInstructor(int id, string weeklyschedule) { if (InstructorExists(id) == false) { return(NotFound()); } SchoolContext db = new SchoolContext(); InstructorApiViewModel instructorApiViewModel = new InstructorApiViewModel(); //initialization of an instructorVM instructorApiViewModel.instructorId = id; instructorApiViewModel.schedule = new List <CourseSessionApiViewModel>(); //initialization of his List of CourseSessionVM List <CourseSession> courseSessions = db.CourseSessions.Where(i => i.InstructorID == id).ToList(); //recovery of the list of this instructor's CourseSessions in the db foreach (CourseSession courseSession in courseSessions) //foreach of this CourseSession we create a CourseSsessionVM and we add it to the list of CourseSessionVM { CourseSessionApiViewModel session = new CourseSessionApiViewModel { courseId = courseSession.CourseID, day = courseSession.DayOfWeek.ToString(), startHour = $"{courseSession.HourStart.ToString()}h00", duration = ((courseSession.HourEnd - courseSession.HourStart) * 60).ToString() }; instructorApiViewModel.schedule.Add(session); } return(Ok(instructorApiViewModel)); //return of the instructor with his list of courseSessionsVM }