/// <summary> /// This method will get all items and then do the filter and sorting in memory. /// </summary> /// <param name="parentId">The specific parent to search the child items for.</param> /// <param name="details">The search details</param> /// <param name="offset">The number of items that will be skipped in result.</param> /// <param name="limit">The maximum number of items to return.</param> /// <param name="cancellationToken">Propagates notification that operations should be canceled</param> /// <returns>A page of the found items.</returns> /// <remarks>If your persistence layer supports search, then avoid using this, at it always reads all the items.</remarks> public async Task <PageEnvelope <TModel> > SearchChildrenAsync(TId parentId, SearchDetails <TModel> details, int offset, int?limit = null, CancellationToken cancellationToken = default) { limit = limit ?? PageInfo.DefaultLimit; InternalContract.RequireNotNull(details, nameof(details)); InternalContract.RequireValidated(details, nameof(details)); InternalContract.RequireGreaterThanOrEqualTo(0, offset, nameof(offset)); InternalContract.RequireGreaterThan(0, limit.Value, nameof(limit)); var allItems = (await _service.ReadChildrenAsync(parentId, int.MaxValue, cancellationToken)) .ToList(); var list = SearchHelper.FilterAndSort(allItems, details) .Skip(offset) .Take(limit.Value); var page = new PageEnvelope <TModel>(offset, limit.Value, allItems.Count(), list); return(page); }
public async Task ReadChildrenTest(ICrudManyToOne <Person, Guid> restClient, string resourceName) { var parentId = Guid.NewGuid(); var expectedUri = $"{ResourcePath}/Persons/{parentId}/{resourceName}?limit={int.MaxValue}"; HttpClientMock.Setup(client => client.SendAsync( It.Is <HttpRequestMessage>(request => request.RequestUri.AbsoluteUri == expectedUri && request.Method == HttpMethod.Get), CancellationToken.None)) .ReturnsAsync((HttpRequestMessage r, CancellationToken c) => CreateResponseMessage(r, new[] { _person })) .Verifiable(); var persons = await restClient.ReadChildrenAsync(parentId); Assert.IsNotNull(persons); var personArray = persons as Person[] ?? persons.ToArray(); Assert.AreEqual(1, personArray.Length); Assert.AreEqual(_person, personArray.FirstOrDefault()); HttpClientMock.Verify(); }
public async Task ReadChildrenTest() { var parentId = Guid.NewGuid(); var expectedUri = $"{ResourcePath}/Persons/{parentId}/Addresses?limit={int.MaxValue}"; HttpClientMock.Setup(client => client.SendAsync( It.Is <HttpRequestMessage>(request => IsExpectedRequest(request, expectedUri, HttpMethod.Get)), CancellationToken.None)) .ReturnsAsync((HttpRequestMessage r, CancellationToken c) => CreateResponseMessage(r, new[] { _address })) .Verifiable(); var addresses = await _client.ReadChildrenAsync(parentId); Assert.IsNotNull(addresses); var addressArray = addresses as Address[] ?? addresses.ToArray(); Assert.AreEqual(1, addressArray.Length); Assert.AreEqual(_address, addressArray.FirstOrDefault()); HttpClientMock.Verify(); }