public async Task <RetrieveBookingsResponse> RetrieveBookings(RetrieveBookingsRequest request) { RetrieveBookingsResponse response; IEnumerable <BookingDetails> items; using (var db = _provider.GetDatabase()) { var bookings = db.GetCollection <BookingDetails>("bookings"); items = bookings.FindAll(); } items = items.Sort(request.SortField, request.SortDescending); var itemsPerPage = request.ItemsPerPage ?? DEFAULT_ITEMS_PER_PAGE; var currentIndex = request.PageIndex ?? 0; var maxItems = itemsPerPage + (itemsPerPage * currentIndex); var paged = items.Skip(maxItems - itemsPerPage).Take(itemsPerPage); var lastPageIndex = items.Count() / itemsPerPage; var prevIndex = currentIndex > 0 ? currentIndex - 1 : (int?)null; var nextIndex = currentIndex < lastPageIndex ? currentIndex + 1 : (int?)null; response = new RetrieveBookingsResponse { Data = paged, CurrentPageIndex = currentIndex, PrevPageIndex = prevIndex, NextPageIndex = nextIndex }; return(await Task.FromResult(response)); }
public async void GetBookings_Should_Return_Correctly() { var response = new RetrieveBookingsResponse { CurrentPageIndex = 123, NextPageIndex = 456, PrevPageIndex = 789, Data = new [] { new BookingDetails() } }; _bittnRepository.RetrieveBookings(Arg.Any <RetrieveBookingsRequest>()).Returns(response); var actual = await _service.GetBookings(new GetBookingsRequest()); actual.NextPageIndex.Should().Be(response.NextPageIndex); actual.PrevPageIndex.Should().Be(response.PrevPageIndex); actual.Data.Should().BeEquivalentTo(response.Data); }