public async Task <HttpResponseMessage> AddCity([FromUri] string cityName) { if (cityName == null || cityName == "") { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } try { await _service.AddCityAsync(new CityName { Name = cityName }); return(Request.CreateResponse(HttpStatusCode.Created)); } catch { return(Request.CreateResponse(HttpStatusCode.InternalServerError)); } }
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")); }
public async Task <ActionResult> Configurations(string configAction, string cityName) { try { if (configAction == "Add") { await service.AddCityAsync(new CityName { Name = cityName }); } else if (configAction == "Delete") { await service.DeleteCitiesWithNameAsync(cityName); } ViewBag.CityList = await service.GetCityListAsync(); ViewBag.Error = ""; } catch (Exception ex) { ViewBag.CityList = new List <CityName>(); ViewBag.Error = ex.Message; } return(View()); }