Exemple #1
0
        public async Task <GetVacanciesResponse> Handle(GetVacanciesQuery request, CancellationToken cancellationToken)
        {
            var validationErrors = ValidateRequest(request);

            if (validationErrors.Any())
            {
                return(new GetVacanciesResponse {
                    ResultCode = ResponseCode.InvalidRequest, ValidationErrors = validationErrors.Cast <object>().ToList()
                });
            }

            IList <VacancySummaryProjection> vacancies;
            var isRequestingProviderOwnedVacancies = request.Ukprn.HasValue;

            if (isRequestingProviderOwnedVacancies)
            {
                var dashboard = await _queryStoreReader.GetProviderDashboardAsync(request.Ukprn.Value);

                vacancies = dashboard?
                            .Vacancies
                            .Where(vs => vs.EmployerAccountId.Equals(request.EmployerAccountId))
                            .ToList();
            }
            else
            {
                var dashboard = await _queryStoreReader.GetEmployerDashboardAsync(request.EmployerAccountId);

                vacancies = dashboard?.Vacancies.ToList();
            }

            if (vacancies == null || vacancies.Any() == false)
            {
                return(new GetVacanciesResponse {
                    ResultCode = ResponseCode.NotFound
                });
            }

            if (request.LegalEntityId.HasValue)
            {
                vacancies = vacancies.Where(vs => vs.LegalEntityId == request.LegalEntityId.Value).ToList();

                if (vacancies.Any() == false)
                {
                    return(new GetVacanciesResponse {
                        ResultCode = ResponseCode.NotFound
                    });
                }
            }

            var totalVacancies = vacancies.Count;
            var pageNo         = PagingHelper.GetRequestedPageNo(request.PageNo, request.PageSize, totalVacancies);
            var totalPages     = PagingHelper.GetTotalNoOfPages(request.PageSize, totalVacancies);

            var responseVacancies = GetVacanciesForPage(vacancies, pageNo, request.PageSize, isRequestingProviderOwnedVacancies);

            return(new GetVacanciesResponse
            {
                ResultCode = ResponseCode.Success,
                Data = new VacanciesSummary(responseVacancies, request.PageSize, pageNo, totalVacancies, totalPages)
            });
        }