コード例 #1
0
        /// <summary>
        /// Gets query.
        /// </summary>
        /// <param name="searchCriteria"></param>
        /// <returns></returns>
        public static string GetQuery(this SearchCriteria searchCriteria)
        {
            searchCriteria.SearchText = searchCriteria.SearchText.GetValidContent(CharOptions.Letter, " ");

            searchCriteria.SearchShowDeleted = searchCriteria.CurrentUser.Role == RoleOption.Administrator;

            var queryBuilder = new StringBuilder();

            queryBuilder.Append('{');

            var containsСondition     = searchCriteria.GetContainsСondition();
            var isOrContainsСondition = containsСondition.Substring(0, 3).ToLowerInvariant() == "$or";

            if (!searchCriteria.SearchText.IsNullOrEmpty() && isOrContainsСondition)
            {
                queryBuilder.Append(searchCriteria.GetContainsСondition() + ", ");
            }

            queryBuilder.Append("$and : [" +
                                searchCriteria.GetEqualsСondition() +

                                searchCriteria.GetDeletedСondition() +
                                searchCriteria.GetDiscountOptionCondition() +
                                searchCriteria.GetDateCondition() +
                                searchCriteria.GetAmountOfDiscountCondition() +
                                searchCriteria.GetRatingTotalCondition() +
                                searchCriteria.GetCompanyNameCondition());
            if (!isOrContainsСondition)
            {
                queryBuilder.Append(", " + searchCriteria.GetContainsСondition());
            }

            queryBuilder.Append(']');
            queryBuilder.Append('}');

            var queryCompile = new Dictionary <string, string>
            {
                { "manualLanguageControl", "true" },

                { "searchCountry", searchCriteria.SearchAddressCountry },
                { "searchCity", searchCriteria.SearchAddressCity },
                { "searchLanguage", searchCriteria.SearchLanguage.ToStringLookup() },

                { "fieldName", "name" },
                { "fieldTags", "tags" },
                { "fieldDescription", "description" },
                { "fieldCountry", "address.country" },
                { "fieldCity", "address.city" },
                { "fieldLanguage", "language" },
                { "fieldTranslations", searchCriteria.GetTranslationsPrefix() },

                { "query", queryBuilder.ToString() }
            }.CompileQuery();

            return(queryCompile["query"]);
        }