From f6f830d066635c91763dd1ae6248c8c189e6fe7e Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Mon, 20 Nov 2017 14:36:20 +0100 Subject: Added WebApi. --- .gitignore | 40 ++++++++ .../AirPollutionWebApi.Tests.csproj | 91 +++++++++++++++++ .../Controllers/HomeControllerTest.cs | 35 +++++++ AirPollutionWebApi.Tests/packages.config | 13 +++ AirPollutionWebApi.sln | 23 +++++ AirPollutionWebApi/AirPollutionWebApi.csproj | 112 +++++++++++++++++++++ AirPollutionWebApi/App_Start/RouteConfig.cs | 19 ++++ AirPollutionWebApi/App_Start/WebApiConfig.cs | 24 +++++ AirPollutionWebApi/Controllers/HomeController.cs | 23 +++++ .../Controllers/ReadingsController.cs | 62 ++++++++++++ AirPollutionWebApi/Global.asax | 1 + AirPollutionWebApi/Global.asax.cs | 17 ++++ AirPollutionWebApi/Models/Reading.cs | 12 +++ AirPollutionWebApi/Singletons/Singleton.cs | 93 +++++++++++++++++ AirPollutionWebApi/Views/Home/Index.cshtml | 1 + AirPollutionWebApi/Views/Shared/Error.cshtml | 5 + AirPollutionWebApi/Views/Shared/_Layout.cshtml | 9 ++ AirPollutionWebApi/Views/Web.config | 30 ++++++ AirPollutionWebApi/Views/_ViewStart.cshtml | 3 + AirPollutionWebApi/Web.config | 16 +++ AirPollutionWebApi/packages.config | 12 +++ 21 files changed, 641 insertions(+) create mode 100755 .gitignore create mode 100644 AirPollutionWebApi.Tests/AirPollutionWebApi.Tests.csproj create mode 100644 AirPollutionWebApi.Tests/Controllers/HomeControllerTest.cs create mode 100644 AirPollutionWebApi.Tests/packages.config create mode 100644 AirPollutionWebApi.sln create mode 100644 AirPollutionWebApi/AirPollutionWebApi.csproj create mode 100644 AirPollutionWebApi/App_Start/RouteConfig.cs create mode 100644 AirPollutionWebApi/App_Start/WebApiConfig.cs create mode 100644 AirPollutionWebApi/Controllers/HomeController.cs create mode 100644 AirPollutionWebApi/Controllers/ReadingsController.cs create mode 100644 AirPollutionWebApi/Global.asax create mode 100644 AirPollutionWebApi/Global.asax.cs create mode 100644 AirPollutionWebApi/Models/Reading.cs create mode 100644 AirPollutionWebApi/Singletons/Singleton.cs create mode 100644 AirPollutionWebApi/Views/Home/Index.cshtml create mode 100644 AirPollutionWebApi/Views/Shared/Error.cshtml create mode 100644 AirPollutionWebApi/Views/Shared/_Layout.cshtml create mode 100644 AirPollutionWebApi/Views/Web.config create mode 100644 AirPollutionWebApi/Views/_ViewStart.cshtml create mode 100644 AirPollutionWebApi/Web.config create mode 100644 AirPollutionWebApi/packages.config 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 @@ + + + + Debug + AnyCPU + {803677F8-DC3E-47DA-8E66-BE10F4F47E3A} + Library + AirPollutionWebApi.Tests + AirPollutionWebApi.Tests + v4.5 + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + + + true + bin\Release + prompt + 4 + + + + + + + + + + + + + + + + ..\packages\Microsoft.AspNet.Razor.3.2.3\lib\net45\System.Web.Razor.dll + + + ..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll + + + ..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.Helpers.dll + + + ..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.Deployment.dll + + + ..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.dll + + + ..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.Razor.dll + + + ..\packages\Microsoft.AspNet.Mvc.5.2.3\lib\net45\System.Web.Mvc.dll + + + ..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll + + + + ..\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll + + + ..\packages\Microsoft.AspNet.WebApi.WebHost.5.2.3\lib\net45\System.Web.Http.WebHost.dll + + + ..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll + + + ..\packages\NUnit.3.9.0\lib\net45\nunit.framework.dll + + + + + {22C32F4C-0DEB-40EA-9D56-48942CBB2A07} + AirPollutionWebApi + + + + + + + + + + \ 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 @@ + + + + + + + + + + + + + \ 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 @@ + + + + Debug + AnyCPU + {22C32F4C-0DEB-40EA-9D56-48942CBB2A07} + {349C5851-65DF-11DA-9384-00065B846F21};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + Library + AirPollutionWebApi + AirPollutionWebApi + v4.5 + + + true + full + false + bin + DEBUG; + prompt + 4 + + + true + bin + prompt + 4 + + + + + + + + + + + + + + + + ..\packages\Microsoft.AspNet.Razor.3.2.3\lib\net45\System.Web.Razor.dll + + + ..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll + + + ..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.Helpers.dll + + + ..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.Deployment.dll + + + ..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.dll + + + ..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.Razor.dll + + + ..\packages\Microsoft.AspNet.Mvc.5.2.3\lib\net45\System.Web.Mvc.dll + + + ..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll + + + + ..\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll + + + ..\packages\Microsoft.AspNet.WebApi.WebHost.5.2.3\lib\net45\System.Web.Http.WebHost.dll + + + ..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll + + + + + + + + + + + + + Global.asax + + + + + + + + + + + + + + + + + + + + + + + + + + \ 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 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 Readings = new List(); + + Singleton() + { + GetData(); + } + + public static Singleton Instance + { + get + { + if (instance == null) + { + instance = new Singleton(); + } + return instance; + } + } + + void GetData() + { + Readings = new List(); + 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 @@ +

Welcome to ASP.NET MVC @ViewData["Version"] on @ViewData["Runtime"]!

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 + +
+

An error occurred while processing your request.

+
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 @@ + + + + @ViewBag.Title + + + @RenderBody() + + 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 @@ + + + + +
+
+ + + + + + + + + + + + + + + + + + + + + + + + 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 @@ + + + + + + + + + + \ 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 @@ + + + + + + + + + + + + \ No newline at end of file -- cgit v1.2.3