示例#1
0
        public async Task GetPaginatedDataAsync_Should_Return_Proper_Values(int itemsPerPage, int page, string search)
        {
            var expected = new BasePaginatedResponse <ProjectDto>
            {
                EntityList = ProjectDtoList,
                PagesCount = 1
            };
            var actual = await _projectService.GetProjects(new BaseQueryParameters { ItemsPerPage = itemsPerPage, Page = page, Search = search });

            await _projectRepository.Received(1).GetFilteredDataCountAsync(Arg.Any <string>());

            await _projectRepository.Received(1).PaginateFiltered(Arg.Any <int>(), Arg.Any <int>(), Arg.Any <string>());

            Assert.Equal(JsonConvert.SerializeObject(expected), JsonConvert.SerializeObject(actual));
        }
        public async Task <BasePaginatedResponse <ProjectDto> > GetProjects(BaseQueryParameters parameters)
        {
            BasePaginatedResponse <ProjectDto> projectsPaginated = null;

            if (parameters.Search == "")
            {
                projectsPaginated = await _redisService.GetItemAsync <BasePaginatedResponse <ProjectDto> >($"projects.{parameters.ItemsPerPage}.{parameters.Page}");
            }

            if (projectsPaginated == null)
            {
                var projectList = await _projectRepository.PaginateFiltered(
                    offset : (parameters.Page - 1) *parameters.ItemsPerPage,
                    itemsCount : parameters.ItemsPerPage,
                    searchPhrase : parameters.Search
                    );

                var projectDtoList = _mapper.Map <IEnumerable <Project>, IEnumerable <ProjectDto> >(projectList);
                var rowsCount      = await _projectRepository.GetFilteredDataCountAsync(parameters.Search);

                var pagesCount = (int)Math.Ceiling((decimal)rowsCount / parameters.ItemsPerPage);

                projectsPaginated = new BasePaginatedResponse <ProjectDto>
                {
                    EntityList = projectDtoList,
                    PagesCount = pagesCount
                };

                if (parameters.Search == "")
                {
                    await _redisService.SetItemAsync($"projects.{parameters.ItemsPerPage}.{parameters.Page}", projectsPaginated, 60);
                }
            }

            return(projectsPaginated);
        }