Beispiel #1
0
        public void AddProductKeyword(int productId, string keyword)
        {
            ProductEntity     p        = new ProductEntity(productId);
            KeywordCollection keywords = new KeywordCollection();

            keywords.GetMulti(null);

            if (keywords.Any(pr => pr.Keyword == keyword))
            {
                int keywordId = keywords.Where <KeywordEntity>(f => f.Keyword == keyword).FirstOrDefault().Id;

                ProductKeywordEntity productKeyword = new ProductKeywordEntity();
                productKeyword.KeywordId = keywordId;
                productKeyword.ProductId = productId;
                productKeyword.Save();
            }
            else
            {
                KeywordEntity k = new KeywordEntity();
                k.Keyword = keyword;
                k.Save();

                ProductKeywordEntity newProductKeyword = new ProductKeywordEntity();
                newProductKeyword.KeywordId = k.Id;
                newProductKeyword.ProductId = productId;
                newProductKeyword.Save();
            }
        }
        /// <summary>Creates a new, empty KeywordEntity object.</summary>
        /// <returns>A new, empty KeywordEntity object.</returns>
        public override IEntity Create()
        {
            IEntity toReturn = new KeywordEntity();

            // __LLBLGENPRO_USER_CODE_REGION_START CreateNewKeyword
            // __LLBLGENPRO_USER_CODE_REGION_END
            return(toReturn);
        }
        /// <summary>
        /// Saves the keywords.
        /// </summary>
        /// <param name="keywords">The keywords.</param>
        /// <exception cref="System.NotImplementedException"></exception>
        public void SaveKeywords(List<KeywordResult> keywords)
        {
            // Get the storage account connection
            var storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["Escc.Search.AutoComplete.AzureStorage"].ConnectionString);

            // Create the table if it doesn't exist.
            var tableClient = storageAccount.CreateCloudTableClient();
            var table = tableClient.GetTableReference("autocomplete");
            table.CreateIfNotExistsAsync().Wait();

            foreach (var keyword in keywords)
            {
                // Azure tables use an index clustered first by partition key then by row key.
                //
                // When we look up a keyword we will want matching search terms ordered by page views.
                // To get that we need to have results ordered by page views, then filter the list to only search terms that match, 
                // which means the partition key has to be based on page views and the row key based on search terms.
                //
                // The partition key is a string, so convert the number of page views to a string and pad with leading 0s so that
                // the alpha sort gives the same result as a numeric sort. However this still sorts low numbers of page views ahead
                // of high, so we need to change low numbers to high ones and vice versa to get the right sort order. Subtracting 1000000
                // makes the numbers of page views negative (assuming they're under 1000000), and multiplying by -1 removes the minus sign,
                // giving us the sort order we want.
                //
                // The row key has to be a sanitised version of the keyword because a key can't contain common search term characters such 
                // as / and ?, so save the keyword separately as-typed so that it can be presented back to users.
                var entity = new KeywordEntity()
                {
                    PartitionKey = (( keyword.PageViews - 1000000) * -1).ToString("D8"),
                    RowKey = ToAzureKeyString(keyword.Keyword),
                    Keyword = keyword.Keyword,
                    FeedDate = keyword.FeedDate.ToShortDateString()
                };

                // Create the TableOperation object that inserts the entity.
                var insertOperation = TableOperation.InsertOrReplace(entity);

                // Execute the insert operation.
                try
                {
                    table.Execute(insertOperation);
                }
                catch (StorageException ex)
                {
                    if (ex.Message.Contains("(400) Bad Request"))
                    {
                        Console.WriteLine(keyword.Keyword + " returned " + ex.RequestInformation.ExtendedErrorInformation.ErrorMessage);
                        ex.ToExceptionless().Submit();
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }