diff options
author | Marcin Zelent <zelent.marcin@protonmail.com> | 2017-12-12 14:24:47 +0100 |
---|---|---|
committer | Marcin Zelent <zelent.marcin@protonmail.com> | 2017-12-12 14:24:47 +0100 |
commit | e4e7dbf553bfa17a39830e67d14d3715946beab3 (patch) | |
tree | bf6c34aaeecb938f9ea11612d214a887ceb5d7ca /PollutometerWebApi | |
parent | 54d1019413958e0ba24b85007c1f5f97ca19a0b3 (diff) |
Added unit tests and fixed some bugs.
Diffstat (limited to 'PollutometerWebApi')
-rw-r--r-- | PollutometerWebApi/App_Start/WebApiConfig.cs | 46 | ||||
-rw-r--r-- | PollutometerWebApi/AqiCalculator.cs | 188 | ||||
-rw-r--r-- | PollutometerWebApi/Controllers/ReadingsController.cs | 127 | ||||
-rw-r--r-- | PollutometerWebApi/PollutometerWebApi.csproj | 47 | ||||
-rw-r--r-- | PollutometerWebApi/SqlOperator.cs | 5 | ||||
-rw-r--r-- | PollutometerWebApi/Views/Web.config | 2 |
6 files changed, 221 insertions, 194 deletions
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 @@ -<?xml version="1.0" encoding="utf-8"?>
+<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
@@ -9,6 +10,18 @@ <RootNamespace>PollutometerWebApi</RootNamespace>
<AssemblyName>PollutometerWebApi</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
+ <FileUpgradeFlags>
+ </FileUpgradeFlags>
+ <UpgradeBackupLocation>
+ </UpgradeBackupLocation>
+ <OldToolsVersion>4.0</OldToolsVersion>
+ <UseIISExpress>true</UseIISExpress>
+ <Use64BitIISExpress />
+ <IISExpressSSLPort />
+ <IISExpressAnonymousAuthentication />
+ <IISExpressWindowsAuthentication />
+ <IISExpressUseClassicPipelineMode />
+ <UseGlobalApplicationHostFile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@@ -76,7 +89,6 @@ </ItemGroup>
<ItemGroup>
<Folder Include="Content\" />
- <Folder Include="Models\" />
<Folder Include="Scripts\" />
</ItemGroup>
<ItemGroup>
@@ -94,7 +106,9 @@ <Compile Include="Models\Aqi.cs" />
</ItemGroup>
<ItemGroup>
- <Content Include="Views\Web.config" />
+ <Content Include="Views\Web.config">
+ <SubType>Designer</SubType>
+ </Content>
<Content Include="Views\Home\Index.cshtml" />
<Content Include="Views\Shared\_Layout.cshtml" />
<Content Include="Views\Shared\Error.cshtml" />
@@ -103,14 +117,35 @@ <Content Include="Global.asax" />
<Content Include="packages.config" />
</ItemGroup>
+ <PropertyGroup>
+ <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
+ <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
+ </PropertyGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
-<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' !=''" />
- <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v11.0\WebApplications\Microsoft.WebApplication.targets" Condition="true" />
+ <Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' !=''" />
+ <Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
+ <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />
<ProjectExtensions>
<MonoDevelop>
<Properties>
<XspParameters Port="8080" Address="127.0.0.1" SslMode="None" SslProtocol="Default" KeyType="None" CertFile="" KeyFile="" PasswordOptions="None" Password="" Verbose="True" />
</Properties>
</MonoDevelop>
+ <VisualStudio>
+ <FlavorProperties GUID="{349C5851-65DF-11DA-9384-00065B846F21}">
+ <WebProjectProperties>
+ <UseIIS>True</UseIIS>
+ <AutoAssignPort>True</AutoAssignPort>
+ <DevelopmentServerPort>0</DevelopmentServerPort>
+ <DevelopmentServerVPath>/</DevelopmentServerVPath>
+ <IISUrl>http://localhost:50114/</IISUrl>
+ <NTLMAuthentication>False</NTLMAuthentication>
+ <UseCustomServer>False</UseCustomServer>
+ <CustomServerUrl>
+ </CustomServerUrl>
+ <SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
+ </WebProjectProperties>
+ </FlavorProperties>
+ </VisualStudio>
</ProjectExtensions>
-</Project>
+</Project>
\ 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 @@ <add namespace="System.Web.Mvc.Ajax" /> <add namespace="System.Web.Mvc.Html" /> <add namespace="System.Web.Routing" /> - <add namespace="AirPollutionWebApi" /> + <add namespace="PollutometerWebApi" /> </namespaces> </pages> </system.web.webPages.razor> |