Banking_System_Server  1.0.0
Qt-based banking app for user/admin account management, transactions, secure server communication via PostgreSQL/Supabase.
UpdateEmailRequest.h
Go to the documentation of this file.
1 
8 #ifndef UPDATEEMAILREQUEST_H
9 #define UPDATEEMAILREQUEST_H
10 
11 #include "Request.h"
12 #include "db.h"
13 
21 {
22 private:
23  DB::DatabaseManager* dbManager = nullptr;
24 
25 public:
31  UpdateEmailRequest() : dbManager(DB::DatabaseManager::createInstance())
32  {
33  // Log to database log table (if needed)
34  }
35 
46  QJsonObject execute(const QJsonObject& jsonObj, QMutex& m) override
47  {
48  QMutexLocker locker(&m); // Lock the mutex for the duration of this function
49 
50  QString email;
51  QString password;
52  QString new_email;
53 
54  QJsonObject response;
55 
56  QJsonObject data;
57 
58  response.insert("Response", 12);
59 
60  // Extract the data array
61  if (jsonObj.contains("Data"))
62  {
63  QJsonObject dataObj = jsonObj["Data"].toObject();
64 
65  if (dataObj.contains("email"))
66  {
67  email = dataObj.value("email").toString();
68  }
69 
70  if (dataObj.contains("password"))
71  {
72  password = dataObj.value("password").toString();
73  }
74 
75  if (dataObj.contains("new_email"))
76  {
77  new_email = dataObj.value("new_email").toString();
78  }
79  }
80  else
81  {
82  qCritical() << "Data not found";
83  }
84 
85  do
86  {
87  if (!isDBConnectionValid(dbManager))
88  {
89  return CreateDBConnectionError(response, data);
90  }
91 
92  // Validate user credentials
93  DB::DbResult result = dbManager->select("*")->table("users")->where("email =", email)->exec();
94  int user_id = result.first().value("id").toInt();
95 
96  if (result.isEmpty())
97  {
98  return CreateErrorResponse(response, data, "email not found");
99  }
100 
101  // change the password of that specific user
102  result = dbManager->select("password")->table("users")->where("id =", user_id)->exec();
103  if (result.first().value("password").toString() != password)
104  {
105  return CreateErrorResponse(response, data, "Invalid password");
106  }
107 
108  // Check if the new email is already associated with another account
109  result = dbManager->select("*")->table("users")->where("email =", new_email)->exec();
110 
111  if (!result.isEmpty())
112  {
113  return CreateErrorResponse(response, data, "Email already exists");
114  }
115 
116  // Update the email
117  bool success = dbManager->where("email = ", email)->update("users", {{"email", new_email}});
118 
119  if (!success)
120  {
121  return CreateErrorResponse(response, data, "Failed to update email");
122  }
123 
124  data.insert("status", int(true));
125  data.insert("message", "Email updated successfully");
126 
127  response.insert("Data", data);
128 
129  } while (false);
130 
131  // Convert response to JSON
132  QJsonDocument responseDoc(response);
133  QByteArray responseData = responseDoc.toJson();
134 
135  // Send response
136  qDebug().noquote() << "<-- UpdateEmail::Response :\n" << responseDoc.toJson(QJsonDocument::Indented);
137 
138  return response;
139  }
140 };
141 
142 #endif // UPDATEEMAILREQUEST_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
bool update(const QString &table, const QVariantMap &data)
Updates data in a specified table.
Definition: db.cpp:361
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
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
bool isEmpty() const
Checks if the result set is empty.
Definition: dbresult.cpp:20
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
The UpdateEmailRequest class handles email update requests for users.
Definition: UpdateEmailRequest.h:21
QJsonObject execute(const QJsonObject &jsonObj, QMutex &m) override
Executes the email update request.
Definition: UpdateEmailRequest.h:46
UpdateEmailRequest()
Constructor for the UpdateEmailRequest class.
Definition: UpdateEmailRequest.h:31
Database management classes for handling database connections and operations.