Exemplo n.º 1
0
        public Models.TemperatureAndMusicList GetInformationByCity(string city)
        {
            Models.TemperatureAndMusicList info = null;

            try
            {
                //var client = new RestClient($"http://localhost:50879/api/Music/city/{city}");
                //var client = new RestClient($"http://localhost/ingaiawebapi/api/Music/city/{city}");
                var client  = new RestClient($"https://ingaiachallenge.azurewebsites.net/api/Music/city/{city}");
                var request = new RestRequest(Method.GET);
                request.AddHeader("Content-Type", "application/json");
                request.AddHeader("Accept", "application/json");
                request.AddHeader("Authorization", Constants.ConstValues.SECURITY_KEY);
                IRestResponse response = client.Execute(request);

                if (response.IsSuccessful)
                {
                    info = SimpleJson.DeserializeObject <Models.TemperatureAndMusicList>(response.Content);
                }
                else
                {
                    if (response != null && response.Content.Length > 0)
                    {
                        ErrorApiMessage err = SimpleJson.DeserializeObject <Models.ErrorApiMessage>(response.Content);
                        if (err != null && err.message.Length > 0)
                        {
                            throw new Exception(err.message);
                        }
                    }
                    else
                    {
                        throw new Exception(response.ErrorException.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(info);
        }
Exemplo n.º 2
0
        public bool SaveLogCity(Models.TemperatureAndMusicList info)
        {
            bool ret = false;

            this.Error = string.Empty;

            try
            {
                string strcmd = @"  INSERT INTO LOGCITY (DTREAD, CITY, TEMP)
                                    VALUES (GETDATE(), @_city, @_temp )
                                ";

                using (SqlConnection sqlcon = new SqlConnection(this.ConnString))
                {
                    sqlcon.Open();
                    if (sqlcon.State == ConnectionState.Open)
                    {
                        using (SqlCommand sqlcmd = new SqlCommand(strcmd, sqlcon))
                        {
                            sqlcmd.Parameters.AddWithValue("_city", info.CityName.ToUpper().Trim());
                            sqlcmd.Parameters.AddWithValue("_temp", info.Temperature);
                            sqlcmd.ExecuteNonQuery();
                        }
                    }
                    sqlcon.Close();
                }

                ret = true;
            }
            catch (Exception ex)
            {
                this.Error = ex.Message;
            }

            return(ret);
        }