-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorker.cs
149 lines (143 loc) · 6.9 KB
/
Worker.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TermProject {
class Worker : Persistable {
public int ID { get; set; }
public string BannerID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string PhoneNumber { get; set; }
public string Email { get; set; }
public string Credential { get; set; }
public string WorkerPassword { get; set; }
public string InitialRegistrationDate { get; private set; }
public string Notes { get; set; }
public string Status { get; set; }
public string DateStatusUpdated { get; set; }
public Worker() : base() { } // call parent default constructor
//------------------------------------------------------------------
public void Populate(int ID) {
string queryString = "SELECT * FROM Worker WHERE ID = " + ID;
List<Object> results = getValues(queryString);
if (results != null) {
foreach (object result in results) {
IEnumerable<Object> row = result as IEnumerable<Object>;
int count = 0;
foreach (object rowValue in row) {
// DEBUG Console.WriteLine(rowValue);
if (count == 0)
this.ID = Convert.ToInt32(rowValue);
else if (count == 1)
this.BannerID = Convert.ToString(rowValue);
else if (count == 2)
FirstName = Convert.ToString(rowValue);
else if (count == 3)
LastName = Convert.ToString(rowValue);
else if (count == 4)
PhoneNumber = Convert.ToString(rowValue);
else if (count == 5)
Email = Convert.ToString(rowValue);
else if (count == 6)
Credential = Convert.ToString(rowValue);
else if (count == 7)
InitialRegistrationDate = Convert.ToString(rowValue);
else if (count == 8)
WorkerPassword = Convert.ToString(rowValue);
else if (count == 9)
Notes = Convert.ToString(rowValue);
else if (count == 10)
Status = Convert.ToString(rowValue);
else if (count == 11)
DateStatusUpdated = Convert.ToString(rowValue);
count = count + 1;
}
}
}
}
//------------------------------------------------------------------
public void Insert() {
this.InitialRegistrationDate = DateTime.Now.ToString("yyyy-MM-dd");
string insertQuery =
"INSERT INTO Worker (BannerId, FirstName, LastName, PhoneNumber, EmailAddress, Credential, InitialRegistrationDate, WorkerPassword, Notes, Status, DateStatusUpdated) " +
"VALUES (" +
"'" + this.BannerID + "', '" +
this.FirstName + "', '" +
this.LastName + "', '" +
this.PhoneNumber + "', '" +
this.Email + "', '" +
this.Credential + "', '" +
this.InitialRegistrationDate + "', '" +
this.WorkerPassword + "', '" +
this.Notes + "', '" +
this.Status + "', '" +
this.DateStatusUpdated + "')";
int returnCode = ModifyDatabase(insertQuery);
if (returnCode != 0) {
Console.WriteLine("Error in inserting Worker object into database");
} else {
Console.WriteLine("Worker object successfully inserted");
string idQueryString = "SELECT MAX(ID) FROM Worker";
List<Object> results = getValues(idQueryString);
if (results != null) {
// DEBUG Console.WriteLine("Got an id from id query");
foreach (object result in results) {
IEnumerable<Object> row = result as IEnumerable<Object>;
foreach (object rowValue in row) {
// DEBUG Console.WriteLine("Retrieved id = " + rowValue);
this.ID = Convert.ToInt32(rowValue);
}
}
}
}
}
//------------------------------------------------------------------
public void Update() {
string updateQuery = "UPDATE Worker SET " +
" BannerId = '" + this.BannerID + "' ," +
" FirstName = '" + this.FirstName + "' ," +
" LastName = '" + this.LastName + "' ," +
" PhoneNumber = '" + this.PhoneNumber + "' ," +
" EmailAddress = '" + this.Email + "' ," +
" Credential = '" + this.Credential + "', " +
" WorkerPassword = '" + this.WorkerPassword + "', " +
" Notes = '" + this.Notes + "', " +
" Status = '" + this.Status + "', " +
" DateStatusUpdated = '" + this.DateStatusUpdated + "' " +
" WHERE " +
" ID = " + this.ID;
int returnCode = ModifyDatabase(updateQuery);
if (returnCode != 0)
Console.WriteLine("Error in updating Worker object into database");
else
Console.WriteLine("Worker object successfully updated");
}
//------------------------------------------------------------------
public void Delete() {
string deleteQuery = "DELETE FROM Worker WHERE " +
" ID = " + this.ID;
Console.WriteLine(deleteQuery);
int returnCode = ModifyDatabase(deleteQuery);
if (returnCode != 0)
Console.WriteLine("Error in deleting Worker object from database");
else
Console.WriteLine("Worker object successfully deleted");
}
public override string ToString() {
return "\nID:\t\t\t" + this.ID +
"\nBanner Id:\t\t" + this.BannerID +
"\nFirst name:\t\t" + this.FirstName +
"\nLast Name:\t\t" + this.LastName +
"\nPhone number:\t\t" + this.PhoneNumber +
"\nEmail address:\t\t" + this.Email +
"\nCredential:\t\t" + this.Credential +
"\nRegistration date:\t" + this.InitialRegistrationDate +
"\nPassword:\t\t" + this.WorkerPassword +
"\nNotes:\t\t\t" + this.Notes +
"\nStatus:\t\t\t" + this.Status +
"\nDate status updated:\t" + this.DateStatusUpdated;
}
}
}