-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStaff.cpp
132 lines (112 loc) · 3.79 KB
/
Staff.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
#include <iostream>
#include <pqxx/pqxx>
#include <windows.h> // used for the Sleep() function
#include "Staff.h"
#include "extern.h"
using namespace std;
MenuManager menuManager3;
Staff::Staff(string email, string firstName) {
this->email = email;
this->firstName = firstName;
}
void Staff::staffSignOut() {
delete this;
admin = nullptr;
menuManager3.showWelcomeMenu();
}
void Staff::promoteMemberToStaffProcess() {
string memberEmail, memberFirstName, memberLastName;
bool memberExists = false;
bool promotionConfirmed = false;
while (!promotionConfirmed) {
memberEmail = identifyMemberToPromote();
if ((memberEmail == "C") || (memberEmail == "c")) { // if the user wants to cancel the operation
system("cls");
break;
}
else {
memberExists = checkMemberExists(memberEmail, memberFirstName, memberLastName);
if (memberExists)
promotionConfirmed = confirmPromotion(memberFirstName, memberLastName, memberExists);
}
}
if (promotionConfirmed)
promoteMemberToStaff(memberEmail);
}
string Staff::identifyMemberToPromote() {
string userEmail;
cout << "--------------------- Promote To Library Staff ---------------------\n\n";
cout << "Enter the email address of the user to be promoted (or enter 'C' to quit and return to the main menu)\n";
cout << "Enter here: "; getline(cin, userEmail);
cout << endl;
return userEmail;
}
bool Staff::checkMemberExists(string memberEmail, string& memberFirstName, string& memberLastName) {
try {
conn->prepare(
"check_member_exists",
"SELECT email, first_name, last_name FROM users WHERE email = $1"
);
}
catch (const pqxx::sql_error& e) {
// error handling is neccessary because prepared statement might still exist within current session
if (string(e.what()).find("Failure during \'[PREPARE check_member_exists]\': ERROR: prepared statement \"check_member_exists\" already exists"))
throw;
}
pqxx::work checkMemberProcess(*conn);
pqxx::result userResult = checkMemberProcess.exec_prepared("check_member_exists", memberEmail);
checkMemberProcess.commit();
if (userResult.size() == 0) {// no user matching with inputted email was found
system("cls");
cout << "Invalid input/email address could not be found, please try again...\n\n";
return false;
}
for (auto row : userResult) {
memberFirstName = row["first_name"].c_str();
memberLastName = row["last_name"].c_str();
}
return true;
}
bool Staff::confirmPromotion(string memberFirstName, string memberLastName, bool& memberExists) {
bool inputIsInvalid = false;
char decision;
system("cls");
while (!inputIsInvalid) {
cout << "--------------------- Promote To Library Staff ---------------------\n\n";
cout << "Are you sure you would like to promote " << memberFirstName << " " << memberLastName << " to a library staff member?\n";
cout << "Enter here (Y/N): "; cin >> decision;
cout << endl;
cin.clear(); cin.ignore(1);
if (tolower(decision) == 'y')
return true;
else if (tolower(decision) == 'n') {
system("cls");
memberExists = false;
return false;
}
else {
system("cls");
cout << "Invalid input! Please try again!\n\n";
}
}
}
void Staff::promoteMemberToStaff(string memberEmail) {
try {
conn->prepare(
"promote_member",
"UPDATE users SET is_admin = TRUE WHERE email = $1"
);
}
catch (const pqxx::sql_error& e) {
// error handling is neccessary because prepared statement might still exist within current session
if (string(e.what()).find("Failure during \'[PREPARE promote_member]\': ERROR: prepared statement \"promote_member\" already exists"))
throw;
}
pqxx::work promoteMemberProcess(*conn);
promoteMemberProcess.exec_prepared("promote_member", memberEmail);
promoteMemberProcess.commit();
cout << "Promotion was successful!\n";
cout << "Redirecting you back to the main menu...";
Sleep(3000);
system("cls");
}