Beispiel #1
0
        public async Task <PageOfAddressesResponse> Handle(GetAddressesPageQuery request, CancellationToken cancellationToken)
        {
            IEnumerable <Address> addressesList;
            long addressesAmount;

            Expression <Func <Address, bool> > filter;

            if (request.City != null && request.Street != null)
            {
                filter = x => x.City == request.City && x.Street == request.Street;
            }
            else if (request.City != null)
            {
                filter = x => x.City == request.City;
            }
            else if (request.Street != null)
            {
                filter = x => x.Street == request.Street;
            }
            else
            {
                filter = x => true;
            }

            addressesList = await _addressRepo.FindWithPaging(filter, request.PageNo <= 0? 1 : request.PageNo, _apiSpecificSettings.PagingPageSize);

            addressesAmount = await _addressRepo.Count(filter);

            var totalPages = (int)Math.Ceiling(addressesAmount / (double)_apiSpecificSettings.PagingPageSize);

            var pageOfAddressesResponse = new PageOfAddressesResponse()
            {
                CurrentPage = request.PageNo <= 0 ? 1: request.PageNo,
                TotalPages  = totalPages,
                Items       = new List <AddressResponse>()
            };

            //maping
            foreach (var item in addressesList)
            {
                pageOfAddressesResponse.Items.Add(new AddressResponse {
                    Id = item.Id, Name = item.Name, City = item.City, Street = item.Street
                });
            }

            return(pageOfAddressesResponse);
        }