diff options
Diffstat (limited to 'AirPollutionWebApi/Controllers')
-rw-r--r-- | AirPollutionWebApi/Controllers/ReadingsController.cs | 49 |
1 files changed, 30 insertions, 19 deletions
diff --git a/AirPollutionWebApi/Controllers/ReadingsController.cs b/AirPollutionWebApi/Controllers/ReadingsController.cs index 19b87e7..480d8c0 100644 --- a/AirPollutionWebApi/Controllers/ReadingsController.cs +++ b/AirPollutionWebApi/Controllers/ReadingsController.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic;
+using System;
using System.Web.Http;
using AirPollutionWebApi.Models; using AirPollutionWebApi.Singletons; @@ -9,15 +9,19 @@ namespace AirPollutionWebApi.Controllers { public ReadingsController() { } - public IEnumerable<Reading> GetAllReadings() + public IHttpActionResult GetAllReadings() {
- var readings = SqlOperator.GetAllReadings(); - return readings; + 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 reading = SqlOperator.GetReadingById(id); + {
+ var command = $"SELECT * FROM Readings WHERE Id={id}"; + var reading = SqlOperator.GetReadings(command)[0]; if (reading != null) return Ok(reading); else return NotFound(); @@ -25,20 +29,25 @@ namespace AirPollutionWebApi.Controllers [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;
- }
+ { + var command = "SELECT MAX(TimeStamp) FROM Readings"; + var reading = SqlOperator.GetReadings(command)[0]; - if (latestReading != null) return Ok(latestReading); + 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) { @@ -61,8 +70,10 @@ namespace AirPollutionWebApi.Controllers } public IHttpActionResult DeleteReading(int id) - { - Reading reading = SqlOperator.GetReadingById(id); + {
+ var command = $"SELECT * FROM Readings WHERE Id={id}";
+ + Reading reading = SqlOperator.GetReadings(command)[0]; if (reading == null) { return NotFound(); |