Пример #1
0
    private IEnumerable <ContentObject> GetSearchResults()
    {
        IEnumerable <ContentObject> co = null;

        vwarDAL.ISearchProxy _SearchProxy = new DataAccessFactory().CreateSearchProxy(Context.User.Identity.Name);
        SearchMethod         method;
        string methodParam = Request.QueryString["Method"];

        if (!String.IsNullOrEmpty(methodParam) && methodParam.ToLowerInvariant() == "and")
        {
            method = SearchMethod.AND;
        }
        else
        {
            method = SearchMethod.OR;
        }

        if (!String.IsNullOrEmpty(Request.QueryString["Search"]))
        {
            string searchTerm = Request.QueryString["Search"];
            if (!String.IsNullOrEmpty(searchTerm))
            {
                SearchTextBox.Text = Server.UrlDecode(searchTerm);
                if (SearchTextBox.Text.Contains(','))
                {
                    var terms = SearchTextBox.Text.Split(',');
                    co = _SearchProxy.QuickSearch(terms, method);
                }
                else
                {
                    co = _SearchProxy.QuickSearch(SearchTextBox.Text);
                }
            }
        }
        else if (!String.IsNullOrEmpty(Request.QueryString["Group"]))
        {
            /* Here, we are getting "everything" (limit NumResultsPerPage) by a grouping.
             *
             * By doing a little processing ahead of time, we can
             * avoid having to use ApplySort when unnecessary, as well
             * as reduce the size of the data returned from MySQL.
             */

            string[] sortParams = SortInfo.Split('-');
            if (sortParams.Length == 2) //Make sure it's in the right format!
            {
                SortOrder order = (sortParams[1].ToLowerInvariant() == "low")
                  ? SortOrder.Ascending
                  : SortOrder.Descending;

                int start = (_PageNumber - 1) * _ResultsPerPage;

                switch (sortParams[0].ToLowerInvariant())
                {
                case "views":
                    co = _SearchProxy.GetByViews(_ResultsPerPage, start, order);
                    break;

                case "updated":
                    co = _SearchProxy.GetByLastUpdated(_ResultsPerPage, start, order);
                    break;

                case "viewed":
                    co = _SearchProxy.GetByLastViewed(_ResultsPerPage, start, order);
                    break;

                case "rating":
                default:
                    co = _SearchProxy.GetByRating(_ResultsPerPage, start, order);
                    break;
                }
                _Presorted = true;
            }
        }
        else //We're searching by field
        {
            System.Collections.Specialized.NameValueCollection fieldsToSearch = new System.Collections.Specialized.NameValueCollection();

            string[] searchableFields = { "Title", "Description", "ArtistName", "SponsorName", "DeveloperName", "Keywords" };

            foreach (string field in searchableFields)
            {
                if (!String.IsNullOrEmpty(Request.QueryString[field]))
                {
                    fieldsToSearch[field] = Server.UrlDecode(Request.QueryString[field]);
                }
            }

            co = _SearchProxy.SearchByFields(fieldsToSearch, method);
        }

        _SearchProxy.Dispose();
        return(co);
    }