Banking_System_Server  1.0.0
Qt-based banking app for user/admin account management, transactions, secure server communication via PostgreSQL/Supabase.
UpdatePasswordRequest.h
Go to the documentation of this file.
1 
8 #ifndef UPDATEPASSWORDREQUEST_H
9 #define UPDATEPASSWORDREQUEST_H
10 
11 #include "Request.h"
12 #include "db.h"
13 
21 {
22 private:
23  DB::DatabaseManager* dbManager = nullptr;
24 
25 public:
31  UpdatePasswordRequest() : 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_password;
53 
54  QJsonObject response;
55  QJsonObject data;
56 
57  response.insert("Response", 13);
58 
59  // Extract the data array
60  if (jsonObj.contains("Data"))
61  {
62  QJsonObject dataObj = jsonObj["Data"].toObject();
63 
64  if (dataObj.contains("email"))
65  {
66  email = dataObj.value("email").toString();
67  }
68 
69  if (dataObj.contains("password"))
70  {
71  password = dataObj.value("password").toString();
72  }
73 
74  if (dataObj.contains("new_password"))
75  {
76  new_password = dataObj.value("new_password").toString();
77  }
78  }
79  else
80  {
81  qCritical() << "Data not found";
82  }
83 
84  do
85  {
86  if (!isDBConnectionValid(dbManager))
87  {
88  return CreateDBConnectionError(response, data);
89  }
90 
91  // Validate user credentials
92  DB::DbResult result = dbManager->select("*")->table("users")->where("email =", email)->exec();
93  int user_id = result.first().value("id").toInt();
94 
95  if (result.isEmpty())
96  {
97  return CreateErrorResponse(response, data, "email not found");
98  }
99 
100  result = dbManager->select("password")->table("users")->where("id =", user_id)->exec();
101  if (result.first().value("password").toString() != password)
102  {
103  return CreateErrorResponse(response, data, "Invalid password");
104  }
105 
106  // Update the password
107  bool success = dbManager->where("email = ", email)->update("users", {{"password", new_password}});
108 
109  if (!success)
110  {
111  return CreateErrorResponse(response, data, "Failed to update password");
112  }
113 
114  data.insert("status", int(true));
115  data.insert("message", "Password updated successfully");
116 
117  response.insert("Data", data);
118 
119  } while (false);
120 
121  // Convert response to JSON
122  QJsonDocument responseDoc(response);
123  QByteArray responseData = responseDoc.toJson();
124 
125  // Send response
126  qDebug().noquote() << "<-- UpdatePassword::Response :\n" << responseDoc.toJson(QJsonDocument::Indented);
127 
128  return response;
129  }
130 };
131 
132 #endif // UPDATEPASSWORDREQUEST_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 UpdatePasswordRequest class handles password update requests for users.
Definition: UpdatePasswordRequest.h:21
QJsonObject execute(const QJsonObject &jsonObj, QMutex &m) override
Executes the password update request.
Definition: UpdatePasswordRequest.h:46
UpdatePasswordRequest()
Constructor for the UpdatePasswordRequest class.
Definition: UpdatePasswordRequest.h:31
Database management classes for handling database connections and operations.