diff options
author | marcinzelent <zelent.marcin@gmail.com> | 2017-11-27 12:41:19 +0100 |
---|---|---|
committer | marcinzelent <zelent.marcin@gmail.com> | 2017-11-27 12:41:19 +0100 |
commit | 10bc4d0f275f5183da672ece1d7dfa5da8375b3f (patch) | |
tree | 6b04b695a19f5b8a487735b8286b749736b7a106 /AirPollutionWebApi/Controllers/ReadingsController.cs | |
parent | 5f56622fdfe457df69ecaeb81c8253cf96cda796 (diff) |
Refactored code and added method in controller for getting readings from last week.
Diffstat (limited to 'AirPollutionWebApi/Controllers/ReadingsController.cs')
-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(); |