Debugged changing of the assets
This commit is contained in:
parent
aa815a5bd7
commit
4ed280a550
13
API/api.cpp
13
API/api.cpp
@ -15,6 +15,7 @@
|
||||
#include "requests/currencies.h"
|
||||
#include "requests/addasset.h"
|
||||
#include "requests/deleteasset.h"
|
||||
#include "requests/updateasset.h"
|
||||
|
||||
API::API (Models::Magpie& magpie, QObject* parent):
|
||||
QObject(parent),
|
||||
@ -136,7 +137,17 @@ API::RequestId API::updateAsset (
|
||||
const QColor& color,
|
||||
Models::Currency::ID currency,
|
||||
const QJSValue& finished
|
||||
) {}
|
||||
) {
|
||||
qDebug() << "Updating asset...";
|
||||
if (magpie.getState() != Models::Magpie::Authenticated)
|
||||
return callCallback(finished, "Can not update assets in current state"), 0;
|
||||
|
||||
auto update = std::make_unique<Request::UpdateAsset>(id, title, icon, color, currency, magpie.getAddress());
|
||||
update->setAuthorizationToken(magpie.getAccessToken());
|
||||
connect(update.get(), &Request::UpdateAsset::success, std::bind(&API::callCallback, this, finished, QString(), QJSValueList{QJSValue(true)}));
|
||||
connect(update.get(), &Request::UpdateAsset::error, std::bind(&API::callCallback, this, finished, std::placeholders::_1, QJSValueList{QJSValue(false)}));
|
||||
return registerAndSend(std::move(update));
|
||||
}
|
||||
|
||||
API::RequestId API::deleteAsset (unsigned int id, const QJSValue& finished) {
|
||||
qDebug() << "Deleting asset...";
|
||||
|
@ -47,6 +47,14 @@ public slots:
|
||||
Models::Currency::ID currency,
|
||||
const QJSValue& finished = QJSValue()
|
||||
);
|
||||
RequestId updateAsset(
|
||||
Models::Asset::ID id,
|
||||
const QString& title,
|
||||
const QString& icon,
|
||||
const QColor& color,
|
||||
Models::Currency::ID currency,
|
||||
const QJSValue& finished = QJSValue()
|
||||
);
|
||||
RequestId deleteAsset(unsigned int id, const QJSValue& finished = QJSValue());
|
||||
|
||||
private slots:
|
||||
|
@ -57,6 +57,20 @@ void Models::Assets::remove (Asset::ID id) {
|
||||
endRemoveRows();
|
||||
}
|
||||
|
||||
void Models::Assets::change (const Asset& asset) {
|
||||
int index = getIndexByID(asset.id);
|
||||
if (index == -1)
|
||||
throw std::runtime_error("An attempt to change non existing Asset in asset model");
|
||||
|
||||
records[index] = asset;
|
||||
emit dataChanged(createIndex(index, 0, &records[index]), createIndex(index, 0, &records[index]));
|
||||
}
|
||||
|
||||
void Models::Assets::change (const std::deque<Asset>& assets) {
|
||||
for (const Asset& asset : assets)
|
||||
change(asset);
|
||||
}
|
||||
|
||||
Models::Asset Models::Assets::get (Asset::ID id) const {
|
||||
int index = getIndexByID(id);
|
||||
if (index == -1)
|
||||
@ -163,7 +177,7 @@ QVariantMap Models::Assets::getAssetByIndex (int index) const {
|
||||
return result;
|
||||
|
||||
QModelIndex idx = createIndex(index, 0, &records[index]);
|
||||
for (int role = Roles::Title; role != Roles::ID; ++role)
|
||||
for (int role = Roles::Title; role <= Roles::ID; ++role)
|
||||
result[roles[role]] = data(idx, role);
|
||||
|
||||
return result;
|
||||
|
@ -47,6 +47,8 @@ public:
|
||||
void add (const Asset& asset);
|
||||
void add (const std::deque<Asset>& assets);
|
||||
void remove (Asset::ID id);
|
||||
void change (const Asset& asset);
|
||||
void change (const std::deque<Asset>& assets);
|
||||
|
||||
Asset get(Asset::ID id) const;
|
||||
|
||||
|
@ -221,6 +221,15 @@ bool Models::Magpie::handleAssetChanges (const QVariantMap& changes) {
|
||||
}
|
||||
}
|
||||
|
||||
aItr = changes.constFind("changed");
|
||||
if (aItr != changes.constEnd() && aItr.value().canConvert<QVariantList>()) {
|
||||
std::deque<Models::Asset> changes;
|
||||
if (!Models::Assets::deserialize(qast<QVariantList>(aItr.value()), changes))
|
||||
qDebug() << "Error deserializng changed assets";
|
||||
else
|
||||
assets.change(changes);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -63,6 +63,7 @@ Item {
|
||||
Button {
|
||||
id: editButton
|
||||
text: qsTr("Edit")
|
||||
icon.name: "entry-edit"
|
||||
flat: true
|
||||
height: parent.height
|
||||
onClicked: edit(assetID)
|
||||
@ -71,9 +72,12 @@ Item {
|
||||
Button {
|
||||
id: deleteButton
|
||||
text: qsTr("Delete")
|
||||
flat: true
|
||||
icon.name: "delete"
|
||||
height: parent.height
|
||||
onClicked: remove(assetID)
|
||||
palette { //unfortunately doesn't work anymore
|
||||
button: "red"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user