public IViewComponentResult Invoke(string city)
        {
            string query = "https://api.openweathermap.org/data/2.5/forecast?q=+" + city + "&units=metric&appid=" + _config.GetValue <string>("WeatherForcastAPIKey");
            WeatherForecastData weatherData = _forecastService.GetWeatherForecast(query).Result;

            return(View(weatherData));
        }
示例#2
0
        public async Task <WeatherForecastData> UpdateWeatherForecast(WeatherForecastData weatherForecastData)
        {
            var forecast = _mapper.Map <WeatherForecastData, WeatherForecast>(weatherForecastData);

            var request = new UpdateWeatherForecastRequest
            {
                Forecast = forecast
            };

            var response = await _client.UpdateWeatherForecastAsync(request);

            return(_mapper.Map <WeatherForecast, WeatherForecastData>(response.Forecast));
        }
        public async Task <Models.WeatherForecast> GetResult(string userId, string remoteIpAddress, bool force)
        {
            var weatherForecastBuilder = new WeatherForecastBuilder();
            var result = await weatherForecastBuilder.GetWeatherForecastCache(remoteIpAddress, force);

            var weatherForecastRepository = new WeatherForecastRepository();
            var userEntry = new WeatherForecastData
            {
                UserId = userId,
                City   = result.City
            };

            await weatherForecastRepository.InsertUserHistory(userEntry);

            return(result);
        }
示例#4
0
        public async Task <WeatherForecastData> GetWeatherForecast(string query)
        {
            WeatherForecastData weatherData = null;

            try
            {
                var response = await _client.GetAsync(query);

                if (response.IsSuccessStatusCode)
                {
                    var content = await response.Content.ReadAsStringAsync();

                    weatherData = JsonConvert.DeserializeObject <WeatherForecastData>(content);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            return(weatherData);
        }
示例#5
0
        public async Task <List <WeatherData> > GetNextDayWeatherDataAsync(string uri)
        {
            WeatherForecastData forecastData        = null;
            List <WeatherData>  weatherForecastData = null;


            HttpResponseMessage response = await client.GetAsync(uri);

            if (response.IsSuccessStatusCode)
            {
                string content = await response.Content.ReadAsStringAsync();

                forecastData        = JsonConvert.DeserializeObject <WeatherForecastData>(content);
                weatherForecastData = forecastData.Items;
            }
            else
            {
                throw new Exception();
            }
            return(weatherForecastData);
        }
        public async Task InsertUserHistory(WeatherForecastData data)
        {
            const string query = @"
                Declare @location int;
                
                If Not Exists(select Top 1 1 from dbo.[User] where UserID=@userId)
                Begin
                    Insert into dbo.[User] (UserId 
                        ,CreatedByName
                        ,CreatedOn
                        ,LastUpdatedByName
                        ,LastUpdatedOn)
                    values (@userId
                        ,'System'
                        ,getutcdate()
                        ,'System'
                        ,getutcdate())
                End;
                
                If Not Exists(select Top 1 1 from dbo.[Location] where city = @city)
                Begin
                    Insert into dbo.[Location] (City 
                        ,CreatedByName
                        ,CreatedOn
                        ,LastUpdatedByName
                        ,LastUpdatedOn)
                    values (@city
                        ,'System'
                        ,getutcdate()
                        ,'System'
                        ,getutcdate())
                End;
                
                Set @location = (Select LocationId from dbo.Location where city = @city);
                
                Insert into dbo.[User_Location] (UserId
                    ,LocationId
                    ,RequestedOn)
                values (@userId
                    ,@location
                    ,getutcdate());
                ";

            using (var connection = _dbProvider.Get())
            {
                connection.Open();
                using (var transaction = connection.BeginTransaction())
                {
                    try
                    {
                        await connection.ExecuteAsync(query, new
                        {
                            userId = data.UserId,
                            city   = data.City
                        }, transaction);

                        transaction.Commit();
                    }
                    catch (Exception)
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }