public async Task <SearchHospitalsResponse> SearchHospitals(SearchHospitalsRequest request) { if (request.PageIndex != null || request.ItemsPerPage != null) { var paged = await SearchAzureHospitals(request.ItemsPerPage, request.PageIndex); return(paged.ToSearchHospitalsResponse()); } var illnesses = await SearchAzureHospitals(null, null); var list = new List <HospitalObject>(); var next = illnesses._links?.Next?.Href; var items = illnesses._embedded.Hospitals; list.AddRange(items); while (!string.IsNullOrWhiteSpace(next)) { illnesses = await SearchAzureHospitals(next); next = illnesses._links?.Next?.Href; var nextItems = illnesses._embedded.Hospitals; list.AddRange(nextItems); } return(new SearchHospitalsResponse { Data = list.ToHospitalDetailsList(), CurrentPageIndex = 0, PrevPageIndex = null, NextPageIndex = null }); }
public async void SearchHospitals_Should_Call_IApiRequestProvider_CreateGetRequest(int?itemsPerPage, int?pageIndex, string expectedLimit, string expectedPage) { var request = new HttpRequestMessage(); _requestProvider.CreateGetRequest(Arg.Any <string>()).Returns(request); var details = new SearchHospitalsRequest { ItemsPerPage = itemsPerPage, PageIndex = pageIndex }; await _repository.SearchHospitals(details); var query = new List <string>(); if (expectedLimit != null) { query.Add($"limit={expectedLimit}"); } if (expectedPage != null) { query.Add($"page={expectedPage}"); } var queryString = query.Any() ? $"?{string.Join("&", query)}" : string.Empty; var url = $"{_database}/hospitals{queryString}"; _requestProvider.Received(1).CreateGetRequest(url); request.Dispose(); }
public async void SearchHospitals_Should_Call_IRequestProvider_CreateGetRequest_Until_There_Are_No_More_Pages() { var request = new HttpRequestMessage(); _requestProvider.CreateGetRequest(Arg.Any <string>()).Returns(request); var embedded = new HospitalsObject { Hospitals = new [] { new HospitalObject { } } }; var withNext = new LinksObject { Next = new LinkObject { Href = "nextUrl" } }; var withoutNext = new LinksObject { Next = new LinkObject() }; var response1 = new HospitalsResponse { _embedded = embedded, _links = withNext }; var response2 = new HospitalsResponse { _embedded = embedded, _links = withNext }; var response3 = new HospitalsResponse { _embedded = embedded, _links = withoutNext }; _apiClient.Send(Arg.Any <HttpRequestMessage>(), Arg.Any <Func <HttpResponseMessage, Task <HospitalsResponse> > >()).Returns(response1, response2, response3); var url = $"{_database}/hospitals"; var search = new SearchHospitalsRequest(); await _repository.SearchHospitals(search); _requestProvider.Received(1).CreateGetRequest(url); _requestProvider.Received(2).CreateGetRequest(withNext.Next.Href); request.Dispose(); }
public async void FindHelp_Should_Call_IHospitalRepository_SearchHospitals() { SearchHospitalsRequest arg = null; _hospitalRepository .SearchHospitals(Arg.Do <SearchHospitalsRequest>(a => arg = a)) .Returns(new SearchHospitalsResponse { Data = new HospitalDetails[0] }); var request = new FindHelpRequest { PageIndex = 123 }; await _service.FindHelp(request); await _hospitalRepository.Received(1).SearchHospitals(Arg.Any <SearchHospitalsRequest>()); arg.ItemsPerPage.Should().BeNull(); arg.HospitalName.Should().BeNull(); arg.PageIndex.Should().BeNull(); }