forked from 3rd-year-CSE-20/SIS_GUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
staffmember.cpp
207 lines (177 loc) · 6.94 KB
/
staffmember.cpp
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "staffmember.h"
#include <QString>
#include "course.h"
#include <QSqlDatabase>
#include <QSql>
#include <QSqlError>
#include <QDir>
#include <QFile>
#include <QDebug>
#include <QSqlQuery>
#include <QString>
#include <QStringList>
#include <QSqlError>
#include <globalDbObject.h>
static QString staff_table = "staff_members";
static QStringList staff_columns = {"first_name","last_name", "degree", "birth_date",
"gendre", "address", "password", "picture", "degree", "department"};
static QStringList staff_types = {"INTEGER PRIMARY KEY AUTOINCREMENT", "TEXT", "TEXT", "TEXT", "TEXT",
"TEXT", "TEXT", "TEXT", "TEXT", "TEXT", "TEXT"};
StaffMember::StaffMember() : Person() {
this->degree = QString("");
this->department = QString("");
this->courses = QVector<Course>();
}
StaffMember::StaffMember(QString first_name, QString last_name, QString gendre, QString picture, QString birth_date, QString address, QString college_id, QString password, QString degree, QString department)
: Person(first_name, last_name, gendre, picture, birth_date, address, college_id, password) {
this->degree = degree;
this->department = department;
this->courses = QVector<Course>();
}
void StaffMember::setDegree(QString degree) {
this->degree = degree;
}
QString StaffMember::getDegree() {
return this->degree;
}
void StaffMember::setDepartment(QString department) {
this->department = department;
}
QString StaffMember::getDepartment() {
return this->department;
}
QVector<Course> StaffMember::getCourses() {
return this->courses;
}
void StaffMember::addCourse(QString course_name) {
int index = -1;
for(int i = 0; i < this->courses.size(); i++) {
if(this->courses[0].getName() == course_name) {
index = i;
break;
}
}
if(index == -1)
this->courses.push_back(Course(course_name));
}
void StaffMember::deleteCourse(QString course_name) {
int index = -1;
for(int i = 0; i < this->courses.size(); i++) {
if(this->courses[0].getName() == course_name) {
index = i;
break;
}
}
if(index != -1)
this->courses.remove(index);
}
QVector<StaffMember> StaffMember::all() {
QSqlQuery query = SQLiteDb.sql_getQuery();
QVector<StaffMember> staff_members;
StaffMember temp;
query.exec("SELECT * FROM staff_members");
QSqlQuery query1;
while(query.next()) {
long long id = query.value(0).toLongLong();
QString first_name = query.value(1).toString();
QString last_name = query.value(2).toString();
QString gendre = query.value(3).toString();
QString picture = query.value(4).toString();
QString birth_date = query.value(5).toString();
QString address = query.value(6).toString();
QString college_id = query.value(7).toString();
QString password = query.value(8).toString();
QString degree = query.value(9).toString();
QString department = query.value(10).toString();
StaffMember temp(first_name, last_name, gendre, picture, birth_date, address, college_id, password, degree, department);
temp.setId(id);
temp.setIsSaved(true);
query1.exec("SELECT * FROM courses_staff_members WHERE course_id = " + QString::number(id));
while(query1.next()) {
temp.addCourse(Course::find(query1.value(1).toLongLong()).getName());
}
staff_members.push_back(temp);
}
return staff_members;
}
bool StaffMember::isInDatabase(long long id) {
QSqlQuery query = SQLiteDb.sql_getQuery();
query.exec("SELECT * FROM staff_members WHERE id = " + QString::number(id));
if(query.next()) {
return true;
}
return false;
}
StaffMember StaffMember::find(long long id) {
QSqlQuery query = SQLiteDb.sql_getQuery();
query.exec("SELECT * FROM staff_members WHERE id = " + QString::number(id));
QSqlQuery query1;
query.next();
QString first_name = query.value(1).toString();
QString last_name = query.value(2).toString();
QString gendre = query.value(3).toString();
QString picture = query.value(4).toString();
QString birth_date = query.value(5).toString();
QString address = query.value(6).toString();
QString college_id = query.value(7).toString();
QString password = query.value(8).toString();
QString degree = query.value(9).toString();
QString department = query.value(10).toString();
StaffMember staff_member(first_name, last_name, gendre, picture, birth_date, address, college_id, password, degree, department);
staff_member.setId(id);
staff_member.setIsSaved(true);
query1.exec("SELECT course_id FROM courses_staff_members WHERE staff_member_id = " + college_id);
while(query1.next()) {
staff_member.addCourse(query1.value(0).toString());
}
return staff_member;
}
bool StaffMember::save(){
QString id_ = QString::number(getId());
SQLiteDb.sql_select("*", staff_table, " id = " + id_);
QSqlQuery query = SQLiteDb.sql_getQuery();
QStringList values = {getFirstName(), getLastName(), getDegree(), getBirthDate(),
getGendre(), getAddress(), getPassword(), getPicture(), getDegree(), getDepartment()};
if(query.next()){
SQLiteDb.sql_update(staff_table, staff_columns, values, "id = " + id_);
for(int i = 0; i < courses.size(); i++){
QString course_name = courses[i].getName();
// SQLiteDb.sql_select("*", "courses_staff_members", " staff_member_id = " + id_ + " AND course_id " + course_id);
// query = SQLiteDb.sql_getQuery();
// if(!query.next()){
SQLiteDb.sql_insert("courses_staff_members", {"staff_member_id", "course_id"}, {getCollegeId(), course_name});
// }
}
return true;
}
SQLiteDb.sql_insert(staff_table, staff_columns, values);
for(int i = 0; i < courses.size(); i++){
QString course_name = courses[i].getName();
SQLiteDb.sql_insert("courses_staff_members", {"staff_member_id", "course_id"}, {getCollegeId(), course_name});
}
return false;
}
QVector<StaffMember> StaffMember::where(QString column, QString value){
SQLiteDb.sql_select("*", staff_table, column + " LIKE '" + value + "%'");
QSqlQuery query = SQLiteDb.sql_getQuery();
QVector<StaffMember> staff_members;
while (query.next()) {
int id = query.value(0).toInt();
staff_members.push_back(find(id));
}
return staff_members;
}
void StaffMember::delete1(){
QString staff_id = QString::number(getId());
SQLiteDb.sql_delete(staff_table, "id = " + staff_id);
SQLiteDb.sql_delete("courses_staff_members", "staff_member_id = " + staff_id);
}
int StaffMember::getLastId(){
QSqlQuery query = SQLiteDb.sql_getQuery();
query.exec("SELECT id FROM staff_members ORDER BY id DESC LIMIT 0, 1;");
if(query.next()){
return query.value(0).toInt();
}else{
return 0;
}
}