예제 #1
0
        public async Task <QueryResponse <GetSuggestionResponse> > Handle(GetSuggestionRequest query, CancellationToken cancellationToken)
        {
            try
            {
                if (!Validate(query))
                {
                    return(new QueryResponse <GetSuggestionResponse>(MessageType.Validation, new QueryResponseError("Validate", Errors.First())));
                }

                string cacheKey = GetCacheKey(query);

                return(await _cache.GetOrCreateAsync(cacheKey, (entry) =>
                {
                    entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromDays(365);
                    entry.SlidingExpiration = TimeSpan.FromDays(365);

                    return ExecuteQuery(query);
                }));
            }
            catch
            {
                // Default behavior no suggestion
                return(new QueryResponse <GetSuggestionResponse>(new GetSuggestionResponse()));
            }
        }
예제 #2
0
        private bool Validate(GetSuggestionRequest query)
        {
            if (query == null)
            {
                Errors.Add("Error: Query parameter is null!");
            }

            return(Errors.Count == 0);
        }
예제 #3
0
        private async Task <QueryResponse <GetSuggestionResponse> > ExecuteQuery(GetSuggestionRequest query)
        {
            if (!string.IsNullOrEmpty(query.CityName))
            {
                var apiResult = await _googlePlacesApiService.GetByPlacesSuggestionAsync(query.CityName);

                if (apiResult.IsSuccessful)
                {
                    return(new QueryResponse <GetSuggestionResponse>(new GetSuggestionResponse()
                    {
                        SuggestionList = apiResult.Response.predictions
                                         .Select(p => p.structured_formatting.main_text)
                                         .Distinct()
                                         .ToList()
                    }));
                }
            }

            // Default behavior no suggestion
            return(new QueryResponse <GetSuggestionResponse>(new GetSuggestionResponse()));
        }