public void APIStudent_Success() { int testID = 100; string testLastName = "Petrolia"; string testFirstName = "Matoula"; DateTime testEnrollmentDate = DateTime.Now; Enrollment testEnrollment1 = new Enrollment() { EnrollmentID = 1, StudentID = testID, CourseID = 100 }; Enrollment testEnrollment2 = new Enrollment() { EnrollmentID = 2, StudentID = testID, CourseID = 200 }; List <Enrollment> testEnrollments = new List <Enrollment>(); testEnrollments.Add(testEnrollment1); testEnrollments.Add(testEnrollment2); EntityGenerator generator = new EntityGenerator(dbContext); Student studentTest = generator.CreateStudentFull(testID, testLastName, testFirstName, testEnrollmentDate, testEnrollments); //Create a list of enrollments - only Course ID List <EnrollmentApiVM> testEnrollmentsAPI = new List <EnrollmentApiVM>(); foreach (Enrollment enrollment in studentTest.Enrollments) { EnrollmentApiVM enrollmentApiVM = new EnrollmentApiVM(); enrollmentApiVM.courseId = enrollment.CourseID; testEnrollmentsAPI.Add(enrollmentApiVM); } IHttpActionResult okResult = controllerToTest.GetStudent(studentTest.ID); OkNegotiatedContentResult <StudentApiVM> contentResult = okResult as OkNegotiatedContentResult <StudentApiVM>; // string to test the format - not used //string expectedResult = "{\"id\":" + studentTest.ID + ",\"lastname\":" + studentTest.LastName + ",\"firstname\":" + studentTest.FirstMidName + // ",\"enrollmentDate\":" + studentTest.EnrollmentDate.ToString("yyyy-MM-dd") + ",\"enrollments\":[{\"CourseId\":" + testEnrollmentsAPI[0].CourseId + // "},{\"CourseId\":" + testEnrollmentsAPI[1].CourseId + "}]}"; Assert.IsNotNull(contentResult); Assert.AreEqual(studentTest.LastName, contentResult.Content.lastname); Assert.AreEqual(studentTest.FirstMidName, contentResult.Content.firstname); Assert.AreEqual(studentTest.ID, contentResult.Content.id); Assert.AreEqual(studentTest.EnrollmentDate.ToString("yyyy-MM-dd"), contentResult.Content.enrollmentDate); Assert.IsNotNull(contentResult.Content.enrollments); Assert.IsNotEmpty(contentResult.Content.enrollments); Assert.AreEqual(testEnrollmentsAPI[0], contentResult.Content.enrollments[0]); Assert.AreEqual(testEnrollmentsAPI[1], contentResult.Content.enrollments[1]); Assert.AreEqual(testEnrollmentsAPI, contentResult.Content.enrollments); Assert.IsInstanceOf(typeof(OkNegotiatedContentResult <StudentApiVM>), okResult); }
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 GetStudent(int id) { // find instead of any: any searches the database, find searches the context if (db.People.Find(id) is Instructor) //if (db.Instructors.Any(x => x.ID == id)) { return(NotFound()); } Student student = db.Students.Find(id); //Student student = db.Students.FirstOrDefault(s => s.ID == id); if (student == null) { return(NotFound()); } //List<Enrollment> enrollments = db.Enrollments.Where(s => s.StudentID == id).ToList(); List <EnrollmentApiVM> CourseIdList = new List <EnrollmentApiVM>(); StudentApiVM studentApiVM = new StudentApiVM(); foreach (Enrollment enrollment in student.Enrollments) { EnrollmentApiVM enrollmentApiVM = new EnrollmentApiVM(); enrollmentApiVM.courseId = enrollment.CourseID; CourseIdList.Add(enrollmentApiVM); } studentApiVM.id = student.ID; studentApiVM.lastname = student.LastName; studentApiVM.firstname = student.FirstMidName; studentApiVM.enrollmentDate = student.EnrollmentDate.ToString(CONSTANTS.SHORT_DATE); studentApiVM.enrollments = CourseIdList; //return ViewModel return(Ok(studentApiVM)); }