Banking_System_Server  1.0.0
Qt-based banking app for user/admin account management, transactions, secure server communication via PostgreSQL/Supabase.
GetAccountNumberRequest.h
Go to the documentation of this file.
1 
8 #ifndef GETACCOUNTNUMBERREQUEST_H
9 #define GETACCOUNTNUMBERREQUEST_H
10 
11 #include "Request.h"
12 #include "db.h"
13 
21 {
22 private:
23  DB::DatabaseManager* dbManager = nullptr;
24 
25 public:
31  GetAccountNumberRequest() : 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  QString email;
52 
53  QJsonObject response;
54  QJsonObject data;
55 
56  response.insert("Response", 2);
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  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  DB::DbResult result = dbManager->select("*")->table("users")->where("email =", email)->exec();
81 
82  if (result.isEmpty())
83  {
84  return CreateErrorResponse(response, data, "email not found");
85  }
86 
87  result = dbManager->select("A.account_number")
88  ->table("Users U")
89  ->join("JOIN Accounts A ON U.id = A.user_id")
90  ->where("U.email =", email)
91  ->exec();
92 
93  if (result.isEmpty())
94  {
95  return CreateErrorResponse(response, data, "No account found");
96  }
97 
98  QJsonObject obj = result.first();
99 
100  data.insert("status", int(true));
101  data.insert("message", "Account number found");
102  data.insert("account_number", obj.value("account_number").toInt());
103 
104  response.insert("Data", data);
105  } while (false);
106 
107  // Convert response to JSON
108  QJsonDocument responseDoc(response);
109  QByteArray responseData = responseDoc.toJson();
110 
111  // Send response
112  qDebug().noquote() << "<-- GetAccountNumber::Response :\n" << responseDoc.toJson(QJsonDocument::Indented);
113 
114  return response;
115  }
116 };
117 
118 #endif // GETACCOUNTNUMBERREQUEST_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
bool isEmpty() const
Checks if the result set is empty.
Definition: dbresult.cpp:20
The GetAccountNumberRequest class handles the retrieval of account numbers.
Definition: GetAccountNumberRequest.h:21
GetAccountNumberRequest()
Constructor for the GetAccountNumberRequest class.
Definition: GetAccountNumberRequest.h:31
QJsonObject execute(const QJsonObject &jsonObj, QMutex &m) override
Executes the request to get the account number.
Definition: GetAccountNumberRequest.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.