-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathComplaintBox.cpp
182 lines (151 loc) · 6.2 KB
/
ComplaintBox.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
#include "ComplaintBox.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <bits/stdc++.h>
using namespace std;
ComplaintBox::ComplaintBox() {
sqlite3_open("complaints.db", &db);
createTables();
}
ComplaintBox::~ComplaintBox() {
sqlite3_close(db);
}
void ComplaintBox::createTables() {
string sqlUsers = "CREATE TABLE IF NOT EXISTS users (username TEXT PRIMARY KEY, password TEXT);";
string sqlAdmins = "CREATE TABLE IF NOT EXISTS adminusers (username TEXT PRIMARY KEY, password TEXT);";
string sqlComplaints = "CREATE TABLE IF NOT EXISTS complaints ("
"complaint_id INTEGER PRIMARY KEY AUTOINCREMENT, "
"category TEXT, "
"subCategory TEXT, "
"message TEXT);";
sqlite3_exec(db, sqlUsers.c_str(), 0, 0, &errMsg);
sqlite3_exec(db, sqlAdmins.c_str(), 0, 0, &errMsg);
sqlite3_exec(db, sqlComplaints.c_str(), 0, 0, &errMsg);
}
void ComplaintBox::registerUser(bool isAdmin) {
string uname, pass;
cout << PURPLE << "Enter username: " << RESET;
cin >> uname;
string checkSql = "SELECT * FROM users WHERE username='" + uname + "';";
bool exists = false;
sqlite3_exec(db, checkSql.c_str(), [](void *data, int, char **, char **) -> int {
*(bool *)data = true;
return 0;
}, &exists, &errMsg);
if (exists) {
cout << BOLDRED << "Username already exists. Please choose a different Username.\n" << RESET;
return;
}
cout << PURPLE << "Enter password: " << RESET;
cin >> pass;
string table = isAdmin ? "adminusers" : "users";
string sql = "INSERT INTO " + table + " (username, password) VALUES ('" + uname + "', '" + pass + "');";
if (sqlite3_exec(db, sql.c_str(), 0, 0, &errMsg) != SQLITE_OK) {
cout << RED << "Error: " << errMsg << RESET << endl;
sqlite3_free(errMsg);
} else {
cout << BOLDGREEN << "Registration successful!\n" << RESET;
}
}
bool ComplaintBox::loginUser(bool isAdmin) {
string uname, pass;
cout << CYAN << "Enter username: " << RESET;
cin >> uname;
cout << CYAN << "Enter password: " << RESET;
cin >> pass;
string table = isAdmin ? "adminusers" : "users";
string sql = "SELECT * FROM " + table + " WHERE username = '" + uname + "' AND password = '" + pass + "';";
bool success = false;
sqlite3_exec(db, sql.c_str(), [](void *successPtr, int, char **, char **) -> int {
*(bool*)successPtr = true;
return 0;
}, &success, &errMsg);
if (success) {
cout << GREEN << "Login successful!\n" << RESET;
return true;
} else {
cout << RED << "Invalid credentials!\n" << RESET;
return false;
}
}
void ComplaintBox::fileComplaint() {
string category, subCategory, message;
cout << YELLOW << "Enter category: " << RESET;
cin.ignore();
getline(cin, category);
cout << YELLOW << "Enter sub-category: " << RESET;
getline(cin, subCategory);
cout << YELLOW << "Enter complaint message: " << RESET;
getline(cin, message);
string sql = "INSERT INTO complaints (category, subCategory, message, status) VALUES ('"
+ category + "', '" + subCategory + "', '" + message + "', 'Pending');";
if (sqlite3_exec(db, sql.c_str(), 0, 0, &errMsg) != SQLITE_OK) {
cout << RED << "Error: " << errMsg << RESET << endl;
sqlite3_free(errMsg);
} else {
cout << BOLDGREEN << "Complaint filed successfully with status 'Pending'!\n" << RESET;
}
}
void ComplaintBox::exportComplaintsToCSV() {
ofstream file("complaints_export.csv");
if (!file.is_open()) {
cout << RED << "Failed to create CSV file.\n" << RESET;
return;
}
file << "complaint_id,category,subCategory,message,status\n";
string sql = "SELECT complaint_id, category, subCategory, message, status FROM complaints;";
auto callback = [](void *data, int argc, char **argv, char **colName) -> int {
ofstream *f = static_cast<ofstream *>(data);
for (int i = 0; i < argc; i++) {
*f << (argv[i] ? argv[i] : "") << (i < argc - 1 ? "," : "\n");
}
return 0;
};
if (sqlite3_exec(db, sql.c_str(), callback, &file, &errMsg) != SQLITE_OK) {
cout << RED << "Export failed: " << errMsg << RESET << endl;
sqlite3_free(errMsg);
} else {
cout << BOLDGREEN << "Complaints exported to 'complaints_export.csv'!\n" << RESET;
}
file.close();
}
void ComplaintBox::searchComplaints() {
string keyword;
cout << YELLOW << "Enter keyword to search for: " << RESET;
cin.ignore();
getline(cin, keyword);
string sql = "SELECT complaint_id, category, subCategory, message, status FROM complaints "
"WHERE category LIKE '%" + keyword + "%' OR "
"subCategory LIKE '%" + keyword + "%' OR "
"message LIKE '%" + keyword + "%' OR "
"status LIKE '%" + keyword + "%';";
cout << CYAN << "\nSearch Results:\n" << RESET;
auto callback = [](void *data, int argc, char **argv, char **colName) -> int {
for (int i = 0; i < argc; i++) {
cout << (colName[i] ? colName[i] : "") << ": " << (argv[i] ? argv[i] : "NULL") << " ";
}
cout << "\n";
return 0;
};
if (sqlite3_exec(db, sql.c_str(), callback, 0, &errMsg) != SQLITE_OK) {
cout << RED << "Search failed: " << errMsg << RESET << endl;
sqlite3_free(errMsg);
}
}
void ComplaintBox::updateComplaintStatus(int complaint_id, const std::string& new_status) {
sqlite3_stmt* stmt;
std::string sql = "UPDATE complaints SET status = ? WHERE complaint_id = ?";
if (sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr) == SQLITE_OK) {
sqlite3_bind_text(stmt, 1, new_status.c_str(), -1, SQLITE_STATIC);
sqlite3_bind_int(stmt, 2, complaint_id);
if (sqlite3_step(stmt) == SQLITE_DONE) {
cout << GREEN << "Status updated successfully.\n" << RESET;
} else {
cerr << RED << "Failed to update status.\n" << RESET;
}
} else {
cerr << RED << "SQL Prepare Failed: " << sqlite3_errmsg(db) << "\n" << RESET;
}
sqlite3_finalize(stmt);
}