public void ThenShouldFilterStartDateTo()
        {
            //Arrange
            var filterValue = DateTime.Now.AddDays(10).Date;

            var apprenticeships = new List <Apprenticeship>
            {
                new Apprenticeship
                {
                    Cohort    = new Cohort(),
                    StartDate = DateTime.Now.AddDays(10).Date
                },
                new Apprenticeship(),
                new Apprenticeship
                {
                    Cohort    = new Cohort(),
                    StartDate = DateTime.Now.AddDays(11).Date
                }
            }.AsQueryable();

            var filterValues = new ApprenticeshipSearchFilters {
                StartDateRange = new DateRange {
                    To = filterValue
                }
            };

            //Act
            var result = apprenticeships.Filter(filterValues).ToList();

            //Assert
            Assert.AreEqual(1, result.Count);
            Assert.IsTrue(result.All(a => a.StartDate.Value.Date <= filterValue));
        }
        public void ThenShouldFilterEndDateEvenWhenSomeEndDatesDoNotExist()
        {
            //Arrange
            var filterValue = DateTime.Now;

            var apprenticeships = new List <Apprenticeship>
            {
                new Apprenticeship
                {
                    EndDate = filterValue
                },
                new Apprenticeship(),
                new Apprenticeship
                {
                    EndDate = filterValue.AddMonths(1)
                }
            }.AsQueryable();

            var filterValues = new ApprenticeshipSearchFilters {
                EndDate = filterValue
            };

            //Act
            var result = apprenticeships.Filter(filterValues).ToList();

            //Assert
            Assert.AreEqual(1, result.Count);
            Assert.IsTrue(result.All(a => a.EndDate.Equals(filterValue)));
        }
        public void ThenShouldFilterAccountLegalEntities()
        {
            //Arrange
            var filterValue = 123;

            var apprenticeships = new List <Apprenticeship>
            {
                new Apprenticeship
                {
                    Cohort = new Cohort()
                },
                new Apprenticeship(),
                new Apprenticeship
                {
                    Cohort = new Cohort {
                        AccountLegalEntityId = filterValue
                    }
                }
            }.AsQueryable();

            var filterValues = new ApprenticeshipSearchFilters {
                AccountLegalEntityId = filterValue
            };

            //Act
            var result = apprenticeships.Filter(filterValues).ToList();

            //Assert
            Assert.AreEqual(1, result.Count);
            Assert.IsTrue(result.All(a => a.Cohort.AccountLegalEntityId == filterValue));
        }
        public void ThenShouldFilterStatusEvenWhenSomePaymentStatusesDoNotExist()
        {
            //Arrange
            var filterValue = ApprenticeshipStatus.Completed.MapToPaymentStatus();

            var apprenticeships = new List <Apprenticeship>
            {
                new Apprenticeship
                {
                    PaymentStatus = filterValue
                },
                new Apprenticeship(),
                new Apprenticeship
                {
                    PaymentStatus = PaymentStatus.Paused
                }
            }.AsQueryable();

            var filterValues = new ApprenticeshipSearchFilters
            {
                Status = ApprenticeshipStatus.Completed
            };

            //Act
            var result = apprenticeships.Filter(filterValues).ToList();

            //Assert
            Assert.AreEqual(1, result.Count);
            Assert.IsTrue(result.All(a => a.PaymentStatus.Equals(filterValue)));
        }
        public void And_Uln_Matches_SearchTerm_Then_Included(
            long searchTerm,
            List <Apprenticeship> apprenticeships)
        {
            apprenticeships[0].Uln = searchTerm.ToString();
            var filter = new ApprenticeshipSearchFilters {
                SearchTerm = searchTerm.ToString()
            };

            var filtered = apprenticeships.AsQueryable().Filter(filter);

            filtered.Count().Should().Be(apprenticeships.Count(apprenticeship =>
                                                               apprenticeship.Uln == searchTerm.ToString()));
        }
        public void And_LastName_Starts_With_SearchTerm_Then_Included(
            string searchTerm,
            List <Apprenticeship> apprenticeships)
        {
            apprenticeships[0].LastName = searchTerm + apprenticeships[0].LastName;
            var filter = new ApprenticeshipSearchFilters {
                SearchTerm = searchTerm
            };

            var filtered = apprenticeships.AsQueryable().Filter(filter);

            filtered.Count().Should().Be(apprenticeships.Count(apprenticeship =>
                                                               apprenticeship.LastName.StartsWith(searchTerm)));
        }
        public void And_First_Name_Matches_But_Last_Name_Does_Not_Match_Search_Term_Then_Returns_Nothing(
            string searchTermFirstName,
            string searchTermLastName,
            List <Apprenticeship> apprenticeships)
        {
            apprenticeships[0].FirstName = searchTermFirstName;
            apprenticeships[0].LastName  = "noMatch";
            var searchTerm = searchTermFirstName + " " + searchTermLastName;
            var filter     = new ApprenticeshipSearchFilters {
                SearchTerm = searchTerm
            };

            var filtered = apprenticeships.AsQueryable().Filter(filter);

            filtered.Count().Should().Be(0);
        }
        public void And_Name_Is_Searched_With_Uln_Then_Returns_No_Results(
            string searchName,
            string searchUln,
            List <Apprenticeship> apprenticeships)
        {
            apprenticeships[0].FirstName = searchName;
            apprenticeships[0].Uln       = searchUln.Substring(0, 9);
            var searchTerm = searchName + " " + searchUln;
            var filter     = new ApprenticeshipSearchFilters {
                SearchTerm = searchTerm
            };

            var filtered = apprenticeships.AsQueryable().Filter(filter);

            filtered.Count().Should().Be(0);
        }
        public void And_FirstName_Or_LastName_Matches_SearchTerm_Then_Included(
            string searchTerm,
            List <Apprenticeship> apprenticeships)
        {
            apprenticeships[0].FirstName = searchTerm;
            apprenticeships[1].LastName  = searchTerm;
            var filter = new ApprenticeshipSearchFilters {
                SearchTerm = searchTerm
            };

            var filtered = apprenticeships.AsQueryable().Filter(filter);

            filtered.Count().Should().Be(apprenticeships.Count(apprenticeship =>
                                                               apprenticeship.FirstName == searchTerm ||
                                                               apprenticeship.LastName == searchTerm));
        }
