// GET: api/students public IEnumerable <Student> GetStudents() { using (SchoolDBContext dbContext = new SchoolDBContext()) { // list for valid students List <Student> students = new List <Student>(); // dataContext var people = dbContext.People; // build students' list foreach (var person in people) { Student student = new Student(); if (person.Discriminator.Equals("Student")) { student.StudentId = person.PersonID; student.FirstName = person.FirstName; student.LastName = person.LastName; // For a student's GPA, call the library to calculate gpa with StudentGrades data CalculateGpa calculateGpa = new CalculateGpa(person.StudentGrades); student.Gpa = calculateGpa.CalStudentGpa(); students.Add(student); } } return(students); } }
// GET: api/student/{studentId} public HttpResponseMessage GetStudentTranscriptById(int studentId) { using (SchoolDBContext dbContext = new SchoolDBContext()) { var person = dbContext.People.Find(studentId); // Not found if (person == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Student with ID " + studentId.ToString() + " does not exist")); } // Not a student if (!person.Discriminator.Equals("Student")) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "ID " + studentId.ToString() + " is not for a student")); } // create a StudentTranscript StudentTranscript studentTranscript = new StudentTranscript(); // student information for StudentTranscript Student student = new Student { StudentId = studentId, FirstName = person.FirstName, LastName = person.LastName }; // For a student's GPA, call the library to calculate gpa with StudentGrades data CalculateGpa calculateGpa = new CalculateGpa(person.StudentGrades); student.Gpa = calculateGpa.CalStudentGpa(); // Create Grades' list List <CourseGrade> courseGrades = new List <CourseGrade>(); foreach (var g in person.StudentGrades) { // except NULL grades if (g.Grade != null) { // Course Info var course = dbContext.Courses.FirstOrDefault(e => e.CourseID == g.CourseID); CourseGrade cGrade = new CourseGrade { CourseId = course.CourseID, Title = course.Title, Credits = course.Credits, Grade = (decimal)g.Grade }; courseGrades.Add(cGrade); } } // store and return the transcript studentTranscript.Student = student; studentTranscript.Grades = courseGrades; return(Request.CreateResponse(HttpStatusCode.OK, studentTranscript)); } }