From c8f0195ccc06575349c0e675c23276ac089bf9c7 Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Fri, 1 Dec 2017 14:48:19 +0100 Subject: Changed name of the project. --- PollutometerWebApi/SqlOperator.cs | 84 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 PollutometerWebApi/SqlOperator.cs (limited to 'PollutometerWebApi/SqlOperator.cs') diff --git a/PollutometerWebApi/SqlOperator.cs b/PollutometerWebApi/SqlOperator.cs new file mode 100644 index 0000000..d85aa3d --- /dev/null +++ b/PollutometerWebApi/SqlOperator.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Data.SqlClient; +using PollutometerWebApi.Models; + +namespace PollutometerWebApi.Singletons +{ + public static class SqlOperator + { + 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 static List GetReadings(string command) + { + var readings = new List(); + + using (SqlConnection databaseConnection = new SqlConnection(ConnectionString)) + { + databaseConnection.Open(); + SqlCommand selectCommand = new SqlCommand(command, databaseConnection); + var reader = selectCommand.ExecuteReader(); + while (reader.Read()) + { + readings.Add(new Reading + { + Id = reader.GetInt32(0), + TimeStamp = reader.GetInt32(1), + Co = reader.GetDouble(2), + No = reader.GetDouble(3), + So = reader.GetDouble(4) + }); + } + } + + return readings; + } + + 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 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});"; + var cmd = new SqlCommand(query, dbCon); + cmd.ExecuteNonQuery(); + dbCon.Close(); + } + } + + public static void DeleteReading(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(); + } + } + } +} \ No newline at end of file -- cgit v1.2.3 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/SqlOperator.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'PollutometerWebApi/SqlOperator.cs') 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 { -- cgit v1.2.3