diff options
Diffstat (limited to 'PollutometerWebApi/Controllers/ReadingsController.cs')
-rw-r--r-- | PollutometerWebApi/Controllers/ReadingsController.cs | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/PollutometerWebApi/Controllers/ReadingsController.cs b/PollutometerWebApi/Controllers/ReadingsController.cs new file mode 100644 index 0000000..b692a9c --- /dev/null +++ b/PollutometerWebApi/Controllers/ReadingsController.cs @@ -0,0 +1,88 @@ +using System;
+using System.Web.Http;
+using PollutometerWebApi.Models; +using PollutometerWebApi.Singletons; + +namespace PollutometerWebApi.Controllers
+{
+ public class ReadingsController : ApiController
+ { + public ReadingsController() { } + + public IHttpActionResult GetAllReadings() + {
+ var command = "SELECT * FROM Readings";
+ var readings = SqlOperator.GetReadings(command); + + if (readings.Count > 0) return Ok(readings);
+ else return NotFound(); + } + + public IHttpActionResult GetReading(int id) + {
+ var command = $"SELECT * FROM Readings WHERE Id={id}"; + var reading = SqlOperator.GetReadings(command)[0]; + + if (reading != null) return Ok(reading); + else return NotFound(); + }
+
+ [Route("api/Readings/latest")]
+ public IHttpActionResult GetLatestReading()
+ { + var command = "SELECT * FROM Readings " +
+ "WHERE TimeStamp IN(SELECT MAX(TimeStamp) FROM Readings)"; + var reading = SqlOperator.GetReadings(command)[0]; + + if (reading != null) return Ok(reading); + else return NotFound();
+ } +
+ [Route("api/Readings/lastweek")] + public IHttpActionResult GetReadingsFromLastWeek() + {
+ var timeNow = DateTimeOffset.Now.ToUnixTimeSeconds(); + var command = "SELECT * FROM Readings " +
+ $"WHERE TimeStamp BETWEEN {timeNow-7*24*3600} AND {timeNow}"; + var readings = SqlOperator.GetReadings(command); + + if (readings.Count > 0) return Ok(readings); + else return NotFound(); + } + + public IHttpActionResult PutReading(int id, Reading reading) + { + if (reading != null) + { + SqlOperator.PutReading(id, reading); + return Ok(); + } + else return BadRequest(); + } + + public IHttpActionResult PostReading(Reading reading) + { + if (reading != null) + { + SqlOperator.PostReading(reading); + return Ok(); + } + else return BadRequest(); + } + + public IHttpActionResult DeleteReading(int id) + {
+ var command = $"SELECT * FROM Readings WHERE Id={id}";
+ + Reading reading = SqlOperator.GetReadings(command)[0]; + if (reading == null) + { + return NotFound(); + } + + SqlOperator.DeleteReading(id); + + return Ok(reading); + }
+ }
+}
\ No newline at end of file |