Ejemplo n.º 1
0
        public async Task <ActionResult <StudentTranscript> > GetTranscript(int id)
        {
            _log.LogInformation($"Get:/students/{id}/transcript: Getting Transcript");

            ServiceResponse <StudentTranscript> response = await _studentsService.GetTranscript(id);

            if (!response.Success)
            {
                _log.LogWarning($"Get:/students/{id}/transcript: {response.Message??"validation failed"}");

                return(NotFound(new NotFoundResponse($"{response.Message ?? "validation failed"}")));
            }

            StudentTranscript transcript = response.Data as StudentTranscript;

            if (transcript == null)
            {
                _log.LogWarning($"Get:/students/{id}/transcript: The Student Exists but we were unable to build a transcript for unknown reasons");

                return(BadRequest(new BadRequestResponse($"Unable to find a student transcript for Student Id {id}")));
            }

            _log.LogInformation($"Get:/students/{id}/transcript: Returning Transcript");

            return(Ok(transcript));
        }
        public async Task StudentsController_Get_StudentTranscript_NotFound()
        {
            int id = 2;

            ServiceResponse <StudentTranscript> response = new ServiceResponse <StudentTranscript>
            {
                Data    = null,
                Success = false,
                Message = $"Get:/students/{id}/transcript: Student does not exist"
            };

            _mockStudentsService.Setup(service => service.GetTranscript(id)).Returns(Task.Run(() => { return(response); }));


            // Act
            var result = await _controller.GetTranscript(id) as ActionResult <StudentTranscript>;

            StudentTranscript transcript = result.Value as StudentTranscript;

            // Assert
            Assert.True(result != null, "StudentsController.GetStudentTranscript should not return a null response");
            Assert.IsType <NotFoundObjectResult>(result.Result);

            // Validate that it is sending School.API.Utility.NotFoundResponse
            Assert.IsType <School.API.Utility.NotFoundResponse>(((Microsoft.AspNetCore.Mvc.ObjectResult)result.Result).Value);
        }
        public static List <StudentTranscript> GetTranscript(string username)
        {
            List <StudentTranscript> stlist = new List <StudentTranscript>();

            try
            {
                string sql = "select cc.CourseNum, cc.Grade, cc.SemesterId as Semester from CoursesCompleted cc "
                             + "inner join Users u on cc.StudentId=u.StudentId where u.UserName=@UserName";
                List <SqlParameter> plist = new List <SqlParameter>();
                DBHelper.addSqlParam(plist, "@UserName", SqlDbType.VarChar, username, 50);
                DataTable dt = DataAccess.GetManyRowsCols(sql, plist);
                foreach (DataRow dr in dt.Rows)
                {
                    StudentTranscript st = new StudentTranscript();
                    st.CourseNum = dr["CourseNum"].ToString();
                    st.Grade     = (double)dr["Grade"];
                    st.Semester  = dr["Semester"].ToString();
                    stlist.Add(st);
                }
            }
            catch (Exception)
            {
                throw;
            }
            return(stlist);
        }
        public async Task StudentsController_Get_StudentTranscript_Success()
        {
            int id = 2;

            // Arrange
            _mockStudentsService.Setup(service => service.GetTranscript(id)).Returns(Task.Run(() => StudentsServiceTests.CreateTranscript(id)));

            // Act
            var result = await _controller.GetTranscript(id) as ActionResult <StudentTranscript>;

            StudentTranscript transcript = ((Microsoft.AspNetCore.Mvc.ObjectResult)result.Result).Value as StudentTranscript;

            // Assert
            Assert.IsType <OkObjectResult>(result.Result);
            Assert.True(transcript != null);
            Assert.True(transcript.studentId == id);
        }
Ejemplo n.º 5
0
        // 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));
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Returns the Transcript for a Student based on the Id value provided.
        /// I suggest calling StudentExists(id) prior to calling this method to verify
        /// that the student exists in this school.  However, this method will return a null
        /// if the student does not exist.
        /// </summary>
        /// <param name="id">Student Id</param>
        /// <returns>StudentTranscript (or null if not found).</returns>
        public async Task <ServiceResponse <StudentTranscript> > GetTranscript(int id)
        {
            _log.LogDebug("StudentService.GetTranscript START");

            if (!StudentExists(id))
            {
                string errorMessage = $"Get:/students/{id}/transcript: Student does not exist";

                return(new ServiceResponse <StudentTranscript>
                {
                    Data = null,
                    Success = false,
                    Message = errorMessage
                });
            }

            DbSet <Person>       personTable       = _uow.Context.Person;
            DbSet <StudentGrade> studentGradeTable = _uow.Context.StudentGrade;
            DbSet <Course>       courseTable       = _uow.Context.Course;

            var query = await(
                from student in personTable
                join studentGrade in studentGradeTable on student.PersonId equals studentGrade.StudentId
                where student.PersonId == id
                select new StudentTranscript
            {
                studentId = student.PersonId,
                firstName = student.FirstName,
                lastName  = student.LastName,
                gpa       = null,
                grades    = null
            }
                ).ToListAsync();

            StudentTranscript transcript = query?.FirstOrDefault();

            if (transcript != null)
            {
                _log.LogDebug($"StudentsService: Generating the list of grades for each student...");

                //
                // Generate the Grades List for this student which is needed to calculate the GPA below.
                //
                transcript.grades = await(from studentGrade in studentGradeTable
                                          join course in courseTable on studentGrade.CourseId equals course.CourseId
                                          where studentGrade.StudentId == id && studentGrade.Grade != null
                                          select new StudentTranscriptGrade
                {
                    courseId = course.CourseId,
                    title    = course.Title,
                    credits  = course.Credits,
                    grade    = studentGrade.Grade
                }).OrderBy(course => course.courseId).ToListAsync();

                _log.LogDebug($"StudentsService: Calculating GPA for this student");

                //
                // See massive comment regarding the method for calculating the GPA for Challenge 2.2
                //
                transcript.gpa = CalculateGPA(transcript.grades);

                _log.LogDebug($"StudentsService: Returning transcript for studentId = {id}");

                _log.LogDebug("StudentService.GetTranscript END 1");

                return(new ServiceResponse <StudentTranscript>
                {
                    Data = transcript,
                    Success = true
                });
            }

            //TODO: Rethink returning a null like this.
            // Perhaps return
            _log.LogError("StudentService.GetTranscript returning null - Perhaps this should be an exception??");

            return(new ServiceResponse <StudentTranscript>
            {
                Data = null,
                Success = false,
                Message = "Unable to gather a transcript for unknown reason"
            });
        }