public IHttpActionResult PostHTForecast(HTForecast hTForecast) { if (!ModelState.IsValid) { return BadRequest(ModelState); } db.HTForecasts.Add(hTForecast); try { db.SaveChanges(); } catch (DbUpdateException) { if (HTForecastExists(hTForecast.Zipcode)) { return Conflict(); } else { throw; } } return CreatedAtRoute("DefaultApi", new { id = hTForecast.Zipcode }, hTForecast); }
public IHttpActionResult PutHTForecast(string id, HTForecast hTForecast) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != hTForecast.Zipcode) { return BadRequest(); } db.Entry(hTForecast).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!HTForecastExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }
public IHttpActionResult PostHTForecast(string Zipcode, [FromBody]string ForecastJson) { if (!ModelState.IsValid) { return BadRequest(ModelState); } JavaScriptSerializer serializer = new JavaScriptSerializer(); var result = serializer.Deserialize<HTForecastRaw[]>(ForecastJson); List<HTForecast> list = new List<HTForecast>(); foreach (var item in result) { HTForecast forecast = new HTForecast() { Zipcode = Zipcode, ForecastDate = UnixTimeStampToDateTime(item.epoch), Temperature = item.temp, Humidity = item.humidity }; HTForecastEqualtyComparer comparer = new HTForecastEqualtyComparer(); if (db.HTForecasts.Any(fc => fc.Zipcode == Zipcode && fc.ForecastDate.Equals(forecast.ForecastDate))) { db.Entry(forecast).State = EntityState.Modified; } else { list.Add(forecast); } } if (list.Count() > 0) { db.HTForecasts.AddRange(list); } try { db.SaveChanges(); } catch (DbUpdateException) { throw; } //return CreatedAtRoute("DefaultApi", new { id = Zipcode }, ForecastJson); return Ok(result); }