コード例 #10
0
        public async Task <IActionResult> GetApprenticeships([FromQuery] GetApprenticeshipsRequest request)
        {
            try
            {
                var filterValues = new ApprenticeshipSearchFilters
                {
                    SearchTerm           = request.SearchTerm,
                    EmployerName         = request.EmployerName,
                    CourseName           = request.CourseName,
                    Status               = request.Status,
                    StartDate            = request.StartDate,
                    EndDate              = request.EndDate,
                    ProviderName         = request.ProviderName,
                    AccountLegalEntityId = request.AccountLegalEntityId,
                    StartDateRange       = new DateRange {
                        From = request.StartDateRangeFrom, To = request.StartDateRangeTo
                    }
                };

                var queryResult = await _mediator.Send(new GetApprenticeshipsQuery
                {
                    EmployerAccountId = request.AccountId,
                    ProviderId        = request.ProviderId,
                    PageNumber        = request.PageNumber,
                    PageItemCount     = request.PageItemCount,
                    SortField         = request.SortField,
                    ReverseSort       = request.ReverseSort,
                    SearchFilters     = filterValues
                });

                if (queryResult == null)
                {
                    return(NotFound());
                }

                var response = await _modelMapper.Map <GetApprenticeshipsResponse>(queryResult);

                return(Ok(response));
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                throw;
            }
        }
        public void Then_If_Filtering_By_Live_Adds_Date_Range()
        {
            //Arrange
            var expectedApprenticeship = new Apprenticeship
            {
                Id            = 2,
                PaymentStatus = PaymentStatus.Active,
                StartDate     = DateTime.UtcNow.AddMonths(-1)
            };
            var apprenticeships = new List <Apprenticeship>
            {
                new Apprenticeship
                {
                    Id            = 1,
                    PaymentStatus = PaymentStatus.Completed
                },
                expectedApprenticeship,
                new Apprenticeship
                {
                    Id            = 3,
                    PaymentStatus = PaymentStatus.Active,
                    StartDate     = DateTime.UtcNow.AddMonths(1)
                }
            }.AsQueryable();

            var filterValues = new ApprenticeshipSearchFilters
            {
                Status = ApprenticeshipStatus.Live
            };

            //Act
            var result = apprenticeships.Filter(filterValues).ToList();

            //Assert
            Assert.AreEqual(1, result.Count);
            result.Should().AllBeEquivalentTo(expectedApprenticeship);
        }
        public static IQueryable<Apprenticeship> Filter(this IQueryable<Apprenticeship> apprenticeships,
            ApprenticeshipSearchFilters filters)
        {
            if (filters == null)
            {
                return apprenticeships;
            }

            if (!string.IsNullOrEmpty(filters.SearchTerm))
            {
                if(long.TryParse(filters.SearchTerm, out var result))
                {
                    apprenticeships = apprenticeships.Where(app =>
                        app.Uln == filters.SearchTerm);
                }
                else
                {
                    var found = new List<long>();

                    if (!filters.SearchTerm.Contains(" "))
                    {
                        found.AddRange(apprenticeships.Where(app =>
                                app.FirstName.StartsWith(filters.SearchTerm))
                            .Select(apprenticeship => apprenticeship.Id));

                        found.AddRange(apprenticeships.Where(app =>
                                app.LastName.StartsWith(filters.SearchTerm))
                            .Select(apprenticeship => apprenticeship.Id));
                    }
                    else
                    {
                        var firstName = filters.SearchTerm.Substring(0, filters.SearchTerm.IndexOf(' '));
                        var lastName = filters.SearchTerm.Substring(firstName.Length + 1);
                        
                        found.AddRange(apprenticeships.Where(app =>
                                app.FirstName.StartsWith(firstName) && 
                                app.LastName.StartsWith(lastName))
                            .Select(apprenticeship => apprenticeship.Id));
                    }

                    apprenticeships = apprenticeships.Where(apprenticeship =>
                        found.Contains(apprenticeship.Id));
                }
            }

            if (!string.IsNullOrEmpty(filters.EmployerName))
            {
                apprenticeships = apprenticeships.Where(app => app.Cohort != null && filters.EmployerName.Equals(app.Cohort.AccountLegalEntity.Name));
            }

            if (!string.IsNullOrEmpty(filters.ProviderName))
            {
                apprenticeships = apprenticeships.Where(app => app.Cohort != null && filters.ProviderName.Equals(app.Cohort.Provider.Name));
            }

            if (!string.IsNullOrEmpty(filters.CourseName))
            {
                apprenticeships = apprenticeships.Where(app => filters.CourseName.Equals(app.CourseName));
            }

            if (filters.Status.HasValue)
            {
                var paymentStatuses = filters.Status.Value.MapToPaymentStatus();

                apprenticeships = apprenticeships.Where(app => paymentStatuses == app.PaymentStatus);
                switch (filters.Status)
                {
                    case ApprenticeshipStatus.WaitingToStart:
                        apprenticeships = apprenticeships.Where(c => c.StartDate.HasValue && c.StartDate >= DateTime.UtcNow);
                        break;
                    case ApprenticeshipStatus.Live:
                        apprenticeships = apprenticeships.Where(c => c.StartDate.HasValue && c.StartDate <= DateTime.UtcNow);
                        break;
                }
            }

            if (filters.StartDate.HasValue)
            {
                apprenticeships = apprenticeships.Where(app =>  
                    app.StartDate.HasValue &&
                    filters.StartDate.Value.Month.Equals(app.StartDate.Value.Month) && 
                    filters.StartDate.Value.Year.Equals(app.StartDate.Value.Year));
            }

            if (filters.EndDate.HasValue)
            {
                apprenticeships = apprenticeships.Where(app => 
                    app.EndDate.HasValue &&
                    filters.EndDate.Value.Month.Equals(app.EndDate.Value.Month) && 
                    filters.EndDate.Value.Year.Equals(app.EndDate.Value.Year));
            }

            if (filters.AccountLegalEntityId.HasValue)
            {
                apprenticeships = apprenticeships.Where(x => x.Cohort != null && x.Cohort.AccountLegalEntityId == filters.AccountLegalEntityId.Value);
            }

            if (filters.StartDateRange != null)
            {
                if (filters.StartDateRange.From.HasValue)
                {
                    apprenticeships = apprenticeships.Where(x => x.StartDate.HasValue && x.StartDate.Value >= filters.StartDateRange.From);
                }

                if (filters.StartDateRange.To.HasValue)
                {
                    apprenticeships = apprenticeships.Where(x => x.StartDate.HasValue && x.StartDate.Value <= filters.StartDateRange.To);
                }
            }

            return apprenticeships;
        }