コード例 #1
0
        public async Task <IEnumerable <Entity> > GetPagedEmployeeReponseAsync(GetEmployeesQuery requestParameter)
        {
            IQueryable <Employee> result;

            var employeeNumber = requestParameter.EmployeeNumber;
            var employeeTitle  = requestParameter.EmployeeTitle;
            var orderBy        = requestParameter.OrderBy;
            var fields         = requestParameter.Fields;
            var pageNumber     = requestParameter.PageNumber;
            var pageSize       = requestParameter.PageSize;

            result = _mockData.GetEmployees(1000)
                     .AsQueryable()
                     .Skip((pageNumber - 1) * pageSize)
                     .Take(pageSize);

            //filter
            FilterByColumn(ref result, employeeNumber, employeeTitle);
            if (!string.IsNullOrWhiteSpace(orderBy))
            {
                result = result.OrderBy(orderBy);
            }

            //limit query fields
            if (!string.IsNullOrWhiteSpace(fields))
            {
                result = result.Select <Employee>("new(" + fields + ")");
            }
            // retrieve data to list
            // var resultData = await result.ToListAsync();
            // Note: Bogus library does not support await for AsQueryable.
            // Workaround:  fake await with Task.Run and use regular ToList
            var resultData = await Task.Run(() => result.ToList());

            // shape data
            return(_dataShaper.ShapeData(resultData, fields));
        }
コード例 #2
0
        public async Task <(IEnumerable <Entity> data, RecordsCount recordsCount)> GetPagedEmployeeReponseAsync(GetEmployeesQuery requestParameter)
        {
            IQueryable <Employee> result;

            var employeeNumber = requestParameter.EmployeeNumber;
            var employeeTitle  = requestParameter.EmployeeTitle;

            var pageNumber = requestParameter.PageNumber;
            var pageSize   = requestParameter.PageSize;
            var orderBy    = requestParameter.OrderBy;
            var fields     = requestParameter.Fields;

            int recordsTotal, recordsFiltered;

            int seedCount = 1000;

            result = _mockData.GetEmployees(seedCount)
                     .AsQueryable();

            // Count records total
            recordsTotal = result.Count();

            // filter data
            FilterByColumn(ref result, employeeNumber, employeeTitle);

            // Count records after filter
            recordsFiltered = result.Count();

            //set Record counts
            var recordsCount = new RecordsCount
            {
                RecordsFiltered = recordsFiltered,
                RecordsTotal    = recordsTotal
            };

            // set order by
            if (!string.IsNullOrWhiteSpace(orderBy))
            {
                result = result.OrderBy(orderBy);
            }

            //limit query fields
            if (!string.IsNullOrWhiteSpace(fields))
            {
                result = result.Select <Employee>("new(" + fields + ")");
            }
            // paging
            result = result
                     .Skip((pageNumber - 1) * pageSize)
                     .Take(pageSize);

            // retrieve data to list
            // var resultData = await result.ToListAsync();
            // Note: Bogus library does not support await for AsQueryable.
            // Workaround:  fake await with Task.Run and use regular ToList
            var resultData = await Task.Run(() => result.ToList());

            // shape data
            var shapeData = _dataShaper.ShapeData(resultData, fields);

            return(shapeData, recordsCount);
        }