public async Task <PagedList <RideRequestDto> > Handle(RideRequestListQuery request, CancellationToken cancellationToken)
            {
                if (request.QueryParameters == null)
                {
                    // log error
                    throw new ApiException("Invalid query parameters.");
                }

                var collection = _db.RideRequests
                                 as IQueryable <RideRequest>;

                var sieveModel = new SieveModel
                {
                    Sorts   = request.QueryParameters.SortOrder ?? "RideRequestId",
                    Filters = request.QueryParameters.Filters
                };

                var appliedCollection = _sieveProcessor.Apply(sieveModel, collection);
                var dtoCollection     = appliedCollection
                                        .ProjectTo <RideRequestDto>(_mapper.ConfigurationProvider);

                return(await PagedList <RideRequestDto> .CreateAsync(dtoCollection,
                                                                     request.QueryParameters.PageNumber,
                                                                     request.QueryParameters.PageSize));
            }
Exemplo n.º 2
0
        public async Task <IActionResult> GetRideRequests([FromQuery] RideRequestParametersDto rideRequestParametersDto)
        {
            // add error handling
            var query         = new RideRequestListQuery(rideRequestParametersDto);
            var queryResponse = await _mediator.Send(query);

            var paginationMetadata = new
            {
                totalCount        = queryResponse.TotalCount,
                pageSize          = queryResponse.PageSize,
                currentPageSize   = queryResponse.CurrentPageSize,
                currentStartIndex = queryResponse.CurrentStartIndex,
                currentEndIndex   = queryResponse.CurrentEndIndex,
                pageNumber        = queryResponse.PageNumber,
                totalPages        = queryResponse.TotalPages,
                hasPrevious       = queryResponse.HasPrevious,
                hasNext           = queryResponse.HasNext
            };

            Response.Headers.Add("X-Pagination",
                                 JsonSerializer.Serialize(paginationMetadata));

            var response = new Response <IEnumerable <RideRequestDto> >(queryResponse);

            return(Ok(response));
        }
        public async Task RideRequestListQuery_Returns_Sorted_RideRequest_RideType_List_In_Desc_Order()
        {
            //Arrange
            var fakeRideRequestOne = new FakeRideRequest {
            }.Generate();
            var fakeRideRequestTwo = new FakeRideRequest {
            }.Generate();

            fakeRideRequestOne.RideType = "bravo";
            fakeRideRequestTwo.RideType = "alpha";
            var queryParameters = new RideRequestParametersDto()
            {
                SortOrder = "RideType"
            };

            await InsertAsync(fakeRideRequestOne, fakeRideRequestTwo);

            //Act
            var query        = new RideRequestListQuery(queryParameters);
            var rideRequests = await SendAsync(query);

            // Assert
            rideRequests
            .FirstOrDefault()
            .Should().BeEquivalentTo(fakeRideRequestTwo, options =>
                                     options.ExcludingMissingMembers());
            rideRequests
            .Skip(1)
            .FirstOrDefault()
            .Should().BeEquivalentTo(fakeRideRequestOne, options =>
                                     options.ExcludingMissingMembers());
        }
        public async Task RideRequestListQuery_Filters_RideRequest_IsEco()
        {
            //Arrange
            var fakeRideRequestOne = new FakeRideRequest {
            }.Generate();
            var fakeRideRequestTwo = new FakeRideRequest {
            }.Generate();

            fakeRideRequestOne.IsEco = false;
            fakeRideRequestTwo.IsEco = true;
            var queryParameters = new RideRequestParametersDto()
            {
                Filters = $"IsEco == {fakeRideRequestTwo.IsEco}"
            };

            await InsertAsync(fakeRideRequestOne, fakeRideRequestTwo);

            //Act
            var query        = new RideRequestListQuery(queryParameters);
            var rideRequests = await SendAsync(query);

            // Assert
            rideRequests.Should().HaveCount(1);
            rideRequests
            .FirstOrDefault()
            .Should().BeEquivalentTo(fakeRideRequestTwo, options =>
                                     options.ExcludingMissingMembers());
        }
        public async Task RideRequestListQuery_Returns_Resource_With_Accurate_Props()
        {
            // Arrange
            var fakeRideRequestOne = new FakeRideRequest {
            }.Generate();
            var fakeRideRequestTwo = new FakeRideRequest {
            }.Generate();
            var queryParameters = new RideRequestParametersDto();

            await InsertAsync(fakeRideRequestOne, fakeRideRequestTwo);

            // Act
            var query        = new RideRequestListQuery(queryParameters);
            var rideRequests = await SendAsync(query);

            // Assert
            rideRequests.Should().HaveCount(2);
        }
        public async Task RideRequestListQuery_Returns_Expected_Page_Size_And_Number()
        {
            //Arrange
            var fakeRideRequestOne = new FakeRideRequest {
            }.Generate();
            var fakeRideRequestTwo = new FakeRideRequest {
            }.Generate();
            var fakeRideRequestThree = new FakeRideRequest {
            }.Generate();
            var queryParameters = new RideRequestParametersDto()
            {
                PageSize = 1, PageNumber = 2
            };

            await InsertAsync(fakeRideRequestOne, fakeRideRequestTwo, fakeRideRequestThree);

            //Act
            var query        = new RideRequestListQuery(queryParameters);
            var rideRequests = await SendAsync(query);

            // Assert
            rideRequests.Should().HaveCount(1);
        }