From e4e7dbf553bfa17a39830e67d14d3715946beab3 Mon Sep 17 00:00:00 2001 From: Marcin Zelent Date: Tue, 12 Dec 2017 14:24:47 +0100 Subject: Added unit tests and fixed some bugs. --- .../Controllers/ReadingsController.cs | 127 ++++++++++----------- 1 file changed, 60 insertions(+), 67 deletions(-) (limited to 'PollutometerWebApi/Controllers') diff --git a/PollutometerWebApi/Controllers/ReadingsController.cs b/PollutometerWebApi/Controllers/ReadingsController.cs index c80599f..adf4d5d 100644 --- a/PollutometerWebApi/Controllers/ReadingsController.cs +++ b/PollutometerWebApi/Controllers/ReadingsController.cs @@ -1,92 +1,85 @@ using System; -using System.Threading.Tasks; using System.Web.Http; -using PollutometerWebApi.Models; -using PollutometerWebApi.Singletons; - +using PollutometerWebApi.Models; + namespace PollutometerWebApi.Controllers { public class ReadingsController : ApiController - { - public ReadingsController() {} - - public IHttpActionResult GetAllReadings() - { + { + public ReadingsController() {} + + public IHttpActionResult GetAllReadings() + { var command = "SELECT * FROM Readings"; - var readings = SqlOperator.GetReadings(command); - + var readings = SqlOperator.GetReadings(command); + if (readings.Count > 0) return Ok(readings); - else return NotFound(); - } - - public IHttpActionResult GetReading(int id) + 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(); + var command = $"SELECT * FROM Readings WHERE Id={id}"; + var readings = SqlOperator.GetReadings(command); + + if (readings.Count == 1) return Ok(readings[0]); + 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); + "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() + [Route("api/Readings/lastweek")] + public IHttpActionResult GetReadingsFromLastWeek() { - var timeNow = DateTimeOffset.Now.ToUnixTimeSeconds(); + 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) - { + $"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); var aqi = AqiCalculator.CalculateAqi(reading); if (aqi.Value >= 151) - EmailSender.SendEmail(aqi); - return Ok(); - } - else return BadRequest(); - } - - public IHttpActionResult DeleteReading(int id) + EmailSender.SendEmail(aqi); + return Ok(reading); + } + 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); + var readings = SqlOperator.GetReadings(command); + if (readings.Count == 0) return NotFound(); + SqlOperator.DeleteReading(id); + + return Ok(readings[0]); } } } \ No newline at end of file -- cgit v1.2.3