diff options
author | marcinzelent <zelent.marcin@protonmail.com> | 2017-11-20 14:36:20 +0100 |
---|---|---|
committer | marcinzelent <zelent.marcin@protonmail.com> | 2017-11-20 14:36:20 +0100 |
commit | f6f830d066635c91763dd1ae6248c8c189e6fe7e (patch) | |
tree | 7cf4d3315bda5829046e029cf52d2e2d9a167380 | |
parent | 7ff8524f57d6f4641e4a3dbea6ff6dbe9e346deb (diff) |
Added WebApi.
21 files changed, 641 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..4e82d27 --- /dev/null +++ b/.gitignore @@ -0,0 +1,40 @@ +# Autosave files
+*~
+
+# build
+[Oo]bj/
+[Bb]in/
+packages/
+TestResults/
+
+# globs
+Makefile.in
+*.DS_Store
+*.sln.cache
+*.suo
+*.cache
+*.pidb
+*.userprefs
+*.usertasks
+config.log
+config.make
+config.status
+aclocal.m4
+install-sh
+autom4te.cache/
+*.user
+*.tar.gz
+tarballs/
+test-results/
+Thumbs.db
+
+# Mac bundle stuff
+*.dmg
+*.app
+
+# resharper
+*_Resharper.*
+*.Resharper
+
+# dotCover
+*.dotCover
diff --git a/AirPollutionWebApi.Tests/AirPollutionWebApi.Tests.csproj b/AirPollutionWebApi.Tests/AirPollutionWebApi.Tests.csproj new file mode 100644 index 0000000..a3f8082 --- /dev/null +++ b/AirPollutionWebApi.Tests/AirPollutionWebApi.Tests.csproj @@ -0,0 +1,91 @@ +<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProjectGuid>{803677F8-DC3E-47DA-8E66-BE10F4F47E3A}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <RootNamespace>AirPollutionWebApi.Tests</RootNamespace>
+ <AssemblyName>AirPollutionWebApi.Tests</AssemblyName>
+ <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug</OutputPath>
+ <DefineConstants>DEBUG;</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release</OutputPath>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="System" />
+ <Reference Include="System.Web" />
+ <Reference Include="System.Xml" />
+ <Reference Include="System.Core" />
+ <Reference Include="System.Web.Services" />
+ <Reference Include="System.Web.Routing" />
+ <Reference Include="System.Web.Extensions" />
+ <Reference Include="System.Web.Abstractions" />
+ <Reference Include="System.Web.DynamicData" />
+ <Reference Include="System.ComponentModel.DataAnnotations" />
+ <Reference Include="System.Xml.Linq" />
+ <Reference Include="Microsoft.CSharp" />
+ <Reference Include="System.Web.Razor">
+ <HintPath>..\packages\Microsoft.AspNet.Razor.3.2.3\lib\net45\System.Web.Razor.dll</HintPath>
+ </Reference>
+ <Reference Include="Microsoft.Web.Infrastructure">
+ <HintPath>..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Web.Helpers">
+ <HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.Helpers.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Web.WebPages.Deployment">
+ <HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.Deployment.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Web.WebPages">
+ <HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Web.WebPages.Razor">
+ <HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.Razor.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Web.Mvc">
+ <HintPath>..\packages\Microsoft.AspNet.Mvc.5.2.3\lib\net45\System.Web.Mvc.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Net.Http.Formatting">
+ <HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Net.Http" />
+ <Reference Include="System.Web.Http">
+ <HintPath>..\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Web.Http.WebHost">
+ <HintPath>..\packages\Microsoft.AspNet.WebApi.WebHost.5.2.3\lib\net45\System.Web.Http.WebHost.dll</HintPath>
+ </Reference>
+ <Reference Include="Newtonsoft.Json">
+ <HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
+ </Reference>
+ <Reference Include="nunit.framework">
+ <HintPath>..\packages\NUnit.3.9.0\lib\net45\nunit.framework.dll</HintPath>
+ </Reference>
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\AirPollutionWebApi\AirPollutionWebApi.csproj">
+ <Project>{22C32F4C-0DEB-40EA-9D56-48942CBB2A07}</Project>
+ <Name>AirPollutionWebApi</Name>
+ </ProjectReference>
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="Controllers\HomeControllerTest.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <None Include="packages.config" />
+ </ItemGroup>
+ <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+</Project>
\ No newline at end of file diff --git a/AirPollutionWebApi.Tests/Controllers/HomeControllerTest.cs b/AirPollutionWebApi.Tests/Controllers/HomeControllerTest.cs new file mode 100644 index 0000000..fdae606 --- /dev/null +++ b/AirPollutionWebApi.Tests/Controllers/HomeControllerTest.cs @@ -0,0 +1,35 @@ +using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Web.Mvc; +using AirPollutionWebApi; +using AirPollutionWebApi.Controllers; + +namespace AirPollutionWebApi.Tests.Controllers +{ + [TestFixture] + public class HomeControllerTest + { + [Test] + public void Index() + { + // Arrange + var controller = new HomeController(); + + // Act + var result = (ViewResult)controller.Index(); + + var mvcName = typeof(Controller).Assembly.GetName(); + var isMono = Type.GetType("Mono.Runtime") != null; + + var expectedVersion = mvcName.Version.Major + "." + mvcName.Version.Minor; + var expectedRuntime = isMono ? "Mono" : ".NET"; + + // Assert + Assert.AreEqual(expectedVersion, result.ViewData["Version"]); + Assert.AreEqual(expectedRuntime, result.ViewData["Runtime"]); + } + } +} diff --git a/AirPollutionWebApi.Tests/packages.config b/AirPollutionWebApi.Tests/packages.config new file mode 100644 index 0000000..89948e8 --- /dev/null +++ b/AirPollutionWebApi.Tests/packages.config @@ -0,0 +1,13 @@ +<?xml version="1.0" encoding="utf-8"?> +<packages> + <package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net45" /> + <package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net45" /> + <package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net45" /> + <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" /> + <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" /> + <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net45" /> + <package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net45" /> + <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" /> + <package id="Newtonsoft.Json" version="10.0.3" targetFramework="net45" /> + <package id="NUnit" version="3.9.0" targetFramework="net45" /> +</packages>
\ No newline at end of file diff --git a/AirPollutionWebApi.sln b/AirPollutionWebApi.sln new file mode 100644 index 0000000..360c4e4 --- /dev/null +++ b/AirPollutionWebApi.sln @@ -0,0 +1,23 @@ +
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio 2012
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AirPollutionWebApi", "AirPollutionWebApi\AirPollutionWebApi.csproj", "{22C32F4C-0DEB-40EA-9D56-48942CBB2A07}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AirPollutionWebApi.Tests", "AirPollutionWebApi.Tests\AirPollutionWebApi.Tests.csproj", "{803677F8-DC3E-47DA-8E66-BE10F4F47E3A}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {22C32F4C-0DEB-40EA-9D56-48942CBB2A07}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {22C32F4C-0DEB-40EA-9D56-48942CBB2A07}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {22C32F4C-0DEB-40EA-9D56-48942CBB2A07}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {22C32F4C-0DEB-40EA-9D56-48942CBB2A07}.Release|Any CPU.Build.0 = Release|Any CPU
+ {803677F8-DC3E-47DA-8E66-BE10F4F47E3A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {803677F8-DC3E-47DA-8E66-BE10F4F47E3A}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {803677F8-DC3E-47DA-8E66-BE10F4F47E3A}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {803677F8-DC3E-47DA-8E66-BE10F4F47E3A}.Release|Any CPU.Build.0 = Release|Any CPU
+ EndGlobalSection
+EndGlobal
diff --git a/AirPollutionWebApi/AirPollutionWebApi.csproj b/AirPollutionWebApi/AirPollutionWebApi.csproj new file mode 100644 index 0000000..745c24d --- /dev/null +++ b/AirPollutionWebApi/AirPollutionWebApi.csproj @@ -0,0 +1,112 @@ +<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProjectGuid>{22C32F4C-0DEB-40EA-9D56-48942CBB2A07}</ProjectGuid>
+ <ProjectTypeGuids>{349C5851-65DF-11DA-9384-00065B846F21};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+ <OutputType>Library</OutputType>
+ <RootNamespace>AirPollutionWebApi</RootNamespace>
+ <AssemblyName>AirPollutionWebApi</AssemblyName>
+ <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin</OutputPath>
+ <DefineConstants>DEBUG;</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <Optimize>true</Optimize>
+ <OutputPath>bin</OutputPath>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="System" />
+ <Reference Include="System.Web" />
+ <Reference Include="System.Xml" />
+ <Reference Include="System.Core" />
+ <Reference Include="System.Web.Services" />
+ <Reference Include="System.Web.Routing" />
+ <Reference Include="System.Web.Extensions" />
+ <Reference Include="System.Web.Abstractions" />
+ <Reference Include="System.Web.DynamicData" />
+ <Reference Include="System.ComponentModel.DataAnnotations" />
+ <Reference Include="System.Xml.Linq" />
+ <Reference Include="Microsoft.CSharp" />
+ <Reference Include="System.Web.Razor">
+ <HintPath>..\packages\Microsoft.AspNet.Razor.3.2.3\lib\net45\System.Web.Razor.dll</HintPath>
+ </Reference>
+ <Reference Include="Microsoft.Web.Infrastructure">
+ <HintPath>..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Web.Helpers">
+ <HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.Helpers.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Web.WebPages.Deployment">
+ <HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.Deployment.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Web.WebPages">
+ <HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Web.WebPages.Razor">
+ <HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.Razor.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Web.Mvc">
+ <HintPath>..\packages\Microsoft.AspNet.Mvc.5.2.3\lib\net45\System.Web.Mvc.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Net.Http.Formatting">
+ <HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Net.Http" />
+ <Reference Include="System.Web.Http">
+ <HintPath>..\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Web.Http.WebHost">
+ <HintPath>..\packages\Microsoft.AspNet.WebApi.WebHost.5.2.3\lib\net45\System.Web.Http.WebHost.dll</HintPath>
+ </Reference>
+ <Reference Include="Newtonsoft.Json">
+ <HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Data" />
+ </ItemGroup>
+ <ItemGroup>
+ <Folder Include="Content\" />
+ <Folder Include="Models\" />
+ <Folder Include="Scripts\" />
+ <Folder Include="Singletons\" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="Controllers\HomeController.cs" />
+ <Compile Include="Global.asax.cs">
+ <DependentUpon>Global.asax</DependentUpon>
+ </Compile>
+ <Compile Include="App_Start\WebApiConfig.cs" />
+ <Compile Include="App_Start\RouteConfig.cs" />
+ <Compile Include="Controllers\ReadingsController.cs" />
+ <Compile Include="Models\Reading.cs" />
+ <Compile Include="Singletons\Singleton.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <Content Include="Views\Web.config" />
+ <Content Include="Views\Home\Index.cshtml" />
+ <Content Include="Views\Shared\_Layout.cshtml" />
+ <Content Include="Views\Shared\Error.cshtml" />
+ <Content Include="Views\_ViewStart.cshtml" />
+ <Content Include="Web.config" />
+ <Content Include="Global.asax" />
+ <Content Include="packages.config" />
+ </ItemGroup>
+ <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+ <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>
+ </ProjectExtensions>
+</Project>
\ No newline at end of file diff --git a/AirPollutionWebApi/App_Start/RouteConfig.cs b/AirPollutionWebApi/App_Start/RouteConfig.cs new file mode 100644 index 0000000..bc8f39a --- /dev/null +++ b/AirPollutionWebApi/App_Start/RouteConfig.cs @@ -0,0 +1,19 @@ +using System.Web.Mvc; +using System.Web.Routing; + +namespace AirPollutionWebApi +{ + public class RouteConfig + { + public static void RegisterRoutes(RouteCollection routes) + { + routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); + + routes.MapRoute( + name: "Default", + url: "{controller}/{action}/{id}", + defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } + ); + } + } +} diff --git a/AirPollutionWebApi/App_Start/WebApiConfig.cs b/AirPollutionWebApi/App_Start/WebApiConfig.cs new file mode 100644 index 0000000..6adb366 --- /dev/null +++ b/AirPollutionWebApi/App_Start/WebApiConfig.cs @@ -0,0 +1,24 @@ +using System.Net.Http.Formatting;
+using System.Web.Http; + +namespace AirPollutionWebApi +{ + 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/AirPollutionWebApi/Controllers/HomeController.cs b/AirPollutionWebApi/Controllers/HomeController.cs new file mode 100644 index 0000000..6a83056 --- /dev/null +++ b/AirPollutionWebApi/Controllers/HomeController.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.Web.Mvc; +using System.Web.Mvc.Ajax; + +namespace AirPollutionWebApi.Controllers +{ + public class HomeController : Controller + { + public ActionResult Index() + { + var mvcName = typeof(Controller).Assembly.GetName(); + var isMono = Type.GetType("Mono.Runtime") != null; + + ViewData["Version"] = mvcName.Version.Major + "." + mvcName.Version.Minor; + ViewData["Runtime"] = isMono ? "Mono" : ".NET"; + + return View(); + } + } +} diff --git a/AirPollutionWebApi/Controllers/ReadingsController.cs b/AirPollutionWebApi/Controllers/ReadingsController.cs new file mode 100644 index 0000000..3ffe624 --- /dev/null +++ b/AirPollutionWebApi/Controllers/ReadingsController.cs @@ -0,0 +1,62 @@ +using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Web;
+using System.Web.Http; +using System.Web.Mvc;
+using AirPollutionWebApi.Models; +using AirPollutionWebApi.Singletons; + +namespace AirPollutionWebApi.Controllers
+{
+ public class ReadingsController : ApiController
+ { + public ReadingsController() { } + + public IEnumerable<Reading> GetAllReadings() + { + return Singleton.Instance.Readings; + } + + //public IHttpActionResult GetReading(int id) + //{ + // var customer = Singleton.Instance.Readings.FirstOrDefault((p) => p.TimeStamp == id); + + // if (customer != null) return Ok(customer); + // else return NotFound(); + //} + + //public IHttpActionResult PutReading(int id, Reading customer) + //{ + // if (customer != null) + // { + // Singleton.Instance.PutData(id, customer); + // return Ok(); + // } + // else return BadRequest(); + //} + + public IHttpActionResult PostReading(Reading customer) + { + if (customer != null) + { + Singleton.Instance.PostData(customer); + return Ok(); + } + else return BadRequest(); + } + + //public IHttpActionResult DeleteReading(int id) + //{ + // Reading customer = Singleton.Instance.Readings.Find((p) => p.Id == id); + // if (customer == null) + // { + // return NotFound(); + // } + + // Singleton.Instance.DeleteData(id); + + // return Ok(customer); + //}
+ }
+}
\ No newline at end of file diff --git a/AirPollutionWebApi/Global.asax b/AirPollutionWebApi/Global.asax new file mode 100644 index 0000000..9fb00cd --- /dev/null +++ b/AirPollutionWebApi/Global.asax @@ -0,0 +1 @@ +<%@ Application Inherits="AirPollutionWebApi.Global" %> diff --git a/AirPollutionWebApi/Global.asax.cs b/AirPollutionWebApi/Global.asax.cs new file mode 100644 index 0000000..aa5b94f --- /dev/null +++ b/AirPollutionWebApi/Global.asax.cs @@ -0,0 +1,17 @@ +using System.Web; +using System.Web.Mvc; +using System.Web.Routing; +using System.Web.Http; + +namespace AirPollutionWebApi +{ + public class Global : HttpApplication + { + protected void Application_Start() + { + AreaRegistration.RegisterAllAreas(); + GlobalConfiguration.Configure(WebApiConfig.Register); + RouteConfig.RegisterRoutes(RouteTable.Routes); + } + } +} diff --git a/AirPollutionWebApi/Models/Reading.cs b/AirPollutionWebApi/Models/Reading.cs new file mode 100644 index 0000000..b829711 --- /dev/null +++ b/AirPollutionWebApi/Models/Reading.cs @@ -0,0 +1,12 @@ +using System; + +namespace AirPollutionWebApi.Models +{ + public class Reading + { + public DateTime TimeStamp { get; set; } + public int Co { get; set; } + public int No { get; set; } + public int So { get; set; } + } +} diff --git a/AirPollutionWebApi/Singletons/Singleton.cs b/AirPollutionWebApi/Singletons/Singleton.cs new file mode 100644 index 0000000..5da91ad --- /dev/null +++ b/AirPollutionWebApi/Singletons/Singleton.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using System.Data.SqlClient; +using AirPollutionWebApi.Models; + +namespace AirPollutionWebApi.Singletons +{ + public class Singleton + { + static Singleton instance; + const string ConnectionString = "Server=tcp:forschool.database.windows.net,1433;Initial Catalog=schooldb;Persist Security Info=False;User ID=***REMOVED***;Password=***REMOVED***;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"; + + public List<Reading> Readings = new List<Reading>(); + + Singleton() + { + GetData(); + } + + public static Singleton Instance + { + get + { + if (instance == null) + { + instance = new Singleton(); + } + return instance; + } + } + + void GetData() + { + Readings = new List<Reading>(); + using (SqlConnection databaseConnection = new SqlConnection(ConnectionString)) + { + string command = "SELECT * FROM Readings"; + databaseConnection.Open(); + SqlCommand selectCommand = new SqlCommand(command, databaseConnection); + var reader = selectCommand.ExecuteReader(); + while (reader.Read()) + { + Readings.Add(new Reading + { + TimeStamp = reader.GetDateTime(0), + Co = reader.GetInt32(1), + No = reader.GetInt32(2), + So = reader.GetInt32(3) + }); + } + } + } + + //public void PutData(int id, Reading reading) + //{ + // using (SqlConnection dbCon = new SqlConnection(ConnectionString)) + // { + // dbCon.Open(); + // string query = $"UPDATE Readings SET FirstName='{reading.FirstName}', LastName='{reading.LastName}', Year='{reading.Year}' WHERE Id={id};"; + // var cmd = new SqlCommand(query, dbCon); + // cmd.ExecuteNonQuery(); + // dbCon.Close(); + // } + // GetData(); + //} + + public void PostData(Reading reading) + { + using (SqlConnection dbCon = new SqlConnection(ConnectionString)) + { + dbCon.Open(); + string query = $"INSERT INTO Readings (TimeStamp,Co,No,So) VALUES('{reading.TimeStamp}',{reading.Co},{reading.No},{reading.So});"; + var cmd = new SqlCommand(query, dbCon); + cmd.ExecuteNonQuery(); + dbCon.Close(); + } + GetData(); + } + + public void DeleteData(int id) + { + using (SqlConnection dbCon = new SqlConnection(ConnectionString)) + { + dbCon.Open(); + string query = $"DELETE FROM Readings WHERE Id={id};"; + var cmd = new SqlCommand(query, dbCon); + cmd.ExecuteNonQuery(); + dbCon.Close(); + } + GetData(); + } + } +}
\ No newline at end of file diff --git a/AirPollutionWebApi/Views/Home/Index.cshtml b/AirPollutionWebApi/Views/Home/Index.cshtml new file mode 100644 index 0000000..2501eee --- /dev/null +++ b/AirPollutionWebApi/Views/Home/Index.cshtml @@ -0,0 +1 @@ +<h2>Welcome to ASP.NET MVC @ViewData["Version"] on @ViewData["Runtime"]!</h2> diff --git a/AirPollutionWebApi/Views/Shared/Error.cshtml b/AirPollutionWebApi/Views/Shared/Error.cshtml new file mode 100644 index 0000000..20ec06a --- /dev/null +++ b/AirPollutionWebApi/Views/Shared/Error.cshtml @@ -0,0 +1,5 @@ +@model System.Web.Mvc.HandleErrorInfo + +<div> + <h2>An error occurred while processing your request.</h2> +</div> diff --git a/AirPollutionWebApi/Views/Shared/_Layout.cshtml b/AirPollutionWebApi/Views/Shared/_Layout.cshtml new file mode 100644 index 0000000..54f6729 --- /dev/null +++ b/AirPollutionWebApi/Views/Shared/_Layout.cshtml @@ -0,0 +1,9 @@ +<!DOCTYPE html> +<html> +<head> + <title>@ViewBag.Title</title> +</head> +<body> + @RenderBody() +</body> +</html> diff --git a/AirPollutionWebApi/Views/Web.config b/AirPollutionWebApi/Views/Web.config new file mode 100644 index 0000000..c23d873 --- /dev/null +++ b/AirPollutionWebApi/Views/Web.config @@ -0,0 +1,30 @@ +<?xml version="1.0"?> +<configuration> + <configSections> + <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> + <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" /> + <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" /> + </sectionGroup> + </configSections> + <system.web.webPages.razor> + <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> + <pages pageBaseType="System.Web.Mvc.WebViewPage"> + <namespaces> + <add namespace="System.Web.Mvc" /> + <add namespace="System.Web.Mvc.Ajax" /> + <add namespace="System.Web.Mvc.Html" /> + <add namespace="System.Web.Routing" /> + <add namespace="AirPollutionWebApi" /> + </namespaces> + </pages> + </system.web.webPages.razor> + <appSettings> + <add key="webpages:Enabled" value="false" /> + </appSettings> + <system.webServer> + <handlers> + <remove name="BlockViewHandler" /> + <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" /> + </handlers> + </system.webServer> +</configuration> diff --git a/AirPollutionWebApi/Views/_ViewStart.cshtml b/AirPollutionWebApi/Views/_ViewStart.cshtml new file mode 100644 index 0000000..2de6241 --- /dev/null +++ b/AirPollutionWebApi/Views/_ViewStart.cshtml @@ -0,0 +1,3 @@ +@{ + Layout = "~/Views/Shared/_Layout.cshtml"; +} diff --git a/AirPollutionWebApi/Web.config b/AirPollutionWebApi/Web.config new file mode 100644 index 0000000..318bf1c --- /dev/null +++ b/AirPollutionWebApi/Web.config @@ -0,0 +1,16 @@ +<?xml version="1.0"?> +<!-- +Web.config file for AirPollutionWebApi. + +The settings that can be used in this file are documented at +http://www.mono-project.com/Config_system.web and +http://msdn2.microsoft.com/en-us/library/b5ysx397.aspx +--> +<configuration> + <system.web> + <compilation debug="true" targetFramework="4.5"> + <assemblies /> + </compilation> + <httpRuntime targetFramework="4.5" /> + </system.web> +</configuration>
\ No newline at end of file diff --git a/AirPollutionWebApi/packages.config b/AirPollutionWebApi/packages.config new file mode 100644 index 0000000..721eb8b --- /dev/null +++ b/AirPollutionWebApi/packages.config @@ -0,0 +1,12 @@ +<?xml version="1.0" encoding="utf-8"?> +<packages> + <package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net45" /> + <package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net45" /> + <package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net45" /> + <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" /> + <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" /> + <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net45" /> + <package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net45" /> + <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" /> + <package id="Newtonsoft.Json" version="10.0.3" targetFramework="net45" /> +</packages>
\ No newline at end of file |