public async Task Then_Downloads_Are_Restricted_To_Twelve_Months_For_Reverse_Sort(
            ReverseOrderedApprenticeshipSearchParameters searchParameters,
            [Frozen] Mock <IProviderCommitmentsDbContext> mockContext)
        {
            //Arrange
            searchParameters.FieldName         = nameof(Apprenticeship.FirstName);
            searchParameters.PageNumber        = 0;
            searchParameters.ReverseSort       = false;
            searchParameters.Filters           = new ApprenticeshipSearchFilters();
            searchParameters.CancellationToken = CancellationToken.None;
            searchParameters.EmployerAccountId = null;


            var apprenticeships = GetTestApprenticeshipsWithAlerts(searchParameters);

            apprenticeships[1].ProviderRef = null;
            apprenticeships[1].EndDate     = DateTime.UtcNow.AddMonths(-13);;

            mockContext
            .Setup(context => context.Apprenticeships)
            .ReturnsDbSet(apprenticeships);

            var service = new ReverseOrderedApprenticeshipSearchService(mockContext.Object);

            //Act
            var actual = await service.Find(searchParameters);

            //Assert
            Assert.AreEqual(apprenticeships.Count - 1, actual.Apprenticeships.Count());
            Assert.IsFalse(actual.Apprenticeships.Contains(apprenticeships[1]));
        }
Ejemplo n.º 2
0
        public async Task <GetApprenticeshipsQueryResult> Handle(GetApprenticeshipsQuery query, CancellationToken cancellationToken)
        {
            var matchedApprenticeshipDetails = new List <GetApprenticeshipsQueryResult.ApprenticeshipDetails>();

            ApprenticeshipSearchResult searchResult;

            if (string.IsNullOrEmpty(query.SortField) || query.SortField == "DataLockStatus")
            {
                var searchParameters = new ApprenticeshipSearchParameters
                {
                    EmployerAccountId = query.EmployerAccountId,
                    ProviderId        = query.ProviderId,
                    PageNumber        = query.PageNumber,
                    PageItemCount     = query.PageItemCount,
                    ReverseSort       = query.ReverseSort,
                    Filters           = query.SearchFilters,
                    CancellationToken = cancellationToken
                };

                searchResult = await _apprenticeshipSearch.Find(searchParameters);
            }
            else
            {
                if (query.ReverseSort)
                {
                    var searchParameters = new ReverseOrderedApprenticeshipSearchParameters
                    {
                        EmployerAccountId = query.EmployerAccountId,
                        ProviderId        = query.ProviderId,
                        PageNumber        = query.PageNumber,
                        PageItemCount     = query.PageItemCount,
                        ReverseSort       = query.ReverseSort,
                        Filters           = query.SearchFilters,
                        FieldName         = query.SortField,
                        CancellationToken = cancellationToken
                    };

                    searchResult = await _apprenticeshipSearch.Find(searchParameters);
                }
                else
                {
                    var searchParameters = new OrderedApprenticeshipSearchParameters
                    {
                        EmployerAccountId = query.EmployerAccountId,
                        ProviderId        = query.ProviderId,
                        PageNumber        = query.PageNumber,
                        PageItemCount     = query.PageItemCount,
                        ReverseSort       = query.ReverseSort,
                        Filters           = query.SearchFilters,
                        FieldName         = query.SortField,
                        CancellationToken = cancellationToken
                    };

                    searchResult = await _apprenticeshipSearch.Find(searchParameters);
                }
            }
            searchResult.Apprenticeships = searchResult.Apprenticeships
                                           .Select(c => { c.IsProviderSearch = query.ProviderId.HasValue; return(c); })
                                           .ToList();

            foreach (var apprenticeship in searchResult.Apprenticeships)
            {
                var details = await _mapper.Map(apprenticeship);

                matchedApprenticeshipDetails.Add(details);
            }

            return(new GetApprenticeshipsQueryResult
            {
                Apprenticeships = matchedApprenticeshipDetails,
                TotalApprenticeshipsFound = searchResult.TotalApprenticeshipsFound,
                TotalApprenticeshipsWithAlertsFound = searchResult.TotalApprenticeshipsWithAlertsFound,
                TotalApprenticeships = searchResult.TotalAvailableApprenticeships,
                PageNumber = searchResult.PageNumber
            });
        }