File indexing completed on 2024-12-22 04:45:24

0001 /*
0002    SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "roledeletejob.h"
0008 #include "restapimethod.h"
0009 #include "rocketchatqtrestapi_debug.h"
0010 
0011 #include <QJsonDocument>
0012 #include <QJsonObject>
0013 #include <QNetworkReply>
0014 using namespace RocketChatRestApi;
0015 RoleDeleteJob::RoleDeleteJob(QObject *parent)
0016     : RestApiAbstractJob(parent)
0017 {
0018 }
0019 
0020 RoleDeleteJob::~RoleDeleteJob() = default;
0021 
0022 bool RoleDeleteJob::start()
0023 {
0024     if (!canStart()) {
0025         deleteLater();
0026         return false;
0027     }
0028     addStartRestApiInfo("RoleDeleteJob::start");
0029     submitPostRequest(json());
0030     return true;
0031 }
0032 
0033 void RoleDeleteJob::onPostRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson)
0034 {
0035     const QJsonObject replyObject = replyJson.object();
0036     if (replyObject[QLatin1String("success")].toBool()) {
0037         addLoggerInfo(QByteArrayLiteral("RoleDeleteJob: success: ") + replyJson.toJson(QJsonDocument::Indented));
0038         Q_EMIT deleteRoleDone();
0039     } else {
0040         emitFailedMessage(replyErrorString, replyObject);
0041         addLoggerWarning(QByteArrayLiteral("RoleDeleteJob: Problem: ") + replyJson.toJson(QJsonDocument::Indented));
0042     }
0043 }
0044 
0045 const QString &RoleDeleteJob::roleId() const
0046 {
0047     return mRoleId;
0048 }
0049 
0050 void RoleDeleteJob::setRoleId(const QString &newRoleId)
0051 {
0052     mRoleId = newRoleId;
0053 }
0054 
0055 bool RoleDeleteJob::requireHttpAuthentication() const
0056 {
0057     return true;
0058 }
0059 
0060 bool RoleDeleteJob::canStart() const
0061 {
0062     if (!RestApiAbstractJob::canStart()) {
0063         return false;
0064     }
0065     if (mRoleId.isEmpty()) {
0066         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "RoleDeleteJob: mRoleId is not valid.";
0067         return false;
0068     }
0069     return true;
0070 }
0071 
0072 QNetworkRequest RoleDeleteJob::request() const
0073 {
0074     const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::RolesDelete);
0075     QNetworkRequest request(url);
0076     addAuthRawHeader(request);
0077     addRequestAttribute(request);
0078     return request;
0079 }
0080 
0081 QJsonDocument RoleDeleteJob::json() const
0082 {
0083     QJsonObject jsonObj;
0084     jsonObj[QLatin1String("roleId")] = mRoleId;
0085 
0086     const QJsonDocument postData = QJsonDocument(jsonObj);
0087     return postData;
0088 }
0089 
0090 #include "moc_roledeletejob.cpp"