public async Task <IEnumerable <Student> > GetStudentsAsync(StudentDtoParameters parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            var queryExpression = _context.Students as IQueryable <Student>;

            if (!string.IsNullOrWhiteSpace(parameters.Name))
            {
                parameters.Name = parameters.Name.Trim();
                queryExpression = queryExpression.Where(x => x.StudentName == parameters.Name);
            }

            if (!string.IsNullOrWhiteSpace(parameters.SearchTerm))
            {
                parameters.SearchTerm = parameters.SearchTerm.Trim();
                queryExpression       = queryExpression.Where(x => x.StudentName.Contains(parameters.SearchTerm));
            }

            queryExpression = queryExpression.Skip(parameters.PageSize * (parameters.PageNumber - 1))
                              .Take(parameters.PageSize);

            return(await queryExpression.ToListAsync());
        }
Esempio n. 2
0
        //查询Student
        public async Task <IEnumerable <Student> > GetStudentsAsync(StudentDtoParameters studentDtoParameters)
        {
            if (studentDtoParameters == null)
            {
                throw new ArgumentNullException(nameof(studentDtoParameters));
            }
            //如果 筛选条件 和 查询字符串 都为空的话
            if (string.IsNullOrWhiteSpace(studentDtoParameters.StudentName) && string.IsNullOrWhiteSpace(studentDtoParameters.SearchTerm))
            {
                return(await _context.Students.ToListAsync());
            }

            var studentItems = _context.Students as IQueryable <Student>;

            //筛选条件
            if (!string.IsNullOrWhiteSpace(studentDtoParameters.StudentName))
            {
                studentDtoParameters.StudentName = studentDtoParameters.StudentName.Trim();

                studentItems = studentItems.Where(x => x.StudentName == studentDtoParameters.StudentName);
            }
            //查询条件
            if (!string.IsNullOrWhiteSpace(studentDtoParameters.SearchTerm))
            {
                studentDtoParameters.SearchTerm = studentDtoParameters.SearchTerm.Trim();

                studentItems = studentItems.Where(x => x.StudentName.Contains(studentDtoParameters.SearchTerm) ||
                                                  x.StudentCode.Contains(studentDtoParameters.SearchTerm));
            }

            return(await studentItems.ToListAsync());
        }
Esempio n. 3
0
        public async Task <ActionResult <IEnumerable <StudentDto> > > GetStudents([FromQuery] StudentDtoParameters studentDtoParameters)
        {
            var students = await _studentRepository.GetStudentsAsync(studentDtoParameters);

            foreach (var student in students)
            {
                student.Profession = await _professionRepository.GetProfessionAsync(student.ProfessionId);

                student.Profession.Academy = await _academyRepository.GetAcademyAsync(student.Profession.AcademyId);
            }
            var studentDtos = _mapper.Map <IEnumerable <StudentDto> >(students);

            return(Ok(studentDtos));
        }
Esempio n. 4
0
        public async Task <ActionResult <IEnumerable <StudentDto> > > GetStudents(
            Guid classId,
            [FromQuery] StudentDtoParameters parameters
            )
        {
            if (!await _classRepository.ClassExitAsync(classId))
            {
                return(NotFound());
            }

            var students = await _studentRepository.GetStudentsAsync(parameters);

            var studentDtos = _mapper.Map <IEnumerable <StudentDto> >(students);

            return(Ok(studentDtos));
        }