Banking_System_Server  1.0.0
Qt-based banking app for user/admin account management, transactions, secure server communication via PostgreSQL/Supabase.
GetDatabaseRequest.h
Go to the documentation of this file.
1 
8 #ifndef GETDATABASEREQUEST_H
9 #define GETDATABASEREQUEST_H
10 
11 #include "Request.h"
12 #include "db.h"
13 
21 {
22 private:
23  DB::DatabaseManager* dbManager = nullptr;
24 
25 public:
31  GetDatabaseRequest() : dbManager(DB::DatabaseManager::createInstance())
32  {
33  // Log to database log table (if needed)
34  }
35 
47  QJsonObject execute(const QJsonObject& jsonObj, QMutex& m) override
48  {
49  QMutexLocker locker(&m); // Lock the mutex for the duration of this function
50 
51  QJsonObject response;
52  QJsonObject data;
53 
54  QString admin_email;
55 
56  response.insert("Response", 7);
57 
58  // Extract the data array
59  if (jsonObj.contains("Data"))
60  {
61  QJsonObject dataObj = jsonObj["Data"].toObject();
62 
63  if (dataObj.contains("email"))
64  {
65  admin_email = dataObj.value("email").toString();
66  }
67  }
68  else
69  {
70  qCritical() << "Data not found";
71  }
72 
73  do
74  {
75  if (!isDBConnectionValid(dbManager))
76  {
77  return CreateDBConnectionError(response, data);
78  }
79 
80  // Check if the user is an admin
81  DB::DbResult result = dbManager->select("role")->table("Users")->where("email =", admin_email)->exec();
82 
83  if (result.isEmpty())
84  {
85  return CreateErrorResponse(response, data, "you are not registered user!");
86  }
87 
88  QJsonObject obj = result.first();
89 
90  if (obj.value("role").toString() != "admin")
91  {
92  return CreateErrorResponse(response, data, "Unauthorized, Cannot get database. User is not an admin");
93  }
94 
95  // Get all users from the database
96  result = dbManager->select("*")->table("Users")->exec();
97 
98  if (result.isEmpty())
99  {
100  return CreateErrorResponse(response, data, "No data found");
101  }
102 
103  QJsonArray userList;
104 
105  for (int i = 0; i < result.size(); ++i)
106  {
107  QJsonObject userObj;
108  QJsonObject obj = result.data(i);
109 
110  userObj.insert("account_number", QJsonValue::Null);
111  userObj.insert("first_name", obj.value("first_name").toString());
112  userObj.insert("last_name", obj.value("last_name").toString());
113  userObj.insert("email", obj.value("email").toString());
114  userObj.insert("role", obj.value("role").toString());
115  userObj.insert("balance", QJsonValue::Null);
116 
117  userList.append(userObj);
118  }
119 
120  // Get all accounts from the database that are associated with the users
121  result = dbManager->select("A.account_number, A.balance, U.email")
122  ->table("Accounts A")
123  ->join("JOIN Users U ON A.user_id = U.id")
124  ->exec();
125 
126  if (!result.isEmpty())
127  {
128  for (int i = 0; i < result.size(); ++i)
129  {
130  QJsonObject obj = result.data(i);
131 
132  for (int j = 0; j < userList.size(); ++j)
133  {
134  QJsonObject userObj = userList[j].toObject();
135  if (userObj.value("email").toString() == obj.value("email").toString())
136  {
137  userObj.insert("account_number", obj.value("account_number").toInt());
138  userObj.insert("balance", obj.value("balance").toDouble());
139  userList[j] = userObj;
140  break;
141  }
142  }
143  }
144  }
145 
146  data.insert("status", int(true));
147  data.insert("message", "Database fetched successfully");
148  data.insert("users", userList);
149 
150  response.insert("Data", data);
151  } while (false);
152 
153  // Convert response to JSON
154  QJsonDocument responseDoc(response);
155  QByteArray responseData = responseDoc.toJson();
156 
157  // Send response
158  qDebug().noquote() << "<-- GetDatabase::Response :\n" << responseDoc.toJson(QJsonDocument::Indented);
159 
160  return response;
161  }
162 };
163 
164 #endif // GETDATABASEREQUEST_H
This file contains the declaration of the Request class, which is an abstract base class for handling...
Manages database connections and SQL operations.
Definition: db.h:80
DatabaseManager * table(const QString &value)
Sets the table for the query.
Definition: db.cpp:115
DatabaseManager * where(const QString &value, const QVariant &val=QVariant())
Adds a WHERE clause to the query.
Definition: db.cpp:121
DbResult exec()
Executes the built query.
Definition: db.cpp:266
DatabaseManager * select(const QString &value)
Selects columns for the query.
Definition: db.cpp:107
DatabaseManager * join(const QString &value)
Adds a JOIN clause to the query.
Definition: db.cpp:188
The DbResult class represents a result set returned from a database query.
Definition: dbresult.h:24
QJsonObject first() const
Retrieves the first item in the result set.
Definition: dbresult.cpp:25
int size() const
Gets the number of items in the result set.
Definition: dbresult.cpp:30
QJsonObject data(int pos) const
Retrieves data at a specific position in the result set.
Definition: dbresult.cpp:35
bool isEmpty() const
Checks if the result set is empty.
Definition: dbresult.cpp:20
The GetDatabaseRequest class handles the retrieval of the entire database.
Definition: GetDatabaseRequest.h:21
GetDatabaseRequest()
Constructor for the GetDatabaseRequest class.
Definition: GetDatabaseRequest.h:31
QJsonObject execute(const QJsonObject &jsonObj, QMutex &m) override
Executes the request to get the entire database.
Definition: GetDatabaseRequest.h:47
The Request class is an abstract base class for handling different types of requests.
Definition: Request.h:25
QJsonObject CreateErrorResponse(QJsonObject &response, QJsonObject &dataObj, QString message)
Creates a generic error JSON response.
Definition: Request.h:90
QJsonObject CreateDBConnectionError(QJsonObject &response, QJsonObject &dataObj)
Creates a JSON response indicating a database connection error.
Definition: Request.h:65
bool isDBConnectionValid(DB::DatabaseManager *dbManager)
Checks if the database connection is valid.
Definition: Request.h:36
Database management classes for handling database connections and operations.