Пример #1
0
        public static void Add(SearchReturnData data)
        {
            var result = Cache.FirstOrDefault(x => x.Valid && x.Value.SearchID.Equals(data.SearchID));

            if (result == null)
                Cache.Add(new ExpirableSearchResult(data, _validFor));
            else
                result.ExpiresOn(DateTime.Now + _validFor);
        }
Пример #2
0
        public SearchReturnData Search(SearchData searchData)
        {
            var searchCmd = connection.CreateCommand();
            searchCmd.CommandText = "Search";
            searchCmd.CommandType = CommandType.StoredProcedure;
            searchCmd.Parameters.Add(new SqlParameter("@userID", searchData.UserID));
            searchCmd.Parameters.Add(new SqlParameter("@tagIDs", DataMapper.MapTagIdsParameterTable(searchData.Tags)));

            connection.Open();
            var results = DataMapper.MapSearchResultsFromDatabase(searchCmd.ExecuteReader());
            connection.Close();

            switch(searchData.SearchType)
            {
                case 0:	// Date Created
                    results = results.OrderByDescending(x => x.DateCreated).ToArray();
                    break;
                case 1: // Hot
                    results = results.OrderByDescending(x => x.NumFavorites).ToArray();
                    break;
                case 2: // User favorites only
                    results = results.Where(x => x.Favorite).OrderByDescending(x => x.DateCreated).ToArray();
                    break;
                default:
                    return null;
            }

            var cacheData = new SearchReturnData()
            {
                SearchID = Guid.NewGuid(),
                Results = DataMapper.MapSearchResults(results).ToArray()
            };
            SearchResultsCache.Add(cacheData);
            if (searchData.PageSize == 0)
                searchData.PageSize = 50;

            var returnData = new SearchReturnData()
            {
                SearchID = cacheData.SearchID,
                Results = cacheData.Results.Take(searchData.PageSize).ToArray()
            };
            return returnData;
        }