From 55fa1f08621a975f439c50d853c4b55fe044e15f Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Thu, 23 Nov 2017 11:42:09 +0100 Subject: Added method for getting latest reading. --- AirPollutionWebApi/Controllers/ReadingsController.cs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'AirPollutionWebApi') diff --git a/AirPollutionWebApi/Controllers/ReadingsController.cs b/AirPollutionWebApi/Controllers/ReadingsController.cs index e8eb0fa..9173f72 100644 --- a/AirPollutionWebApi/Controllers/ReadingsController.cs +++ b/AirPollutionWebApi/Controllers/ReadingsController.cs @@ -21,7 +21,24 @@ namespace AirPollutionWebApi.Controllers if (reading != null) return Ok(reading); else return NotFound(); - } + } + + [Route("/api/Readings/latest")] + public IHttpActionResult GetLatestReading() + { + var readings = SqlOperator.GetAllReadings(); + Reading latestReading = null; + + foreach(var reading in readings) + { + if (latestReading == null) latestReading = reading; + if (reading.TimeStamp > latestReading.TimeStamp) + latestReading = reading; + } + + if (latestReading != null) return Ok(latestReading); + else return NotFound(); + } public IHttpActionResult PutReading(int id, Reading reading) { -- cgit v1.2.3