aboutsummaryrefslogtreecommitdiff
blob: 00269ad11e2be639e5ea143cf40c47fc230ce7a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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() {}

		public static Singleton Instance
		{
			get
			{
				if (instance == null)
				{
					instance = new Singleton();
				}
				return instance;
			}
		}

		public 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
					{
                        Id = reader.GetInt32(0),
                        TimeStamp = reader.GetInt32(1),
                        Co = reader.GetInt32(2),
						No = reader.GetInt32(3),
						So = reader.GetInt32(4)
					});
				}
			}
		}

		//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();
		}
	}
}