public IHttpActionResult Putsensor(int id, Sensor sensor) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != sensor.Id) { return(BadRequest()); } db.Entry(sensor).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!sensorExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public IHttpActionResult Post(Location location) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.Locations.Add(location); db.SaveChanges(); return(CreatedAtRoute("DefaultApi", new { id = location.Id }, location)); }
public IHttpActionResult Post(SensorType sensorType) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.SensorTypes.Add(sensorType); db.SaveChanges(); return(CreatedAtRoute("DefaultApi", new { id = sensorType.Id }, sensorType)); }
/// <summary> /// This will store the data in the database. /// </summary> /// <param name="sensorValues">Dictionary containing the sensor as key, and the value as - well - value</param> /// <returns>OK if everything was added correctly to the db. BadRequest if something went wrong.</returns> private HttpStatusCode AddValueToDatabase(IDictionary <Sensor, Value> sensorValues) { try { foreach (KeyValuePair <Sensor, Value> keyValuePair in sensorValues) { keyValuePair.Value.FK_Sensor = keyValuePair.Key.Id; _dbConnection.Values.Add(keyValuePair.Value); _dbConnection.SaveChanges(); } return(HttpStatusCode.OK); } catch (DbEntityValidationException ex) { foreach (var dbEntityValidationResult in ex.EntityValidationErrors) { foreach (DbValidationError dbValidationError in dbEntityValidationResult.ValidationErrors) { Trace.TraceError($"error message: {dbValidationError.ErrorMessage}\nproperty name: {dbValidationError.PropertyName}"); } } } catch (Exception x) { Trace.TraceError("unknown exception: " + x.GetType()); } return(HttpStatusCode.BadRequest); }