8 #ifndef UPDATEUSERREQUEST_H
9 #define UPDATEUSERREQUEST_H
48 QJsonObject
execute(
const QJsonObject& jsonObj, QMutex& m)
override
50 QMutexLocker locker(&m);
54 QString new_first_name;
55 QString new_last_name;
62 response.insert(
"Response", 10);
65 if (jsonObj.contains(
"Data"))
67 QJsonObject dataObj = jsonObj[
"Data"].toObject();
69 if (dataObj.contains(
"email"))
71 email = dataObj.value(
"email").toString();
73 if (dataObj.contains(
"account_number"))
75 account_number = dataObj.value(
"account_number").toInt();
77 if (dataObj.contains(
"newData"))
79 QJsonObject newDataObj = dataObj[
"newData"].toObject();
81 if (newDataObj.contains(
"first_name"))
83 new_first_name = newDataObj.value(
"first_name").toString();
85 if (newDataObj.contains(
"last_name"))
87 new_last_name = newDataObj.value(
"last_name").toString();
89 if (newDataObj.contains(
"email"))
91 new_email = newDataObj.value(
"email").toString();
93 if (newDataObj.contains(
"role"))
95 new_role = newDataObj.value(
"role").toString();
101 qCritical() <<
"Data not found";
113 QJsonObject obj = result.
first();
114 QString sneder_role = obj.value(
"role").toString();
121 if (sneder_role !=
"admin")
127 result = dbManager->
select(
"*")->
table(
"accounts")->
where(
"account_number =", account_number)->
exec();
129 int user_id = result.
first().value(
"user_id").toInt();
139 int new_user_id = result.
first().value(
"id").toInt();
141 qDebug() <<
"user_id: " << user_id;
142 qDebug() <<
"new_user_id: " << new_user_id;
144 if (!result.
isEmpty() && result.
first().value(
"id").toInt() != user_id)
149 qDebug() <<
"Debugging update user";
150 qDebug() <<
"user_id: " << user_id;
151 qDebug() <<
"new_email: " << new_email;
152 qDebug() <<
"new_role: " << new_role;
153 qDebug() <<
"new_first_name: " << new_first_name;
154 qDebug() <<
"new_last_name: " << new_last_name;
158 QString current_role = result.
first().value(
"role").toString();
160 if (current_role ==
"user" && new_role ==
"admin")
163 bool success = dbManager->
where(
"account_number = ", account_number)->
del(
"accounts");
172 bool success = dbManager->
where(
"id = ", user_id)
173 ->
update(
"users", {{
"first_name", new_first_name},
174 {
"last_name", new_last_name},
175 {
"email", new_email},
176 {
"role", new_role}});
183 data.insert(
"status",
int(
true));
184 data.insert(
"message",
"User updated successfully");
186 response.insert(
"Data", data);
191 QJsonDocument responseDoc(response);
192 QByteArray responseData = responseDoc.toJson();
195 qDebug().noquote() <<
"<-- UpdateUser::Response :\n" << QJsonDocument(response).toJson(QJsonDocument::Indented);
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
bool del(const QString &table)
Deletes data from a specified table.
Definition: db.cpp:398
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 UpdateUserRequest class handles user update requests.
Definition: UpdateUserRequest.h:22
UpdateUserRequest()
Constructor for the UpdateUserRequest class.
Definition: UpdateUserRequest.h:32
QJsonObject execute(const QJsonObject &jsonObj, QMutex &m) override
Executes the user update request.
Definition: UpdateUserRequest.h:48
Database management classes for handling database connections and operations.