public async Task <dynamic> GetResourceBasedOnThresholdAsync(LuisInput luisInput)
        {
            var encodedSentence = HttpUtility.UrlEncode(luisInput.Sentence);

            if (string.IsNullOrEmpty(luisInput.LuisTopScoringIntent))
            {
                dynamic luisResponse = await luisProxy.GetIntents(encodedSentence);

                luisTopIntents = ParseLuisIntent(luisResponse);
            }
            LuisViewModel luisViewModel = null;

            if ((luisTopIntents != null && IsIntentAccurate(luisTopIntents)) || !string.IsNullOrEmpty(luisInput.LuisTopScoringIntent))
            {
                luisViewModel = await GetInternalResourcesAsync(
                    luisTopIntents?.TopScoringIntent ?? luisInput.LuisTopScoringIntent,
                    luisInput,
                    luisTopIntents != null && luisTopIntents.TopNIntents != null?luisTopIntents.TopNIntents : null);
            }
            //Will fetch web links only when there are no mapping LUIS Intent or no mapping resources to specific LUIS Intent
            return(((luisViewModel != null && luisViewModel.Resources != null &&
                     ((JContainer)(luisViewModel.Resources)).Count > 0)) ||
                   !string.IsNullOrEmpty(luisInput.LuisTopScoringIntent) ?
                   JObject.FromObject(luisViewModel).ToString() :
                   await GetWebResourcesAsync(encodedSentence));
        }
        public async Task <IActionResult> GetAsync([FromBody] LuisInput luisInput)
        {
            if (string.IsNullOrWhiteSpace(luisInput.Sentence))
            {
                return(BadRequest("search term cannot be empty string."));
            }

            var resources = await luisBusinessLogic.GetResourceBasedOnThresholdAsync(luisInput);

            return(Content(resources));
        }
        public async Task <dynamic> GetInternalResourcesAsync(string keyword, LuisInput luisInput, IEnumerable <string> relevantIntents)
        {
            CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
            TextInfo    textInfo    = cultureInfo.TextInfo;
            Location    location    = luisInput.Location;

            var topics = await topicsResourcesBusinessLogic.GetTopicsAsync(textInfo.ToTitleCase(keyword), location);

            List <string> topicIds = new List <string>();

            foreach (var item in topics)
            {
                string topicId = item.id;
                topicIds.Add(topicId);
            }

            if (topicIds.Count == 0 || location == null)
            {
                return(new LuisViewModel
                {
                    TopIntent = keyword
                });
            }

            ResourceFilter resourceFilter = new ResourceFilter {
                TopicIds = topicIds, PageNumber = 0, ResourceType = Constants.All, Location = location
            };
            var            GetResourcesTask   = topicsResourcesBusinessLogic.GetResourcesCountAsync(resourceFilter);
            ResourceFilter sortResourceFilter = resourceFilter;

            sortResourceFilter.IsOrder      = true;
            sortResourceFilter.OrderByField = luisInput.OrderByField;
            sortResourceFilter.OrderBy      = luisInput.OrderBy;
            var ApplyPaginationTask = topicsResourcesBusinessLogic.ApplyPaginationAsync(sortResourceFilter);

            //To get guided assistant id
            resourceFilter.ResourceType = Constants.GuidedAssistant;
            var GetGuidedAssistantId = topicsResourcesBusinessLogic.ApplyPaginationAsync(resourceFilter);
            await Task.WhenAll(GetResourcesTask, ApplyPaginationTask, GetGuidedAssistantId);

            var            groupedResourceType     = GetResourcesTask.Result;
            PagedResources resources               = ApplyPaginationTask.Result;
            PagedResources guidedAssistantResponse = GetGuidedAssistantId.Result;
            var            guidedAssistantResult   = guidedAssistantResponse != null?JsonUtilities.DeserializeDynamicObject <GuidedAssistant>(guidedAssistantResponse.Results.FirstOrDefault()) : null;

            dynamic searchFilter = new JObject();

            searchFilter.OrderByField = resourceFilter.OrderByField;
            searchFilter.OrderBy      = resourceFilter.OrderBy;
            return(new LuisViewModel
            {
                TopIntent = keyword,
                RelevantIntents = relevantIntents != null?JsonUtilities.DeserializeDynamicObject <dynamic>(relevantIntents) : JsonConvert.DeserializeObject(Constants.EmptyArray),
                                      Topics = topics != null?JsonUtilities.DeserializeDynamicObject <dynamic>(topics) : JsonConvert.DeserializeObject(Constants.EmptyArray),
                                                   Resources = resources != null?JsonUtilities.DeserializeDynamicObject <dynamic>(resources.Results) : JsonConvert.DeserializeObject(Constants.EmptyArray),
                                                                   ContinuationToken = resources != null && resources.ContinuationToken != null?JsonConvert.DeserializeObject(resources.ContinuationToken) : JsonConvert.DeserializeObject(Constants.EmptyArray),
                                                                                           TopicIds = topicIds != null?JsonUtilities.DeserializeDynamicObject <dynamic>(topicIds) : JsonConvert.DeserializeObject(Constants.EmptyArray),
                                                                                                          ResourceTypeFilter = groupedResourceType != null?JsonUtilities.DeserializeDynamicObject <dynamic>(groupedResourceType) : JsonConvert.DeserializeObject(Constants.EmptyArray),
                                                                                                                                   GuidedAssistantId = guidedAssistantResult != null ? guidedAssistantResult.CuratedExperienceId : string.Empty,
                                                                                                                                   SearchFilter = searchFilter
            });
        }