コード例 #1
0
 public async Task<IHttpActionResult> PostSensorReading()
 {
     if (!ModelState.IsValid)
     {
         return BadRequest(ModelState);
     }
     dynamic obj = await Request.Content.ReadAsAsync<JObject>();
     var dt = DateTime.Now;
     SensorReading sensorReading = new SensorReading();
     int numReadings = (int)obj["numReadings"];
     sensorReading.timeLogged = dt;
     sensorReading.duration = (int)obj["duration"];
     sensorReading.deviceID = (int) obj["deviceID"];
     db.SensorReadings.Add(sensorReading);
     db.SaveChanges();
     JArray types = obj["readingType"];
     JArray values = obj["readingValue"];
     for(int i = 0; i < numReadings; i++)
     {
         SensorReadingValue value = new SensorReadingValue();
         value.sensorReadingID = sensorReading.ID;
         value.readingValueType = (int) types[i];
         value.readingValue = (string) values[i];
         db.SensorReadingValues.Add(value);
     }
     db.SaveChanges();
     return CreatedAtRoute("ControllerAndAction", new { id = sensorReading.ID }, sensorReading);
 }
コード例 #2
0
        public IHttpActionResult PostSensorReadingValue(SensorReadingValue sensorReadingValue)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.SensorReadingValues.Add(sensorReadingValue);
            db.SaveChanges();

            return CreatedAtRoute("ControllerAndAction", new { id = sensorReadingValue.ID }, sensorReadingValue);
        }