Merge 470ce0f9c8 into 1909d3c94e
This commit is contained in:
commit
ebd3338581
17 changed files with 546 additions and 1 deletions
37
client/ui/controllers/api/apiNewsController.cpp
Normal file
37
client/ui/controllers/api/apiNewsController.cpp
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#include "apiNewsController.h"
|
||||
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
|
||||
ApiNewsController::ApiNewsController(const QSharedPointer<NewsModel> &newsModel,
|
||||
const std::shared_ptr<Settings> &settings,
|
||||
QObject *parent)
|
||||
: QObject(parent), m_newsModel(newsModel), m_settings(settings)
|
||||
{
|
||||
}
|
||||
|
||||
void ApiNewsController::fetchNews()
|
||||
{
|
||||
GatewayController gatewayController(m_settings->getGatewayEndpoint(), m_settings->isDevGatewayEnv(),
|
||||
apiDefs::requestTimeoutMsecs, m_settings->isStrictKillSwitchEnabled());
|
||||
QByteArray responseBody;
|
||||
ErrorCode errorCode = gatewayController.get(QString("%1v1/news"), responseBody);
|
||||
qDebug() << "fetchNews" << errorCode;
|
||||
if (errorCode != ErrorCode::NoError) {
|
||||
emit errorOccurred(errorCode);
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonDocument doc = QJsonDocument::fromJson(responseBody);
|
||||
QJsonArray newsArray;
|
||||
if (doc.isArray()) {
|
||||
newsArray = doc.array();
|
||||
} else if (doc.isObject()) {
|
||||
QJsonObject obj = doc.object();
|
||||
if (obj.value("news").isArray()) {
|
||||
newsArray = obj.value("news").toArray();
|
||||
}
|
||||
}
|
||||
|
||||
m_newsModel->updateModel(newsArray);
|
||||
}
|
||||
32
client/ui/controllers/api/apiNewsController.h
Normal file
32
client/ui/controllers/api/apiNewsController.h
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#ifndef APINEWSCONTROLLER_H
|
||||
#define APINEWSCONTROLLER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QSharedPointer>
|
||||
#include <memory>
|
||||
#include <QJsonArray>
|
||||
|
||||
#include "settings.h"
|
||||
#include "ui/models/newsmodel.h"
|
||||
#include "core/controllers/gatewayController.h"
|
||||
#include "core/api/apiDefs.h"
|
||||
|
||||
class ApiNewsController : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit ApiNewsController(const QSharedPointer<NewsModel> &newsModel,
|
||||
const std::shared_ptr<Settings> &settings,
|
||||
QObject *parent = nullptr);
|
||||
|
||||
Q_INVOKABLE void fetchNews();
|
||||
|
||||
signals:
|
||||
void errorOccurred(ErrorCode errorCode);
|
||||
|
||||
private:
|
||||
QSharedPointer<NewsModel> m_newsModel;
|
||||
std::shared_ptr<Settings> m_settings;
|
||||
};
|
||||
|
||||
#endif // APINEWSCONTROLLER_H
|
||||
Loading…
Add table
Add a link
Reference in a new issue