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. --- PollutometerWebApi/App_Start/WebApiConfig.cs | 46 ++--- PollutometerWebApi/AqiCalculator.cs | 188 ++++++++++----------- .../Controllers/ReadingsController.cs | 127 +++++++------- PollutometerWebApi/PollutometerWebApi.csproj | 47 +++++- PollutometerWebApi/SqlOperator.cs | 5 +- PollutometerWebApi/Views/Web.config | 2 +- 6 files changed, 221 insertions(+), 194 deletions(-) (limited to 'PollutometerWebApi') diff --git a/PollutometerWebApi/App_Start/WebApiConfig.cs b/PollutometerWebApi/App_Start/WebApiConfig.cs index 8c2725a..d3b2dca 100644 --- a/PollutometerWebApi/App_Start/WebApiConfig.cs +++ b/PollutometerWebApi/App_Start/WebApiConfig.cs @@ -1,24 +1,24 @@ using System.Net.Http.Formatting; -using System.Web.Http; - -namespace PollutometerWebApi -{ - public static class WebApiConfig - { - public static void Register(HttpConfiguration config) - { - // Web API configuration and services - config.Formatters.Clear(); - config.Formatters.Add(new JsonMediaTypeFormatter()); - - // Web API routes - config.MapHttpAttributeRoutes(); - - config.Routes.MapHttpRoute( - name: "DefaultApi", - routeTemplate: "api/{controller}/{id}", - defaults: new { id = RouteParameter.Optional } - ); - } - } -} +using System.Web.Http; + +namespace PollutometerWebApi +{ + public static class WebApiConfig + { + public static void Register(HttpConfiguration config) + { + // Web API configuration and services + config.Formatters.Clear(); + config.Formatters.Add(new JsonMediaTypeFormatter()); + + // Web API routes + config.MapHttpAttributeRoutes(); + + config.Routes.MapHttpRoute( + name: "DefaultApi", + routeTemplate: "api/{controller}/{id}", + defaults: new { id = RouteParameter.Optional } + ); + } + } +} diff --git a/PollutometerWebApi/AqiCalculator.cs b/PollutometerWebApi/AqiCalculator.cs index e00d792..58f6194 100644 --- a/PollutometerWebApi/AqiCalculator.cs +++ b/PollutometerWebApi/AqiCalculator.cs @@ -1,102 +1,102 @@ -using PollutometerWebApi.Models; - -namespace PollutometerWebApi -{ - public static class AqiCalculator - { - public static Aqi CalculateAqi(Reading reading) - { - double[,,] breakpoints = - { - { - {0, 4.4}, - {4.5, 9.4}, - {9.5, 12.4}, - {12.5, 15.4}, - {15.5, 30.4}, - {30.5, 40.4}, - {40.5, 50.4} - }, - { - {0.000, 0.034}, - {0.035, 0.144}, - {0.145, 0.224}, - {0.225, 0.304}, - {0.305, 0.604}, - {0.605, 0.804}, - {0.805, 1.004} - }, - { - {0, 0.05}, - {0.08, 0.10}, - {0.15, 0.20}, - {0.25, 0.31}, - {0.65, 1.24}, - {1.25, 1.64}, - {1.65, 2.04} - }, - { - {0, 50}, - {51, 100}, - {101, 150}, - {151, 200}, - {201, 300}, - {301, 400}, - {401, 500} - } - }; - +using PollutometerWebApi.Models; + +namespace PollutometerWebApi +{ + public static class AqiCalculator + { + public static Aqi CalculateAqi(Reading reading) + { + double[,,] breakpoints = + { + { + {0, 4.4}, + {4.5, 9.4}, + {9.5, 12.4}, + {12.5, 15.4}, + {15.5, 30.4}, + {30.5, 40.4}, + {40.5, 50.4} + }, + { + {0.000, 0.034}, + {0.035, 0.144}, + {0.145, 0.224}, + {0.225, 0.304}, + {0.305, 0.604}, + {0.605, 0.804}, + {0.805, 1.004} + }, + { + {0, 0.05}, + {0.08, 0.10}, + {0.15, 0.20}, + {0.25, 0.31}, + {0.65, 1.24}, + {1.25, 1.64}, + {1.65, 2.04} + }, + { + {0, 50}, + {51, 100}, + {101, 150}, + {151, 200}, + {201, 300}, + {301, 400}, + {401, 500} + } + }; + double i, c = 0, cLow = 0, cHigh = 0, iLow = 0, iHigh = 0; - Aqi aqi = new Aqi(); - + Aqi aqi = new Aqi(); + for (int x = 0; x < 4; x++) - { - switch (x) - { - case 0: - c = reading.Co; - break; - case 1: - c = reading.So; - break; - case 2: - c = reading.No; - break; - } - - for (int y = 0; y < 7; y++) - { - if (c >= breakpoints[x, y, 0] && c <= breakpoints[x, y, 1]) - { - cLow = breakpoints[x, y, 0]; - cHigh = breakpoints[x, y, 1]; - iLow = breakpoints[3, y, 0]; - iHigh = breakpoints[3, y, 1]; - break; - } + { + switch (x) + { + case 0: + c = reading.Co; + break; + case 1: + c = reading.So; + break; + case 2: + c = reading.No; + break; + } + + for (int y = 0; y < 7; y++) + { + if (c >= breakpoints[x, y, 0] && c <= breakpoints[x, y, 1]) + { + cLow = breakpoints[x, y, 0]; + cHigh = breakpoints[x, y, 1]; + iLow = breakpoints[3, y, 0]; + iHigh = breakpoints[3, y, 1]; + break; + } } i = (iHigh - iLow) / (cHigh - cLow) * (c - cLow) + iLow; - if (i > aqi.Value) + if (i > aqi.Value) { - aqi.Value = i; - switch (x) - { - case 0: - aqi.GasName = "CO"; - break; - case 1: - aqi.GasName = "SO"; - break; - case 2: - aqi.GasName = "NO"; - break; - } + aqi.Value = i; + switch (x) + { + case 0: + aqi.GasName = "CO"; + break; + case 1: + aqi.GasName = "SO"; + break; + case 2: + aqi.GasName = "NO"; + break; + } } - } - - return aqi; - } - } -} + } + + return aqi; + } + } +} 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 diff --git a/PollutometerWebApi/PollutometerWebApi.csproj b/PollutometerWebApi/PollutometerWebApi.csproj index f2ef659..6e5eb45 100644 --- a/PollutometerWebApi/PollutometerWebApi.csproj +++ b/PollutometerWebApi/PollutometerWebApi.csproj @@ -1,5 +1,6 @@ - + + Debug AnyCPU @@ -9,6 +10,18 @@ PollutometerWebApi PollutometerWebApi v4.6.2 + + + + + 4.0 + true + + + + + + true @@ -76,7 +89,6 @@ - @@ -94,7 +106,9 @@ - + + Designer + @@ -103,14 +117,35 @@ + + 10.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + - - + + + + + + + True + True + 0 + / + http://localhost:50114/ + False + False + + + False + + + - + \ No newline at end of file diff --git a/PollutometerWebApi/SqlOperator.cs b/PollutometerWebApi/SqlOperator.cs index d85aa3d..1e296a1 100644 --- a/PollutometerWebApi/SqlOperator.cs +++ b/PollutometerWebApi/SqlOperator.cs @@ -1,9 +1,8 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Data.SqlClient; using PollutometerWebApi.Models; -namespace PollutometerWebApi.Singletons +namespace PollutometerWebApi { public static class SqlOperator { diff --git a/PollutometerWebApi/Views/Web.config b/PollutometerWebApi/Views/Web.config index c23d873..35da8d0 100644 --- a/PollutometerWebApi/Views/Web.config +++ b/PollutometerWebApi/Views/Web.config @@ -14,7 +14,7 @@ - + -- cgit v1.2.3