public IHttpActionResult GetStudent(int id) { Student student = db.Students.Find(id); if (student == null) { return(NotFound()); } //Obtain list enrollment of a student List <Enrollment> enrollments = db.Enrollments.Where(s => s.StudentID == id).ToList(); //Create list of CoursID List <EnrollmentViewModel> CoursIDListe = new List <EnrollmentViewModel>(); //Create a object student from studentViewModel StudentApiViewModel studentViewModel = new StudentApiViewModel(); //Create a object enrollment from enrollmentViewModel EnrollmentViewModel enrollmentViewModel = new EnrollmentViewModel(); //implementation of the list EnrollmentViewModel with courseId foreach (Enrollment enrollment in enrollments) { enrollmentViewModel.courseId = enrollment.CourseID; CoursIDListe.Add(enrollmentViewModel); } studentViewModel.id = student.ID; studentViewModel.lastname = student.LastName; studentViewModel.firstname = student.FirstMidName; studentViewModel.enrollmentDate = student.EnrollmentDate.ToString("yyyy-MM-dd"); studentViewModel.enrollments = CoursIDListe; return(Ok(studentViewModel)); }
public IHttpActionResult Get(int id) { Student student = db.Students.FirstOrDefault(s => s.ID == id); //TODO : send a not found and not a ok if (student == null) { Dictionary <string, string> ErrorDict = new Dictionary <string, string> (); ErrorDict["Status"] = "Error"; ErrorDict["Message"] = "There is no student with that Id"; return(Ok(ErrorDict)); } StudentApiViewModel studentToSend = new StudentApiViewModel(); List <EnrollmentApiViewModel> coursesList = new List <EnrollmentApiViewModel>(); // TODO : Use modelDto for the enrollments foreach (Enrollment enrollment in student.Enrollments) { EnrollmentApiViewModel course = new EnrollmentApiViewModel(); course.courseId = enrollment.CourseID.ToString(); coursesList.Add(course); } studentToSend.id = student.ID; studentToSend.firstname = student.FirstMidName; studentToSend.lastname = student.LastName; studentToSend.enrollmentDate = student.EnrollmentDate.ToString("yyyy-MM-dd"); studentToSend.enrollments = coursesList; return(Ok(studentToSend)); }