public void GivenIHaveASensorAs(Table table) { _sensor = new TankTempWeb.Models.Api.v1.Sensor { NetworkId = Convert.ToInt32(table.Rows[0][0]), SerialNumber = table.Rows[0][1], Description = table.Rows[0][2], Unit = table.Rows[0][3], Type = table.Rows[0][4], }; }
public TankApi.Sensor Get(int id) { var sensor = _sensors.Get(id); if (sensor == null) throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound) { ReasonPhrase = string.Format("Sensor {0} not found.",id) }); var apiSensor = new TankTempWeb.Models.Api.v1.Sensor() { Description = sensor.Description, Id = sensor.Id, SerialNumber = sensor.SerialNumber, Unit = sensor.Unit, Type = sensor.GetType().Name }; return apiSensor; }
public HttpResponseMessage Post(TankApi.Sensor sensor) { var network = _networks.Get(sensor.NetworkId); if(network == null){ throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound) { ReasonPhrase = string.Format("Network {0} does not exist", sensor.NetworkId) }); } Sensor newSensor = null; switch (sensor.Type){ case "TemperatureSensor": newSensor = new TemperatureSensor(); break; default: throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest) { ReasonPhrase = string.Format("Unsupported Sensor type {0}", sensor.Type) }); } newSensor.Description = sensor.Description; newSensor.SerialNumber = sensor.SerialNumber; newSensor.Unit = sensor.Unit; network.AddSensor(newSensor); _sensors.Save(newSensor); var createdSensor = new TankApi.Sensor() { Id = newSensor.Id, Description = newSensor.Description, Type = newSensor.GetType().Name, SerialNumber = newSensor.SerialNumber, Unit = newSensor.Unit }; var response = Request.CreateResponse<TankApi.Sensor>(HttpStatusCode.Created, createdSensor); response.Headers.Location = new Uri(Request.RequestUri,string.Concat("sensor","/",createdSensor.Id.ToString())); return response; }