Ejemplo n.º 1
0
        public async Task <CheckWebsiteStatusResponse> CheckWebsiteStatusAsync(CheckWebsiteStatusRequest request)
        {
            var key = request.GenerateCacheKey();

            var cachedResponse = cacheHandler.GetCacheObject <CheckWebsiteStatusResponse>(key);

            if (cachedResponse != null)
            {
                return(cachedResponse);
            }

            if (!Enum.TryParse <SearchEngineType>(request.SearchEngine, true, out var engineType))
            {
                throw new SearchEngineHandlerNotFound(request.SearchEngine);
            }

            var searchEngine = searchEngineHandlers.Select(engineType);

            var links = (await searchEngine.SelectLinksAsync(request.Query, request.Count)).ToList();

            var resultItems = new List <SearchResultItem>();

            for (var i = 0; i < links.Count; i++)
            {
                if (links[i].Contains(request.Link, StringComparison.InvariantCultureIgnoreCase))
                {
                    resultItems.Add(new SearchResultItem()
                    {
                        Position = i + 1
                    });
                }
            }

            var response = new CheckWebsiteStatusResponse()
            {
                ResultItems = resultItems
            };

            cacheHandler.SetCacheObject(key, response);

            return(response);
        }