From c8f0195ccc06575349c0e675c23276ac089bf9c7 Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Fri, 1 Dec 2017 14:48:19 +0100 Subject: Changed name of the project. --- .../Controllers/ReadingsController.cs | 88 ++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 PollutometerWebApi/Controllers/ReadingsController.cs (limited to 'PollutometerWebApi/Controllers/ReadingsController.cs') 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 -- cgit v1.2.3 From b7757c89b6576cfc33579f1dec118086e159e03e Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Fri, 1 Dec 2017 14:56:49 +0100 Subject: Added method for sending e-mail. --- PollutometerWebApi/Controllers/ReadingsController.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'PollutometerWebApi/Controllers/ReadingsController.cs') diff --git a/PollutometerWebApi/Controllers/ReadingsController.cs b/PollutometerWebApi/Controllers/ReadingsController.cs index b692a9c..cb91c71 100644 --- a/PollutometerWebApi/Controllers/ReadingsController.cs +++ b/PollutometerWebApi/Controllers/ReadingsController.cs @@ -7,7 +7,10 @@ namespace PollutometerWebApi.Controllers { public class ReadingsController : ApiController { - public ReadingsController() { } + public ReadingsController() + { + EmailSender.SendEmail(); + } public IHttpActionResult GetAllReadings() { -- cgit v1.2.3 From 79bf391df757c50374a5acde3008860d48295926 Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Fri, 1 Dec 2017 17:56:38 +0100 Subject: Added periodical checking of pollution and sending e-mail. --- PollutometerWebApi/Controllers/ReadingsController.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'PollutometerWebApi/Controllers/ReadingsController.cs') diff --git a/PollutometerWebApi/Controllers/ReadingsController.cs b/PollutometerWebApi/Controllers/ReadingsController.cs index cb91c71..9767645 100644 --- a/PollutometerWebApi/Controllers/ReadingsController.cs +++ b/PollutometerWebApi/Controllers/ReadingsController.cs @@ -1,4 +1,5 @@ using System; +using System.Threading.Tasks; using System.Web.Http; using PollutometerWebApi.Models; using PollutometerWebApi.Singletons; @@ -7,13 +8,12 @@ namespace PollutometerWebApi.Controllers { public class ReadingsController : ApiController { - public ReadingsController() - { - EmailSender.SendEmail(); - } + public ReadingsController() {} public IHttpActionResult GetAllReadings() - { + { + Task.Run(() => EmailService.Start()); + var command = "SELECT * FROM Readings"; var readings = SqlOperator.GetReadings(command); -- cgit v1.2.3 From a3ad4472de9096ec0a12d15097048578b3514d4b Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Mon, 4 Dec 2017 10:14:32 +0100 Subject: Improved calculating of AQI and sending e-mail. --- PollutometerWebApi/Controllers/ReadingsController.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'PollutometerWebApi/Controllers/ReadingsController.cs') diff --git a/PollutometerWebApi/Controllers/ReadingsController.cs b/PollutometerWebApi/Controllers/ReadingsController.cs index 9767645..f915e17 100644 --- a/PollutometerWebApi/Controllers/ReadingsController.cs +++ b/PollutometerWebApi/Controllers/ReadingsController.cs @@ -12,8 +12,6 @@ namespace PollutometerWebApi.Controllers public IHttpActionResult GetAllReadings() { - Task.Run(() => EmailService.Start()); - var command = "SELECT * FROM Readings"; var readings = SqlOperator.GetReadings(command); @@ -67,7 +65,10 @@ namespace PollutometerWebApi.Controllers { if (reading != null) { - SqlOperator.PostReading(reading); + SqlOperator.PostReading(reading); + var aqi = AqiCalculator.CalculateAqi(reading); + if (aqi.Value > 151) + EmailSender.SendEmail(aqi); return Ok(); } else return BadRequest(); -- cgit v1.2.3 From 37985c9923e712dc517b03f3280ce44b28090548 Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Mon, 4 Dec 2017 10:24:05 +0100 Subject: Sending e-mail if value is bigger or equal 151. --- PollutometerWebApi/Controllers/ReadingsController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'PollutometerWebApi/Controllers/ReadingsController.cs') diff --git a/PollutometerWebApi/Controllers/ReadingsController.cs b/PollutometerWebApi/Controllers/ReadingsController.cs index f915e17..c80599f 100644 --- a/PollutometerWebApi/Controllers/ReadingsController.cs +++ b/PollutometerWebApi/Controllers/ReadingsController.cs @@ -67,7 +67,7 @@ namespace PollutometerWebApi.Controllers { SqlOperator.PostReading(reading); var aqi = AqiCalculator.CalculateAqi(reading); - if (aqi.Value > 151) + if (aqi.Value >= 151) EmailSender.SendEmail(aqi); return Ok(); } -- cgit v1.2.3 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/ReadingsController.cs') 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