public async Task <GetCurrentWeatherResponse> GetCurrentWeatherAsync(GetCurrentWeatherRequest request, GetCurrentWeatherResponse response) { try { // Map request var serviceRequest = _mapper.Map <Repository.Entities.Requests.GetCurrentWeatherRequest>(request); // Map response var serviceResponse = _mapper.Map <Repository.Entities.Responses.GetCurrentWeatherResponse>(response); // Call action serviceResponse = await _weatherWorkflow.GetCurrentWeatherAsync(serviceRequest, serviceResponse); // Merge response logs response.Marge(serviceResponse); // Handle response if (response.IsSuccess) { response.Temperature = serviceResponse.Temperature; response.WeatherDescription = serviceResponse.WeatherDescription; } else { response.Failed("WeatherConnector:GetCurrentWeatherAsync"); } } catch (Exception ex) { response.Failed(ex); } return(response); }
public async Task <GetCurrentWeatherResponse> GetCurrentWeatherAsync(GetCurrentWeatherRequest request, GetCurrentWeatherResponse response) { try { var placeTemperature = await _weatherRepository.GetPlaceTemperatureAsync(request.CityKey).ConfigureAwait(false); if (placeTemperature == null) { var currentConditions = await _weatherRepository.GetCurrentConditionsAsync(request.CityKey).ConfigureAwait(false); if (currentConditions != null) { var conditions = currentConditions.FirstOrDefault(); if (conditions != null) { #region Save new weather data var savedData = await _weatherRepository.InsertPlaceWeatherAndData(request.CityKey, conditions.WeatherText, conditions.Temperature.Metric.Value, request.CityName).ConfigureAwait(false); if (savedData != null) { response.Temperature = savedData.Temperature; response.WeatherDescription = savedData.WeatherText; response.Success("GetCurrentWeatherAsync"); } #endregion // Build Response } else { response.Failed("[WeatherDataLayer:GetCurrentWeatherAsync] No items found in currentConditions API result."); } } else { response.Failed("[WeatherDataLayer:GetCurrentWeatherAsync] GetCurrentConditionsAsync failed or no data found."); } } else { response.Temperature = placeTemperature.Temperature; response.WeatherDescription = placeTemperature.WeatherText; response.Success("GetCurrentWeatherAsync"); } } catch (Exception ex) { response.Failed(ex); } return(response); }
public async Task <GetCurrentWeatherResponse> GetCurrentWeatherAsync(GetCurrentWeatherRequest request, GetCurrentWeatherResponse response) { try { response = await _weatherDataLayer.GetCurrentWeatherAsync(request, response).ConfigureAwait(false); if (!response.IsSuccess) { response.Failed("WeatherWorkflow:GetCurrentWeatherAsync"); } } catch (Exception ex) { response.Failed(ex); } return(response); }
public async Task <CurrentWeatherResponse> GetCurrentWeather(GetCurrentWeatherRequest request) { CurrentWeatherResponse result = null; string content = ""; HttpClient client = new HttpClient(); client.BaseAddress = new Uri(baseUrl); WeatherDbDataContext db = new WeatherDbDataContext(connectString); //Create new Employee weather weather = db.weathers.FirstOrDefault(x => x.key == request.Key); if (weather != null) { return(new CurrentWeatherResponse { text = weather.WeatherText, value = double.Parse(weather.celsius) }); } else { HttpResponseMessage response = client.GetAsync(baseUrl + "currentconditions/v1/" + request.Key + "?apikey=" + apiKey).Result; if (response.IsSuccessStatusCode) { content = await response.Content.ReadAsStringAsync(); var list = JsonConvert.DeserializeObject <List <CurrentWeather> >(content); if (list.Count > 0) { result = new CurrentWeatherResponse { text = list[0].WeatherText, value = list[0].temperature.metric.Value }; db.weathers.InsertOnSubmit(new weather { celsius = result.value.ToString(), key = request.Key, WeatherText = result.text }); db.SubmitChanges(); } } } return(result); }
public async Task <GetCurrentWeatherResponse> GetCurrentWeatherAsync([FromQuery] GetCurrentWeatherRequest request) { var response = new GetCurrentWeatherResponse(); try { response = await _weatherWorkflow.GetCurrentWeatherAsync(request, response).ConfigureAwait(false); if (response.IsSuccess) { response.Success("GetCurrentWeatherAsync"); } else { response.Failed("WeatherController:GetCurrentWeatherAsync"); } } catch (Exception ex) { response.Failed(ex); } return(response); }