コード例 #1
0
        public async Task <IActionResult> Submit(SubmitModel model)
        {
            if (!ModelState.IsValid)
            {
                return(Json(new ResultModel
                {
                    Success = false,
                    Errors = ModelState.Values.SelectMany(m => m.Errors).Select(m => m.ErrorMessage)
                }));;
            }

            if (!_searchEngine.IsValidSearchEngine(model.Url))
            {
                var urls = new List <string>
                {
                    $"Available search engines -"
                };

                urls.AddRange(_searchEngine.GetSearchEngines().Select(s => s.Url));

                return(Json(new ResultModel
                {
                    Success = false,
                    Errors = urls,
                }));
            }

            var hits = await _searchEngine.FindHits(model.Url, model.SearchTerms.Split(' '));

            var result = new ResultModel
            {
                Hits    = string.Join(", ", hits).Trim(),
                Success = true
            };

            return(Json(result));
        }