public SoilMoistureViewModel Post([FromBody] SoilMoistureViewModel value) { if (!value.IsValid) { throw new ArgumentOutOfRangeException(); } value.Taken = DateTime.Now; _data.SoilMoistureReadings.Add(value.ToData()); _data.SaveChanges(); return(value); }
public void ShouldNotAddSoilMoistureReadingForInvalidSensorNumber() { var model = new SoilMoistureViewModel { MilliVolts = 987, SensorNumber = -34, Taken = DateTime.Parse("1/1/2014") }; ExceptionAssert.Throws <ArgumentOutOfRangeException>(() => _controller.Post(model)); }
public void ShouldAddSoilMoistureReadingForValidSensorNumber() { var model = new SoilMoistureViewModel { MilliVolts = 987, SensorNumber = 1, Taken = DateTime.Parse("1/1/2014") }; model = _controller.Post(model); Assert.IsTrue(model.Taken > DateTime.Parse("1/1/2014")); Assert.AreEqual(987, _controller.Get(1).MilliVolts); }
public SoilMoistureViewModel Get(int sensorNumber) { try { return(SoilMoistureViewModel.FromData( _data.SoilMoistureReadings .Where(r => r.SensorNumber == sensorNumber) .OrderByDescending(r => r.Taken) .FirstOrDefault())); } catch (NullReferenceException) { throw new NoSoilMoistureReadingsException(); } }
public async Task ShouldAttemptToAddTemperatureIssuing_POST_To_api_Temperature() { using (var client = new HttpClient()) { client.BaseAddress = new Uri(TestBaseAddress); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var itemToAdd = new SoilMoistureViewModel { Taken = DateTime.Parse("1/1/2014"), MilliVolts = 400, SensorNumber = 1 }; var response = await client.PostAsJsonAsync("api/SoilMoisture", itemToAdd); var json = await response.Content.ReadAsStringAsync(); var model = JsonConvert.DeserializeObject <SoilMoistureViewModel>(json); Assert.IsTrue(model.Taken > DateTime.Parse("1/1/2014")); Assert.AreEqual(response.StatusCode, HttpStatusCode.OK); } }