Ejemplo n.º 1
0
        public async Task <GetAllCityResponseDto> GetAllAsync(PagingDto pagingDto)
        {
            if (pagingDto.RecordsPerPage > 100)
            {
                return new GetAllCityResponseDto {
                           Success = false, Message = "The maximum number of records per page is 100."
                }
            }
            ;

            var citiesTask = CityRepository.GetAllAsync(pagingDto.RecordsPerPage, pagingDto.Page);
            var countTask  = CityRepository.CountAsync();

            await Task.WhenAll(citiesTask, countTask);

            var totalPages = Math.DivRem(countTask.Result, pagingDto.RecordsPerPage, out int remainder);

            pagingDto.TotalRecords = countTask.Result;
            pagingDto.TotalPages   = remainder > 0 ? totalPages + 1 : totalPages;

            return(new GetAllCityResponseDto
            {
                Data = citiesTask.Result.Select(city => new CityDto
                {
                    Key = city.Key.ToString(),
                    Name = city.Name,
                    PostalCode = city.PostalCode,
                    CreatedOn = city.CreatedOn.ToString("s")
                }).ToList(),
                Paging = pagingDto
            });
        }
        public async Task OnGetAllAsyncCountOdd()
        {
            var count  = 11;
            var paging = new PagingDto {
                RecordsPerPage = 2
            };
            var cities = Builder <City> .CreateListOfSize(paging.RecordsPerPage).Build();

            CityRepository.CountAsync().Returns(count);
            CityRepository.GetAllAsync(Arg.Any <int>(), Arg.Any <int>()).Returns(cities);

            var result = await CityService.GetAllAsync(paging);

            result.Success.Should().BeNull();
            result.Data.Should().HaveCount(paging.RecordsPerPage);
            result.Paging.TotalPages.Should().Be((count / paging.RecordsPerPage) + 1);
        }