Banking_System_Server  1.0.0
Qt-based banking app for user/admin account management, transactions, secure server communication via PostgreSQL/Supabase.
GetBalanceRequest.h
Go to the documentation of this file.
1 
8 #ifndef GETBALANCEREQUEST_H
9 #define GETBALANCEREQUEST_H
10 
11 #include "Request.h"
12 #include "db.h"
13 
20 class GetBalanceRequest : public Request
21 {
22 private:
23  DB::DatabaseManager* dbManager = nullptr;
24 
25 public:
31  GetBalanceRequest() : 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  int accountNumber;
52 
53  QJsonObject response;
54  QJsonObject data;
55 
56  response.insert("Response", 3);
57 
58  // Extract the data array
59  if (jsonObj.contains("Data"))
60  {
61  QJsonObject dataObj = jsonObj["Data"].toObject();
62 
63  if (dataObj.contains("account_number"))
64  {
65  accountNumber = dataObj.value("account_number").toInt();
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 =
81  dbManager->select("balance")->table("Accounts")->where("account_number =", accountNumber)->exec();
82 
83  if (result.isEmpty())
84  {
85  return CreateErrorResponse(response, data, "Account not found");
86  }
87 
88  QJsonObject obj = result.first();
89 
90  data.insert("status", int(true));
91  data.insert("message", "Balance fetched successfully");
92  data.insert("balance", obj.value("balance").toDouble());
93 
94  response.insert("Data", data);
95  } while (false);
96 
97  // Convert response to JSON
98  QJsonDocument responseDoc(response);
99  QByteArray responseData = responseDoc.toJson();
100 
101  // Send response
102  qDebug().noquote() << "<-- GetBalance::Response :\n" << responseDoc.toJson(QJsonDocument::Indented);
103 
104  return response;
105  }
106 };
107 
108 #endif // GETBALANCEREQUEST_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
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 GetBalanceRequest class handles the retrieval of account balances.
Definition: GetBalanceRequest.h:21
QJsonObject execute(const QJsonObject &jsonObj, QMutex &m) override
Executes the request to get the account balance.
Definition: GetBalanceRequest.h:47
GetBalanceRequest()
Constructor for the GetBalanceRequest class.
Definition: GetBalanceRequest.h:31
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.