예제 #1
0
        //push a single item to remote server
        private bool DoPush(Batch batch, DataItemPush item)
        {
            try
            {
                string result = Post(_url_push, new NameValueCollection()
                                {
                                    {"secret_code",   ConfigManager.Current.RemoteSecretKey},
                                    {"sku",           item.SKU.ToString()},
                                    {"product_name",  item.ProductName.ToString()},
                                    {"description",   item.Description.ToString()},
                                    {"url",           item.Url.ToString()},
                                    {"original_url",  item.OriginalUrl.ToString()},
                                    {"image_url",     item.ImageUrl.ToString()},
                                    {"price",         item.Price.ToString("#.##")},
                                    {"delivery_cost", item.DeliveryCost.ToString()},
                                    {"currency_code", item.CurrencyCode.ToString()},
                                    {"brand",         item.Brand.ToString()},
                                    {"colour",        item.Colour.ToString()},
                                    {"gender",        item.Gender.ToString()},
                                    {"size",          item.Size.ToString()},
                                    {"mid",           batch.Merchant.MerchantID.ToString()}
                                });

                if (result == "OK"){
                    return true;
                }
                else{
                    int trimLength = 100;
                    if (result.Length < 100) { trimLength = result.Length; }
                    Console.WriteLine("RemotePush.DoPush seems good, SKU: " + item.SKU + ", but remote server return: " + result.Substring(0, trimLength));        //truncate to up to 100 characters error
                    return false;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("RemotePush.DoPush has failed: " + ex.ToString());
                return false;
            }
        }
        //get list of data that ready to be pushed
        public List<DataItemPush> GetDataForPush(int batchId)
        {
            Console.WriteLine("DatabaseManager.GetDataForPush - read data for push");
            List<DataItemPush> result = new List<DataItemPush>();
            string sql = "Select PushId, BatchId, SKU, ProductName, Description, Url, OriginalUrl, ImageUrl, Price, DeliveryCost, " +
                         "CurrencyCode, Brand, Colour, Gender, Size " +
                         "From FeedChangePush where BatchId = " + batchId + " And DatePushed is Null";

            SqlCommand cmd = new SqlCommand(sql, sqlConn);
            cmd.CommandType = CommandType.Text;
            try
            {
                using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    while (reader.Read())
                    {
                        DataItemPush item = new DataItemPush();
                        item.PushId = (int)reader["PushId"];
                        item.BatchId = batchId;
                        item.SKU = reader["SKU"].ToString();
                        item.ProductName = reader["ProductName"].ToString();
                        item.Description = reader["Description"].ToString();
                        item.Url = reader["Url"].ToString();
                        item.OriginalUrl = reader["OriginalUrl"].ToString();
                        item.ImageUrl = reader["ImageUrl"].ToString();
                        item.Price = (decimal)reader["Price"];
                        item.DeliveryCost = reader["DeliveryCost"].ToString();
                        item.CurrencyCode = reader["CurrencyCode"].ToString();
                        item.Brand = reader["Brand"].ToString();
                        item.Colour = reader["Colour"].ToString();
                        item.Gender = reader["Gender"].ToString();
                        item.Size = reader["Size"].ToString();

                        result.Add(item);
                    }
                    reader.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("DatabaseManager.GetDataForPush is failed at BatchId: " + batchId.ToString() + " - " + ex.ToString());
            }

            Console.WriteLine("DatabaseManager.GetDataForPush - Number of push items: " + result.Count.ToString());
            return result;
        }