public ActionResult Index(WeatherLookupViewModel weatherLookupViewModel) { //Check if model state (Data annotations on viewmodel class) is valid if not return the model to the //view. if (!ModelState.IsValid) { return(View(weatherLookupViewModel)); } //Create request to be sent to the WeatherLookupService var request = new WeatherLookupRequest { ZipCode = weatherLookupViewModel.ZipCodeSearch }; //Execute the request against the weatherlookupservice var response = WeatherLookupService.WeatherLookupRequest(request); //If the service was unable to grab the current weather, then tell the view not to show the //Weather search results box. weatherLookupViewModel.WeatherSearchPerformed = response.Success; //Depending on the state of the response, clean up the viewModel we are returning. weatherLookupViewModel.WeatherDataFound = response.Success; weatherLookupViewModel.CurrentConditions = response.Success ? response.Conditions : string.Empty; weatherLookupViewModel.CurrentTemperature = response.Success ? response.Temperature : string.Empty; if (response.Success) { weatherLookupViewModel.WeatherIconUrls = response.WeatherIconUrls; } return(View(weatherLookupViewModel)); }
private IndividualWeatherEntryViewModel GetWeatherData(string zipCode, Guid userId, string userName) { var individualWeatherEntryViewModel = new IndividualWeatherEntryViewModel(); var request = new WeatherLookupRequest { ZipCode = zipCode }; var response = WeatherLookupService.WeatherLookupRequest(request); if (!response.Success) { return(individualWeatherEntryViewModel); } individualWeatherEntryViewModel.CurrentTemperature = response.Temperature; individualWeatherEntryViewModel.CurrentConditions = response.Conditions; individualWeatherEntryViewModel.UserId = userId; individualWeatherEntryViewModel.Username = userName; individualWeatherEntryViewModel.PostalCode = zipCode; return(individualWeatherEntryViewModel); }
public WeatherLookupResponse WeatherLookupRequest(WeatherLookupRequest request) { var response = new WeatherLookupResponse { Success = false, Message = "Failed to lookup weather." }; try { //Using rest sharp to pull weather data from the weather api url. var client = new RestClient(_apiUrl); var zipCodeRequest = string.Format(_apiRestRequest, request.ZipCode); var restRequest = new RestRequest(zipCodeRequest, Method.GET) { RequestFormat = DataFormat.Json }; //Used http://json2csharp.com/ to build C# classes from the worldWeatherONline API. var restResponse = client.Execute <WorldWideWeatherOnline.JSONClasses.RootObject>(restRequest); //return a failed response if we are unable to pull weather data. if (restResponse.ErrorException != null) { response.Success = false; response.Exception = restResponse.ErrorException; response.Message = restResponse.ErrorMessage; return(response); } //If any piece of restResponse we need was not returned. Return a failed response. if (restResponse.Data == null || restResponse.Data.data == null || restResponse.Data.data.current_condition == null) { response.Success = false; response.Message = "Unable to determine the current conditions"; return(response); } var returnedData = restResponse.Data; var currentConditions = returnedData.data.current_condition.FirstOrDefault(); if (currentConditions == null) { response.Success = false; response.Message = "Unable to determine the current conditions"; return(response); } //Weather descriptions is a collection, so build a string with all the values var weatherDescriptions = currentConditions.weatherDesc; var conditions = new StringBuilder(); foreach (var weatherDescription in weatherDescriptions) { conditions.AppendLine(weatherDescription.value); } var weatherIconUrls = currentConditions.weatherIconUrl; //Add all weather icon urls gotten to the response. foreach (var weatherIconUrl in weatherIconUrls) { response.WeatherIconUrls.Add(weatherIconUrl.value); } response.Temperature = currentConditions.temp_F + " degrees Farenheight"; response.Conditions = conditions.ToString(); } catch (Exception exception) { response.Exception = exception; response.Success = false; return(response); } response.Success = true; response.Message = string.Empty; return(response); }