private void PostDataToAPI()
        {
            try
            {
                PostData dataPost = new PostData();
                dataPost.Status = "overover";
                DataTable dataToPost = cpn.GetAllDataNewWithStatusZero(rowAmount, listFields);
                //DataTable copyDataTable;
                //copyDataTable = dataToPost.Copy();
                //try
                //{
                //    cpn.UpdateSotokhai(copyDataTable);
                //}catch(Exception ex) { }

                if (dataToPost != null && dataToPost.Rows.Count > 0)
                {
                    DataTable copyDataTable;
                    copyDataTable = dataToPost.Copy();
                    //JObject json = JObject.Parse(apiUtils.GetJson(dataToPost));
                    dataPost.ShipmentNo = apiUtils.GetJsonFromDataTable(copyDataTable, limit);
                    HttpWebResponse response = (HttpWebResponse)apiUtils.CallToPostWebAPI(dataPost, apiFunctionPath);

                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        // Get the request stream.
                        Stream dataStream = response.GetResponseStream();
                        // Open the stream using a StreamReader for easy access.
                        StreamReader reader = new StreamReader(response.GetResponseStream());
                        // Read the content.
                        string responseFromServer = reader.ReadToEnd();
                        // Display the content.
                        //Console.WriteLine(responseFromServer);

                        string res = string.Empty;

                        // ... Read the string.
                        //Task<string> result = content.ReadAsStringAsync();
                        //res = result.Result;
                        JObject json = JObject.Parse(responseFromServer);
                        if (json["code"].ToString() == "success")
                        {
                            WebAPIUtils.WriteLog("\n******Post data success: " + DateTime.Now);
                            StringBuilder ids            = new StringBuilder();
                            StringBuilder shipmentIdList = new StringBuilder();
                            int           totalRow       = copyDataTable.Rows.Count;
                            for (int i = 0; i < totalRow; i++)
                            {
                                if (ids.Length == 0)
                                {
                                    ids.Append(copyDataTable.Rows[i]["Id"]);
                                    shipmentIdList.Append(copyDataTable.Rows[i]["ShipmentID"]);
                                }
                                else
                                {
                                    ids.Append(",");
                                    ids.Append(copyDataTable.Rows[i]["Id"]);
                                    shipmentIdList.Append(",");
                                    shipmentIdList.Append(copyDataTable.Rows[i]["ShipmentID"]);
                                }
                            }
                            int updateData = cpn.UpdateDataAfterSuccess(ids.ToString());
                            if (updateData > 0)
                            {
                                WebAPIUtils.WriteLog("\n ******Success update " + totalRow + "(" + updateData + ") rows to database. List id = " + ids);
                            }
                            else
                            {
                                WebAPIUtils.WriteLog("\n ******Fail update " + totalRow + " rows to database list id = " + ids);
                            }
                        }
                        else
                        {
                            WebAPIUtils.WriteLog("\n ******Post data success but get error: " + json.Values("message").ToString());
                        }
                        // Clean up the streams.
                        reader.Close();
                        dataStream.Close();
                        response.Close();
                    }
                    else
                    {
                        WebAPIUtils.WriteLog("\n******Post data fail : " + "Error Code");
                        //response.StatusCode + " : Message - " + response.ReasonPhrase);
                    }
                }
            }
            catch (Exception ex)
            {
                WebAPIUtils.WriteLog(ex);
            }
        }
Exemplo n.º 2
0
        private void PostDataToAPI(int tableType)
        {
            try
            {
                string   type     = tableType == 1 ? "IN" : "OUT";
                PostData dataPost = new PostData();
                dataPost.Status = type;
                DataTable dataToPost;
                if (tableType == 1)
                {
                    dataToPost = cpn.GetAllShipmentInfoWithStatusZero(rowAmount, listFields);
                }
                else
                {
                    dataToPost = cpn.GetAllShipmentOutWithStatusZero(rowAmount, listFields);
                }

                if (dataToPost != null && dataToPost.Rows.Count > 0)
                {
                    //JObject json = JObject.Parse(apiUtils.GetJson(dataToPost));
                    dataPost.ShipmentNo = apiUtils.GetJsonFromDataTable(dataToPost, limit);
                    HttpWebResponse response = (HttpWebResponse)apiUtils.CallToPostWebAPI(dataPost, apiFunctionPath);

                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        // Get the request stream.
                        Stream dataStream = response.GetResponseStream();
                        // Open the stream using a StreamReader for easy access.
                        StreamReader reader = new StreamReader(response.GetResponseStream());
                        // Read the content.
                        string responseFromServer = reader.ReadToEnd();
                        // Display the content.
                        //Console.WriteLine(responseFromServer);

                        string res = string.Empty;

                        // ... Read the string.
                        //Task<string> result = content.ReadAsStringAsync();
                        //res = result.Result;
                        JObject json = JObject.Parse(responseFromServer);

                        if (json["code"].ToString() == "success")
                        {
                            apiUtils.WriteLog("\n******Post sync data success: " + DateTime.Now);
                            //StringBuilder ids = new StringBuilder();
                            StringBuilder shipmentIdList = new StringBuilder();
                            int           totalRow       = dataToPost.Rows.Count;
                            for (int i = 0; i < totalRow; i++)
                            {
                                if (shipmentIdList.Length == 0)
                                {
                                    shipmentIdList.Append("'" + dataToPost.Rows[i]["ShipmentID"] + "'");
                                }
                                else
                                {
                                    shipmentIdList.Append(",");
                                    shipmentIdList.Append("'" + dataToPost.Rows[i]["ShipmentID"] + "'");
                                }
                            }

                            int updateData = cpn.UpdateDataAfterSuccess(shipmentIdList.ToString(), tableType);

                            if (updateData > 0)
                            {
                                apiUtils.WriteLog("\n ******Success sync " + type + " " + totalRow + "(" + updateData + ") rows to database." + Environment.NewLine + "\nList ShipmentId:" + shipmentIdList.ToString());
                            }
                            else
                            {
                                apiUtils.WriteLog("\n ******Fail sync " + type + " " + totalRow + " rows to database." + Environment.NewLine + "\n List ShipmentId:" + shipmentIdList.ToString());
                            }
                        }
                        else
                        {
                            apiUtils.WriteLog("\n ******Post sync data success but get error: " + json.Values("message").ToString());
                        }
                        // Clean up the streams.
                        reader.Close();
                        dataStream.Close();
                        response.Close();
                    }
                    else
                    {
                        apiUtils.WriteLog("\n******Post sync data fail : " + "Error Code");
                        //response.StatusCode + " : Message - " + response.ReasonPhrase);
                    }
                }
            }
            catch (Exception ex)
            {
                apiUtils.WriteLog(ex);
            }
        }