private IQueryable <Person> GetFilteredPersonRecords(CustomStoreRequestModel request)
        {
            var dbPersons = _applicationDbContext.Persons.AsQueryable();

            if (!dbPersons.Any())
            {
                return(dbPersons);
            }

            if (!string.IsNullOrEmpty(request.FilterValue))
            {
                var filterProperties       = typeof(Person).GetProperties().Where(property => !property.Name.Equals("Id"));
                var genderFilterProperties = typeof(Person).GetProperties().Where(property => property.Name.Equals("Gender"));

                dbPersons = dbPersons.Where(person => filterProperties.Any(prop => (GetFormattedStringFromObject(prop.GetValue(person, null)).ToLowerInvariant()).Contains(request.FilterValue.ToLowerInvariant())) ||
                                            genderFilterProperties.Any(prop => GetFormattedStringFromObject(prop.GetValue(person, null)).ToLowerInvariant().Contains(request.FilterValue.ToLowerInvariant())));
            }

            if (request.Sort != null)
            {
                dbPersons = request.Sort.Desc ? dbPersons.OrderByDescending(request.Sort.Field) : dbPersons.OrderBy(request.Sort.Field);
            }
            else
            {
                dbPersons = dbPersons.OrderByDescending(per => per.LastName);
            }

            return(dbPersons);
        }
Exemple #2
0
        public async Task <IActionResult> GetPersonRecords([FromQuery] CustomStoreRequestModel requestModel)
        {
            var result = await _personService.GetPersonRecords(requestModel);

            if (result.IsSuccess)
            {
                return(new OkObjectResult(result.Data));
            }

            return(StatusCode(500, result.ErrorMessage));
        }
        public async Task <BaseResponseCustomStoreModel <PersonModel> > GetPersonRecords(CustomStoreRequestModel request)
        {
            var response = await ProcessRequestAsync(async() =>
            {
                var count     = await _applicationDbContext.Persons.CountAsync();
                var dbPersons = GetFilteredPersonRecords(request);
                var dbItems   = await dbPersons.Skip(request.Skip).Take(request.Take).ToListAsync();
                var items     = Mapper.Map <List <PersonModel> >(dbItems);

                return(new BaseResponseCustomStoreModel <PersonModel>(items, count));
            });

            return(response);
        }