示例#1
0
        public async Task <IActionResult> PostFavorites([FromBody] City favorites)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var existing = await _context.Favorites.FindAsync(favorites.Key);

            if (existing == null)
            {
                _context.Favorites.Add(favorites);
                await _context.SaveChangesAsync();
            }
            return(CreatedAtAction("GetFavorites", new { id = favorites.Key }, favorites));
        }
        public async Task <IActionResult> GetCurrentWeather([FromRoute] string key)
        {
            var exitingWeather = await _context.CityWeather.FindAsync(key);

            if (exitingWeather != null)
            {
                return(Ok(exitingWeather));
            }
            else
            {
                var client = GetHttpClient(CURRENTCONDITIONSURL + key + "?apikey=" + APIKEY);

                HttpResponseMessage response = await client.GetAsync("");

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

                    dynamic data = JArray.Parse(jsonString)[0];

                    var currentWeather = new Weather()
                    {
                        Key         = key,
                        WeatherText = data.WeatherText,
                        Temperature = data.Temperature.Metric.Value,
                        Icon        = data.WeatherIcon
                    };


                    //Save Weather for next time
                    _context.CityWeather.Add(currentWeather);
                    await _context.SaveChangesAsync();


                    return(Ok(currentWeather));
                }
            }
            return(NotFound());
        }
        public async Task<ActionResult<int>> CreateAddress(Address address)
        {
            if (address == null)
            {
                throw new Exception("Address is empty!");
            }
            address.CreatedDate = DateTime.Now;
            _context.Addresses.Add(address);
            var result = await _context.SaveChangesAsync();
            return (result > 0) ? address.Id : 0;

            throw new Exception("Problem Saving Changes");
        }
示例#4
0
        public async Task <IActionResult> PostCityWeather([FromBody] CityWeather cityWeather)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var city = await _context.CityWeather.FindAsync(cityWeather.Key);

            if (city == null)
            {
                _context.CityWeather.Add(cityWeather);
                await _context.SaveChangesAsync();
            }
            return(CreatedAtAction("GetCityWeather", new { id = cityWeather.Key }, cityWeather));
        }