Ejemplo n.º 1
0
        public ActionResult <string> GetStudentsFromStudyBuddySearch([FromQuery] StudyBuddySearchDTO searchParams)
        {
            string id = GetUserIdFromToken();
            IEnumerable <StudentListItemDTO> student = _studentService.GetStudentFromStudyBuddySearch(searchParams, id);

            return(Ok(student));
        }
        public IEnumerable<StudentListItemDTO> GetStudentFromStudyBuddySearch(StudyBuddySearchDTO searchParams, string loggedInUserId)
        {
            if (searchParams == null)
                throw new ParameterException("Search parameters cannot be null");
            if (searchParams.GoodInDiscipline < 0 || searchParams.GoodInDiscipline > Enum.GetValues(typeof(SubjectType)).Length)
                throw new ParameterException("Discipline value is invalid");
            if (searchParams.ComletedWithGrade < 0 || searchParams.ComletedWithGrade > 5)
                throw new ParameterException("Grade value is invalid");
            if (searchParams.SubjectID != "null" && !Guid.TryParse(searchParams.SubjectID, out Guid guid))
                throw new ParameterException("Subject ID is invalid");
            if (searchParams.IsAlreadyCompleted && searchParams.IsCurrentlyEnrolledTo)
                throw new ParameterException("Search parameters are invalid, already completed and currently enrolled to cant have true value at the same time.");

            List<Student> results = new List<Student>();

            if (!(searchParams.SubjectID == null && searchParams.GoodInDiscipline == 0))
            {
                if (searchParams.SubjectID != null)
                {
                    if (!searchParams.IsAlreadyCompleted && !searchParams.IsCurrentlyEnrolledTo && !searchParams.IsCommonCourse)
                        results = _studentRepository.GetStudentsEnrolledToSubject(searchParams.SubjectID).ToList();

                    string currentSemester = SemesterManager.GetCurrentSemester();

                    if (searchParams.IsCommonCourse)
                    {
                        results = _studentRepository.GetStudentsEnrolledToSubjectAndHavingCurrentlyCommonCourse(loggedInUserId, searchParams.SubjectID, currentSemester).ToList();
                    }

                    if (searchParams.IsCurrentlyEnrolledTo)
                    {
                        if (results.Count != 0)
                        {
                            results = GetStudentsEnrolledToSubjectCurrently(searchParams.IsAttendingToAnotherTeacher, loggedInUserId, currentSemester, searchParams.SubjectID, results);
                        }
                        else if (searchParams.IsAttendingToAnotherTeacher)
                            results = _studentRepository.GetStudentsCurrentlyEnrolledToSubjectWithStudentButHavingAnotherCurseTeacher(loggedInUserId, searchParams.SubjectID, currentSemester).ToList();
                        else
                            results = _studentRepository.GetStudentsEnrolledToSubjectInSemester(searchParams.SubjectID, currentSemester).ToList();
                    }
                    if (searchParams.IsAlreadyCompleted)
                    {
                        if (results.Count != 0)
                        {
                            results = GetStudentsAlreadyCompletedSubjectFromStudentList(searchParams.ComletedWithGrade, searchParams.SubjectID, results);
                        }
                        else if (searchParams.ComletedWithGrade != 0)
                            results = _studentRepository.GetStudentsCompletedSubjectWithGrade(searchParams.SubjectID, searchParams.ComletedWithGrade).ToList();
                        else
                            results = _studentRepository.GetStudentsCompletedSubject(searchParams.SubjectID).ToList();
                    }

                }
                if (searchParams.GoodInDiscipline != 0)
                {
                    double betterThanAvg = 3.5;
                    if (results.Count() != 0)
                        results = GetStudentsGoodInDisciplineFromStudentList(results, searchParams.GoodInDiscipline, betterThanAvg);
                    else
                        results = _studentRepository.GetStudentsGoodInDiscipline(searchParams.GoodInDiscipline, betterThanAvg).ToList();
                }


            }
            return results.Select(x => MapStudent.MapStudentDBModelToStudentListItemDTO(x));
        }