public async Task <IActionResult> GetProviderById(string providerVersionId, string providerId)
        {
            SearchModel searchModel = new SearchModel {
                Top = 1
            };

            searchModel.Filters = new Dictionary <string, string[]>
            {
                { "providerId", new string[] { providerId } },
                { "providerVersionId", new string[] { providerVersionId } }
            };

            try
            {
                ProviderVersionSearchResults results = await SearchProviderVersionSearchResults(searchModel);

                if (results.Results.IsNullOrEmpty())
                {
                    return(new NotFoundResult());
                }

                return(new OkObjectResult(results.Results.First()));
            }
            catch (FailedToQuerySearchException exception)
            {
                string error = $"Failed to query search with Provider Version Id: {providerVersionId} and Provider Id: {providerId}";

                _logger.Error(exception, error);

                return(new InternalServerErrorResult(error));
            }
        }
        private async Task <ProviderVersionSearchResults> SearchProviderVersionSearchResults(SearchModel searchModel)
        {
            IEnumerable <Task <SearchResults <ProvidersIndex> > > searchTasks = BuildSearchTasks(searchModel);

            await TaskHelper.WhenAllAndThrow(searchTasks.ToArraySafe());

            ProviderVersionSearchResults results = new ProviderVersionSearchResults();

            foreach (var searchTask in searchTasks)
            {
                ProcessSearchResults(searchTask.Result, results);
            }

            return(results);
        }
        public async Task <IActionResult> SearchProviderVersions(SearchModel searchModel)
        {
            Guard.ArgumentNotNull(searchModel, nameof(searchModel));

            try
            {
                ProviderVersionSearchResults results = await SearchProviderVersionSearchResults(searchModel);

                return(new OkObjectResult(results));
            }
            catch (FailedToQuerySearchException exception)
            {
                string error = $"Failed to query search with term: {searchModel.SearchTerm}";

                _logger.Error(exception, error);

                return(new InternalServerErrorResult(error));
            }
        }
        public async Task <IActionResult> GetLocalAuthoritiesByProviderVersionId(string providerVersionId)
        {
            string authorityFacet         = "authority";
            string providerVersionIdFacet = "providerVersionId";

            SearchModel searchModel = new SearchModel()
            {
                Top = 0
            };

            searchModel.FacetCount          = 1000;
            searchModel.IncludeFacets       = true;
            searchModel.OverrideFacetFields = new[] { authorityFacet };
            searchModel.SearchMode          = Models.Search.SearchMode.All;
            searchModel.Filters             = new Dictionary <string, string[]>
            {
                { providerVersionIdFacet, new string[] { providerVersionId } }
            };

            try
            {
                ProviderVersionSearchResults results = await SearchProviderVersionSearchResults(searchModel);

                IEnumerable <string> providerVersions = results.Facets.Single(x => x.Name == providerVersionIdFacet).FacetValues.Select(x => x.Name);
                IEnumerable <string> authorities      = results.Facets.Single(x => x.Name == authorityFacet).FacetValues.Select(x => x.Name);

                if (!providerVersions.Any(x => x == providerVersionId))
                {
                    return(new NotFoundResult());
                }

                return(new OkObjectResult(authorities));
            }
            catch (FailedToQuerySearchException exception)
            {
                string error = $"Failed to query search with Provider Version Id: {providerVersionId}";

                _logger.Error(exception, error);

                return(new InternalServerErrorResult(error));
            }
        }
        public async Task <IActionResult> SearchProviders(string providerVersionId, SearchModel searchModel = null)
        {
            searchModel = searchModel ?? new SearchModel();

            if (!searchModel.Filters.ContainsKey("providerVersionId"))
            {
                searchModel.Filters.Add("providerVersionId", new string[] { providerVersionId });
            }

            try
            {
                ProviderVersionSearchResults results = await SearchProviderVersionSearchResults(searchModel);

                return(new OkObjectResult(results));
            }
            catch (FailedToQuerySearchException exception)
            {
                string error = $"Failed to query search with Provider Version Id: {providerVersionId} and Search Term: {searchModel.SearchTerm}";

                _logger.Error(exception, error);

                return(new InternalServerErrorResult(error));
            }
        }
 private void ProcessSearchResults(SearchResults <ProvidersIndex> searchResult, ProviderVersionSearchResults results)
 {
     if (!searchResult.Facets.IsNullOrEmpty())
     {
         results.Facets = results.Facets.Concat(searchResult.Facets);
     }
     else
     {
         results.TotalCount = (int)(searchResult?.TotalCount ?? 0);
         results.Results    = searchResult?.Results?.Select(m => new ProviderVersionSearchResult
         {
             Id   = m.Result.Id,
             Name = m.Result.Name,
             ProviderVersionId = m.Result.ProviderVersionId,
             ProviderId        = m.Result.ProviderId,
             URN   = m.Result.URN,
             UKPRN = m.Result.UKPRN,
             UPIN  = m.Result.UPIN,
             EstablishmentNumber    = m.Result.EstablishmentNumber,
             DfeEstablishmentNumber = m.Result.DfeEstablishmentNumber,
             Authority             = m.Result.Authority,
             ProviderType          = m.Result.ProviderType,
             ProviderSubType       = m.Result.ProviderSubType,
             DateOpened            = m.Result.DateOpened,
             DateClosed            = m.Result.DateClosed,
             ProviderProfileIdType = m.Result.ProviderProfileIdType,
             LaCode                    = m.Result.LaCode,
             NavVendorNo               = m.Result.NavVendorNo,
             CrmAccountId              = m.Result.CrmAccountId,
             LegalName                 = m.Result.LegalName,
             Status                    = m.Result.Status,
             PhaseOfEducation          = m.Result.PhaseOfEducation,
             ReasonEstablishmentOpened = m.Result.ReasonEstablishmentOpened,
             ReasonEstablishmentClosed = m.Result.ReasonEstablishmentClosed,
             Successor                 = m.Result.Successor,
             TrustStatus               = m.Result.TrustStatus,
             TrustName                 = m.Result.TrustName,
             TrustCode                 = m.Result.TrustCode,
             Town                          = m.Result.Town,
             Postcode                      = m.Result.Postcode,
             LocalAuthorityName            = m.Result.LocalAuthorityName,
             CompaniesHouseNumber          = m.Result.CompaniesHouseNumber,
             GroupIdNumber                 = m.Result.GroupIdNumber,
             RscRegionName                 = m.Result.RscRegionName,
             RscRegionCode                 = m.Result.RscRegionCode,
             GovernmentOfficeRegionName    = m.Result.GovernmentOfficeRegionName,
             GovernmentOfficeRegionCode    = m.Result.GovernmentOfficeRegionCode,
             DistrictCode                  = m.Result.DistrictCode,
             DistrictName                  = m.Result.DistrictName,
             WardName                      = m.Result.WardName,
             WardCode                      = m.Result.WardCode,
             CensusWardCode                = m.Result.CensusWardCode,
             CensusWardName                = m.Result.CensusWardName,
             MiddleSuperOutputAreaCode     = m.Result.MiddleSuperOutputAreaCode,
             MiddleSuperOutputAreaName     = m.Result.MiddleSuperOutputAreaName,
             LowerSuperOutputAreaCode      = m.Result.LowerSuperOutputAreaCode,
             LowerSuperOutputAreaName      = m.Result.LowerSuperOutputAreaName,
             ParliamentaryConstituencyCode = m.Result.ParliamentaryConstituencyCode,
             ParliamentaryConstituencyName = m.Result.ParliamentaryConstituencyName,
             CountryCode                   = m.Result.CountryCode,
             CountryName                   = m.Result.CountryName,
             LocalGovernmentGroupTypeCode  = m.Result.LocalGovernmentGroupTypeCode,
             LocalGovernmentGroupTypeName  = m.Result.LocalGovernmentGroupTypeName,
             Street                        = m.Result.Street,
             Locality                      = m.Result.Locality,
             Address3                      = m.Result.Address3,
             PaymentOrganisationIdentifier = m.Result.PaymentOrganisationIdentifier,
             PaymentOrganisationName       = m.Result.PaymentOrganisationName
         });
     }
 }