示例#1
0
        private static ErrorSearchResult CreateErrorResult <T>(ElasticQueryResult <T> found) where T : TreeRecord
        {
            var result = new ErrorSearchResult();
            var error  = new ApiError
            {
                StatusCode = found.Status
            };

            result.Error = error;
            return(result);
        }
        public ISearchResult Search([FromBody] SearchParameters search, string language = null)
        {
            ISearchResult result;

            try
            {
                language = language ?? WebHelper.GetClientLanguage(Request);

                var error = entityProvider.CheckSearchParameters(search, language);
                if (!string.IsNullOrEmpty(error))
                {
                    return(new ErrorSearchResult
                    {
                        Error = new ApiError
                        {
                            StatusCode = (int)HttpStatusCode.Forbidden,
                            Message = error,
                            Details = string.Empty
                        }
                    });
                }

                var access = GetUserAccess(language);
                var userId = ControllerHelper.GetCurrentUserId();

                if (usageAnalyzer.GetExceededThreshold(userId, Request) != null)
                {
                    if (string.IsNullOrEmpty(search?.Captcha?.Token))
                    {
                        return(GetCaptchaMissing(language));
                    }

                    if (!SecurityHelper.IsValidCaptcha(search.Captcha, FrontendSettingsViaduc.Instance.GetServerSettings()))
                    {
                        return(GetCaptchaInvalid(language));
                    }

                    usageAnalyzer.Reset(userId, Request);
                }

                result = entityProvider.Search <SearchRecord>(search, access);

                if (result is SearchResult <SearchRecord> searchResult)
                {
                    usageAnalyzer.UpdateUsageStatistic(userId, Request, searchResult.Entities.Items.Count);

                    if (usageAnalyzer.GetExceededThreshold(userId, Request) != null)
                    {
                        return(GetCaptchaMissing(language));
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Search for {searchQuery} failed", JsonConvert.SerializeObject(search, Formatting.Indented));
                result = new ErrorSearchResult
                {
                    Error = new ApiError
                    {
                        StatusCode = (int)HttpStatusCode.InternalServerError,
                        Message    = FrontendSettingsViaduc.Instance.GetTranslation(language, "search.unexpectedSystemError",
                                                                                    "Es ist ein unerwarteter Fehler aufgetreten.")
                    }
                };
            }

            return(result);
        }