diff options
Diffstat (limited to 'AirPollutionWebApi')
-rw-r--r-- | AirPollutionWebApi/AirPollutionWebApi.csproj | 3 | ||||
-rw-r--r-- | AirPollutionWebApi/Controllers/ReadingsController.cs | 74 | ||||
-rw-r--r-- | AirPollutionWebApi/Models/Reading.cs | 4 | ||||
-rw-r--r-- | AirPollutionWebApi/SqlOperator.cs (renamed from AirPollutionWebApi/Singletons/Singleton.cs) | 83 |
4 files changed, 84 insertions, 80 deletions
diff --git a/AirPollutionWebApi/AirPollutionWebApi.csproj b/AirPollutionWebApi/AirPollutionWebApi.csproj index a59670c..5a20af0 100644 --- a/AirPollutionWebApi/AirPollutionWebApi.csproj +++ b/AirPollutionWebApi/AirPollutionWebApi.csproj @@ -78,7 +78,6 @@ <Folder Include="Content\" />
<Folder Include="Models\" />
<Folder Include="Scripts\" />
- <Folder Include="Singletons\" />
</ItemGroup>
<ItemGroup>
<Compile Include="Controllers\HomeController.cs" />
@@ -89,7 +88,7 @@ <Compile Include="App_Start\RouteConfig.cs" />
<Compile Include="Controllers\ReadingsController.cs" />
<Compile Include="Models\Reading.cs" />
- <Compile Include="Singletons\Singleton.cs" />
+ <Compile Include="SqlOperator.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Views\Web.config" />
diff --git a/AirPollutionWebApi/Controllers/ReadingsController.cs b/AirPollutionWebApi/Controllers/ReadingsController.cs index c8c8087..e8eb0fa 100644 --- a/AirPollutionWebApi/Controllers/ReadingsController.cs +++ b/AirPollutionWebApi/Controllers/ReadingsController.cs @@ -1,9 +1,5 @@ -using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Web;
-using System.Web.Http; -using System.Web.Mvc;
+using System.Collections.Generic;
+using System.Web.Http;
using AirPollutionWebApi.Models; using AirPollutionWebApi.Singletons; @@ -15,49 +11,49 @@ namespace AirPollutionWebApi.Controllers public IEnumerable<Reading> GetAllReadings() {
- Singleton.Instance.GetData(); - return Singleton.Instance.Readings; + var readings = SqlOperator.GetAllReadings(); + return 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) + public IHttpActionResult GetReading(int id) { - if (customer != null) + var reading = SqlOperator.GetReadingById(id); + + if (reading != null) return Ok(reading); + 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) { - Singleton.Instance.PostData(customer); + SqlOperator.PostReading(reading); 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(); - // } + public IHttpActionResult DeleteReading(int id) + { + Reading reading = SqlOperator.GetReadingById(id); + if (reading == null) + { + return NotFound(); + } - // Singleton.Instance.DeleteData(id); + SqlOperator.DeleteReading(id); - // return Ok(customer); - //}
+ return Ok(reading); + }
}
}
\ No newline at end of file diff --git a/AirPollutionWebApi/Models/Reading.cs b/AirPollutionWebApi/Models/Reading.cs index 4880659..07f4599 100644 --- a/AirPollutionWebApi/Models/Reading.cs +++ b/AirPollutionWebApi/Models/Reading.cs @@ -1,6 +1,4 @@ -using System; - -namespace AirPollutionWebApi.Models +namespace AirPollutionWebApi.Models { public class Reading { diff --git a/AirPollutionWebApi/Singletons/Singleton.cs b/AirPollutionWebApi/SqlOperator.cs index 00269ad..74ad2e8 100644 --- a/AirPollutionWebApi/Singletons/Singleton.cs +++ b/AirPollutionWebApi/SqlOperator.cs @@ -5,9 +5,8 @@ using AirPollutionWebApi.Models; namespace AirPollutionWebApi.Singletons { - public class Singleton + public static class SqlOperator { - static Singleton instance; const string ConnectionString = "Server=tcp:forschool.database.windows.net,1433;" + "Initial Catalog=schooldb;" + "Persist Security Info=False;" + @@ -18,72 +17,85 @@ namespace AirPollutionWebApi.Singletons "TrustServerCertificate=False;" + "Connection Timeout=30;"; - public List<Reading> Readings = new List<Reading>(); - - Singleton() {} - - public static Singleton Instance + public static List<Reading> GetAllReadings() { - get + var readings = new List<Reading>(); + + using (SqlConnection databaseConnection = new SqlConnection(ConnectionString)) { - if (instance == null) + string command = "SELECT * FROM Readings;"; + databaseConnection.Open(); + SqlCommand selectCommand = new SqlCommand(command, databaseConnection); + var reader = selectCommand.ExecuteReader(); + while (reader.Read()) { - instance = new Singleton(); + readings.Add(new Reading + { + Id = reader.GetInt32(0), + TimeStamp = reader.GetInt32(1), + Co = reader.GetInt32(2), + No = reader.GetInt32(3), + So = reader.GetInt32(4) + }); } - return instance; } + + return readings; } - public void GetData() + public static Reading GetReadingById(int id) { - Readings = new List<Reading>(); + Reading reading = new Reading(); + using (SqlConnection databaseConnection = new SqlConnection(ConnectionString)) { - string command = "SELECT * FROM Readings"; + string command = $"SELECT * FROM Readings WHERE Id={id};"; databaseConnection.Open(); SqlCommand selectCommand = new SqlCommand(command, databaseConnection); var reader = selectCommand.ExecuteReader(); while (reader.Read()) { - Readings.Add(new Reading + reading = new Reading { - Id = reader.GetInt32(0), - TimeStamp = reader.GetInt32(1), - Co = reader.GetInt32(2), + Id = reader.GetInt32(0), + TimeStamp = reader.GetInt32(1), + Co = reader.GetInt32(2), No = reader.GetInt32(3), So = reader.GetInt32(4) - }); + }; } } + + return reading; } - //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 static void PutReading(int id, Reading reading) + { + using (SqlConnection dbCon = new SqlConnection(ConnectionString)) + { + dbCon.Open(); + string query = $"UPDATE Readings SET TimeStamp='{reading.TimeStamp}'," + + $"Co='{reading.Co}', No='{reading.No}', So='{reading.So}' WHERE Id={id};"; + var cmd = new SqlCommand(query, dbCon); + cmd.ExecuteNonQuery(); + dbCon.Close(); + } + } - public void PostData(Reading reading) + public static void PostReading(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});"; + 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) + public static void DeleteReading(int id) { using (SqlConnection dbCon = new SqlConnection(ConnectionString)) { @@ -93,7 +105,6 @@ namespace AirPollutionWebApi.Singletons cmd.ExecuteNonQuery(); dbCon.Close(); } - GetData(); } } }
\ No newline at end of file |