public async Task <HttpResponseMessage> AddDefaultCity([FromBody] CityEntity city) { if (city == null || !ModelState.IsValid) { return(new HttpResponseMessage(HttpStatusCode.BadRequest)); } if ((await _weatherService.GetCitiesByNameAsync(city.Name)).Count() != 0) { return(Request.CreateResponse(HttpStatusCode.Conflict, $"City '{city.Name}' already exist")); } if (!await _forecastProvider.SuccessPingCityAsync(city.Name)) { return(Request.CreateResponse(HttpStatusCode.BadRequest, string.Format(InvalidCity, city.Name))); } await _weatherService.AddCityAsync(city); return(new HttpResponseMessage(HttpStatusCode.Created)); }
public async Task <ActionResult> AddCity(CityEntity city) { if (string.IsNullOrEmpty(city?.Name)) { return(RedirectToAction("Index")); } var view = "~/Views/Settings/Index.cshtml"; var cities = await _weatherService.GetCitiesAsync(); if ((await _weatherService.GetCitiesByNameAsync(city.Name)).Count() != 0) { ViewBag.ErrorMessage = $"City '{city.Name}' had already added in default cities."; return(View(view, cities)); } if (!await _forecastProvider.SuccessPingCityAsync(city.Name)) { ViewBag.ErrorMessage = $"SmartWeather service doesn't provide forecast for city '{city.Name}'"; return(View(view, cities)); } await _weatherService.AddCityAsync(city); return(RedirectToAction("Index")); }