예제 #1
0
 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);
 }
예제 #2
0
        public void ShouldNotAddSoilMoistureReadingForInvalidSensorNumber()
        {
            var model = new SoilMoistureViewModel
            {
                MilliVolts   = 987,
                SensorNumber = -34,
                Taken        = DateTime.Parse("1/1/2014")
            };

            ExceptionAssert.Throws <ArgumentOutOfRangeException>(() => _controller.Post(model));
        }
예제 #3
0
        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);
        }
예제 #4
0
 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();
     }
 }
예제 #5
0
        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);
            }
        }