-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataBaseHandler.cs
151 lines (133 loc) · 4.64 KB
/
DataBaseHandler.cs
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Collections.Generic;
using Npgsql;
namespace MonitorPhotoApp
{
class DataBaseHandler
{
private const string server = "Server = 62.116.255.129; ";
private const string port = "Port = 5432; ";
private const string dbName = "Database = monitor_photo_app; ";
private const string dbUser = "User Id = monitor; ";
private const string dbUsrPwd = "Password = erp;";
public delegate void StatusUpdateHandler(object sender, ProgressEventArgs e);
public event StatusUpdateHandler OnDatabasIsReady;
private NpgsqlConnection conn;
private NpgsqlDataReader dr;
public List<PhotoInfo> PhotosInfoList { get; private set; }
public DataBaseHandler()
{
this.PhotosInfoList = new List<PhotoInfo>();
try
{
Log.AddToLog("Creating DB connection");
conn = new NpgsqlConnection(server + port + dbName + dbUser + dbUsrPwd);
}
catch (Exception)
{
Log.AddToLog("PGSQL connection failed");
throw;
}
}
private void DBDataIsReady(PhotoAttribute attr)
{
// Make sure someone is listening to event
if (OnDatabasIsReady == null) return;
ProgressEventArgs args = new ProgressEventArgs(attr);
// Broadcast to listeners
OnDatabasIsReady(this, args);
}
public void ExtractDataFromDB(PhotoAttribute attr)
{
string query = "SELECT photo_id ,photo_url FROM photos";
switch (attr)
{
case PhotoAttribute.all:
query = "SELECT photo_id, photo_url, isFunny FROM photos";
break;
case PhotoAttribute.funny:
query = "SELECT photo_id, photo_url, isFunny FROM photos WHERE isfunny = true";
break;
case PhotoAttribute.notFunny:
query = "SELECT photo_id, photo_url, isFunny FROM photos WHERE isfunny = false";
break;
default:
break;
}
Log.AddToLog("Reading Database. Query " + query);
try
{
conn.Open();
NpgsqlCommand command = new NpgsqlCommand(query, conn);
this.PhotosInfoList.Clear();
// Get the reader
dr = command.ExecuteReader();
while (dr.Read())
{
this.PhotosInfoList.Add(new PhotoInfo(int.Parse(dr[0].ToString()), dr[1].ToString(), bool.Parse(dr[2].ToString())));
}
dr.Close();
}
catch (Exception)
{
Log.AddToLog("Error when connecting to database.");
//dont throw;
}
finally
{
// Close connection
if (conn != null)
conn.Close();
}
// Notify that the data from DB is ready to be processed
DBDataIsReady(attr);
}
public void AlterDB(int photoId, bool isFunny)
{
// INSERT query to database to set value of isFunny (bool)
string query = "UPDATE photos SET isFunny = " + isFunny.ToString().ToLower() + " WHERE photo_id = " + photoId.ToString();
try
{
Log.AddToLog($"Altering databse. Query: {query}");
conn.Open();
NpgsqlCommand command = new NpgsqlCommand(query, conn);
dr = command.ExecuteReader();
dr.Close();
}
catch (Exception)
{
// Just throw here for now..
throw;
}
finally
{
if (conn != null)
conn.Close();
}
}
}
public class ProgressEventArgs : EventArgs
{
public PhotoAttribute Attr { get; private set; }
public ProgressEventArgs(PhotoAttribute attr)
{
Attr = attr;
}
}
public class PhotoInfo
{
// Class to store downloaded photo properties
public string URL { get; set; }
public int ID { get; set; }
public bool IsFunny { get; set; }
public PhotoInfo(int ID, string URL, bool IsFunny) {
this.URL = URL;
this.ID = ID;
this.IsFunny = IsFunny;
}
}
}
public enum PhotoAttribute
{
all, funny, notFunny
}