DWMarketCheckModel GetResult(DWMarketCheckInputParam p)
        {
            DWMarketCheckModel result = new DWMarketCheckModel();

            result.errorCode = (byte)DW_ERROR_CODE.OK;

            RetryPolicy retryPolicy = new RetryPolicy <SqlAzureTransientErrorDetectionStrategy>(globalVal.conRetryCount, TimeSpan.FromSeconds(globalVal.conRetryFromSeconds));

            using (SqlConnection connection = new SqlConnection(globalVal.DBConnectionString))
            {
                string strQuery = string.Format("SELECT Version FROM DWMarketVersion WHERE MarketName = '{0}'", p.marketName);
                using (SqlCommand command = new SqlCommand(strQuery, connection))
                {
                    connection.OpenWithRetry(retryPolicy);
                    using (SqlDataReader dreader = command.ExecuteReaderWithRetry(retryPolicy))
                    {
                        if (dreader.HasRows == false)
                        {
                            result.errorCode = (byte)DW_ERROR_CODE.DB_ERROR;
                            return(result);
                        }

                        while (dreader.Read())
                        {
                            result.serverVersion = dreader[0].ToString();
                        }
                    }
                }
            }

            if (result.serverVersion == p.clientVersion)
            {
                result.checkState = (byte)APP_VERSION_CHECK_TYPE.SAME_TYPE;
            }
            else
            {
                result.checkState = (byte)APP_VERSION_CHECK_TYPE.DIFFERENT_TYPE;
            }

            return(result);
        }
        public HttpResponseMessage Post(DWMarketCheckInputParam p)
        {
            // try decrypt data
            if (!string.IsNullOrEmpty(p.token) && globalVal.CloudBreadCryptSetting == "AES256")
            {
                try
                {
                    string decrypted = Crypto.AES_decrypt(p.token, globalVal.CloudBreadCryptKey, globalVal.CloudBreadCryptIV);
                    p = JsonConvert.DeserializeObject <DWMarketCheckInputParam>(decrypted);
                }
                catch (Exception ex)
                {
                    ex = (Exception)Activator.CreateInstance(ex.GetType(), "Decrypt Error", ex);
                    throw ex;
                }
            }

            Logging.CBLoggers logMessage = new Logging.CBLoggers();
            string            jsonParam  = JsonConvert.SerializeObject(p);


            HttpResponseMessage response        = new HttpResponseMessage();
            EncryptedData       encryptedResult = new EncryptedData();

            try
            {
                DWMarketCheckModel result = GetResult(p);

                /// Encrypt the result response
                if (globalVal.CloudBreadCryptSetting == "AES256")
                {
                    try
                    {
                        encryptedResult.token = Crypto.AES_encrypt(JsonConvert.SerializeObject(result), globalVal.CloudBreadCryptKey, globalVal.CloudBreadCryptIV);
                        response = Request.CreateResponse(HttpStatusCode.OK, encryptedResult);
                        return(response);
                    }
                    catch (Exception ex)
                    {
                        ex = (Exception)Activator.CreateInstance(ex.GetType(), "Encrypt Error", ex);
                        throw ex;
                    }
                }

                response = Request.CreateResponse(HttpStatusCode.OK, result);
                return(response);
            }

            catch (Exception ex)
            {
                // error log
                logMessage.memberID  = "Version Check";
                logMessage.Level     = "ERROR";
                logMessage.Logger    = "DWMarketVersionCheckController";
                logMessage.Message   = jsonParam;
                logMessage.Exception = ex.ToString();
                Logging.RunLog(logMessage);

                throw;
            }
        }