some little refactor, deleting assets
This commit is contained in:
parent
7a116bfdf2
commit
5c4bd18cdc
21 changed files with 139 additions and 36 deletions
|
@ -4,15 +4,11 @@
|
|||
set(HEADERS
|
||||
api.h
|
||||
codes.h
|
||||
finalaction.h
|
||||
helpers.h
|
||||
)
|
||||
|
||||
set(SOURCES
|
||||
api.cpp
|
||||
codes.cpp
|
||||
finalaction.cpp
|
||||
helpers.cpp
|
||||
)
|
||||
|
||||
target_sources(magpie PRIVATE ${SOURCES})
|
||||
|
|
38
API/api.cpp
38
API/api.cpp
|
@ -13,13 +13,13 @@
|
|||
#include "requests/poll.h"
|
||||
#include "requests/listassets.h"
|
||||
#include "requests/addasset.h"
|
||||
#include "requests/deleteasset.h"
|
||||
|
||||
API::API (Models::Magpie& magpie, QObject* parent):
|
||||
QObject(parent),
|
||||
idCounter(0),
|
||||
magpie(magpie),
|
||||
network(),
|
||||
pollReply(),
|
||||
requests()
|
||||
{}
|
||||
|
||||
|
@ -90,18 +90,6 @@ API::RequestId API::sendLogin (const QString& login, const QString& password, co
|
|||
return registerAndSend(std::move(log));
|
||||
}
|
||||
|
||||
API::RequestId API::addAsset (const QString& title, const QString& icon, const QJSValue& finished) {
|
||||
qDebug() << "Adding asset...";
|
||||
if (magpie.getState() != Models::Magpie::Authenticated)
|
||||
return callCallback(finished, "Can not add assets in current state"), 0;
|
||||
|
||||
auto add = std::make_unique<Request::AddAsset>(title, icon, QColor::fromString("black"), 1, magpie.getAddress());
|
||||
add->setAuthorizationToken(magpie.getAccessToken());
|
||||
connect(add.get(), &Request::AddAsset::success, std::bind(&API::callCallback, this, finished, QString(), QJSValueList{QJSValue(true)}));
|
||||
connect(add.get(), &Request::AddAsset::error, std::bind(&API::callCallback, this, finished, std::placeholders::_1, QJSValueList{QJSValue(false)}));
|
||||
return registerAndSend(std::move(add));
|
||||
}
|
||||
|
||||
API::RequestId API::requestAssets (const SuccessListHandler& success, const ErrorHandler& error) {
|
||||
qDebug() << "Requesting assets...";
|
||||
|
||||
|
@ -112,6 +100,30 @@ API::RequestId API::requestAssets (const SuccessListHandler& success, const Erro
|
|||
return registerAndSend(std::move(list));
|
||||
}
|
||||
|
||||
API::RequestId API::addAsset (const QString& title, const QString& icon, const QColor& color, const QJSValue& finished) {
|
||||
qDebug() << "Adding asset...";
|
||||
if (magpie.getState() != Models::Magpie::Authenticated)
|
||||
return callCallback(finished, "Can not add assets in current state"), 0;
|
||||
|
||||
auto add = std::make_unique<Request::AddAsset>(title, icon, color, 1, magpie.getAddress());
|
||||
add->setAuthorizationToken(magpie.getAccessToken());
|
||||
connect(add.get(), &Request::AddAsset::success, std::bind(&API::callCallback, this, finished, QString(), QJSValueList{QJSValue(true)}));
|
||||
connect(add.get(), &Request::AddAsset::error, std::bind(&API::callCallback, this, finished, std::placeholders::_1, QJSValueList{QJSValue(false)}));
|
||||
return registerAndSend(std::move(add));
|
||||
}
|
||||
|
||||
API::RequestId API::deleteAsset (unsigned int id, const QJSValue& finished) {
|
||||
qDebug() << "Deleting asset...";
|
||||
if (magpie.getState() != Models::Magpie::Authenticated)
|
||||
return callCallback(finished, "Can not delete assets in current state"), 0;
|
||||
|
||||
auto del = std::make_unique<Request::DeleteAsset>(id, magpie.getAddress());
|
||||
del->setAuthorizationToken(magpie.getAccessToken());
|
||||
connect(del.get(), &Request::DeleteAsset::success, std::bind(&API::callCallback, this, finished, QString(), QJSValueList{QJSValue(true)}));
|
||||
connect(del.get(), &Request::DeleteAsset::error, std::bind(&API::callCallback, this, finished, std::placeholders::_1, QJSValueList{QJSValue(false)}));
|
||||
return registerAndSend(std::move(del));
|
||||
}
|
||||
|
||||
API::RequestId API::poll (const SuccessMapHandler& success, const ErrorHandler& error, bool clear) {
|
||||
auto poll = std::make_unique<Request::Poll>(magpie.getAddress(), clear);
|
||||
poll->setAuthorizationToken(magpie.getAccessToken());
|
||||
|
|
|
@ -29,7 +29,6 @@ public:
|
|||
|
||||
explicit API(Models::Magpie& magpie, QObject* parent = nullptr);
|
||||
|
||||
|
||||
RequestId requestAssets(const SuccessListHandler& success, const ErrorHandler& error);
|
||||
RequestId poll(const SuccessMapHandler& success, const ErrorHandler& error, bool clear = false);
|
||||
|
||||
|
@ -40,7 +39,8 @@ public slots:
|
|||
RequestId test(const QString& path, const QJSValue& finished = QJSValue());
|
||||
RequestId sendRegister(const QString& login, const QString& password, const QJSValue& finished = QJSValue());
|
||||
RequestId sendLogin(const QString& login, const QString& password, const QJSValue& finished = QJSValue());
|
||||
RequestId addAsset(const QString& title, const QString& icon, const QJSValue& finished = QJSValue());
|
||||
RequestId addAsset(const QString& title, const QString& icon, const QColor& color, const QJSValue& finished = QJSValue());
|
||||
RequestId deleteAsset(unsigned int id, const QJSValue& finished = QJSValue());
|
||||
|
||||
private slots:
|
||||
void onRequestDone(RequestId id);
|
||||
|
@ -54,5 +54,4 @@ private:
|
|||
Models::Magpie& magpie;
|
||||
QNetworkAccessManager network;
|
||||
std::map<RequestId, std::unique_ptr<Request::Request>> requests;
|
||||
std::unique_ptr<QNetworkReply> pollReply;
|
||||
};
|
||||
|
|
|
@ -1,13 +0,0 @@
|
|||
// SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include "finalaction.h"
|
||||
|
||||
FinalAction::FinalAction(const std::function<void()>& action):
|
||||
action(action)
|
||||
{}
|
||||
|
||||
FinalAction::~FinalAction() {
|
||||
action();
|
||||
}
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
// SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
|
||||
class FinalAction {
|
||||
public:
|
||||
FinalAction(const std::function<void()>& action);
|
||||
~FinalAction();
|
||||
|
||||
FinalAction() = delete;
|
||||
FinalAction(const FinalAction&) = delete;
|
||||
FinalAction(FinalAction&&) = delete;
|
||||
FinalAction& operator = (const FinalAction&) = delete;
|
||||
FinalAction& operator = (FinalAction&&) = delete;
|
||||
|
||||
private:
|
||||
std::function<void()> action;
|
||||
};
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
//SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
|
||||
//SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include "helpers.h"
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
//SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
|
||||
//SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QVariant>
|
||||
#include <stdexcept>
|
||||
|
||||
#define UNUSED(X) (void)(X)
|
||||
|
||||
template <class T>
|
||||
const T& qast(const QVariant& variant) {
|
||||
if (variant.userType() == qMetaTypeId<T>())
|
||||
return *reinterpret_cast<const T*>(variant.data());
|
||||
|
||||
throw std::runtime_error("An usuccessfull qast");
|
||||
}
|
|
@ -10,6 +10,7 @@ set(HEADERS
|
|||
poll.h
|
||||
listassets.h
|
||||
addasset.h
|
||||
deleteasset.h
|
||||
)
|
||||
|
||||
set(SOURCES
|
||||
|
@ -21,6 +22,7 @@ set(SOURCES
|
|||
poll.cpp
|
||||
listassets.cpp
|
||||
addasset.cpp
|
||||
deleteasset.cpp
|
||||
)
|
||||
|
||||
target_sources(magpie PRIVATE ${SOURCES})
|
||||
|
|
|
@ -4,7 +4,12 @@
|
|||
#include "addasset.h"
|
||||
|
||||
Request::AddAsset::AddAsset (const QString& title, const QString& icon, const QColor& color, unsigned int currency, const QUrl& baseUrl):
|
||||
Post(createUrl(baseUrl, "/addAsset"), {{"title", title}, {"icon", icon}, {"currency", std::to_string(currency).c_str()}, {"color", "0"}})
|
||||
Post(createUrl(baseUrl, "/addAsset"), {
|
||||
{"title", title},
|
||||
{"icon", icon},
|
||||
{"currency", std::to_string(currency).c_str()},
|
||||
{"color", std::to_string(color.rgba()).c_str()}
|
||||
})
|
||||
{
|
||||
emptyResult = true;
|
||||
}
|
||||
|
|
12
API/requests/deleteasset.cpp
Normal file
12
API/requests/deleteasset.cpp
Normal file
|
@ -0,0 +1,12 @@
|
|||
//SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
|
||||
//SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include "deleteasset.h"
|
||||
|
||||
Request::DeleteAsset::DeleteAsset(unsigned int id, const QUrl &baseUrl):
|
||||
Post(createUrl(baseUrl, "/deleteAsset"), {
|
||||
{"id", std::to_string(id).c_str()}
|
||||
})
|
||||
{
|
||||
emptyResult = true;
|
||||
}
|
20
API/requests/deleteasset.h
Normal file
20
API/requests/deleteasset.h
Normal file
|
@ -0,0 +1,20 @@
|
|||
//SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
|
||||
//SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QObject>
|
||||
|
||||
#include "post.h"
|
||||
|
||||
namespace Request {
|
||||
|
||||
class DeleteAsset : public Post {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
DeleteAsset(unsigned int id, const QUrl& baseUrl);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
#include "listassets.h"
|
||||
|
||||
#include "API/helpers.h"
|
||||
#include "utils/helpers.h"
|
||||
|
||||
Request::ListAssets::ListAssets (const QUrl& baseUrl):
|
||||
Request(createUrl(baseUrl, "/listAssets")) {}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue