Première tranche verticale : domaine circuit via DataProvider/API
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
#include "dataprovider.h"
|
||||
#include <QNetworkRequest>
|
||||
#include <QNetworkReply>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QUrl>
|
||||
#include <QUrlQuery>
|
||||
|
||||
DataProvider::DataProvider(QObject *parent) : QObject(parent)
|
||||
{
|
||||
}
|
||||
|
||||
void DataProvider::get_familles(const QString &type)
|
||||
{
|
||||
QUrl url(m_baseUrl + "/elements/familles");
|
||||
QUrlQuery query;
|
||||
query.addQueryItem("type", type);
|
||||
url.setQuery(query);
|
||||
|
||||
auto reply = m_manager.get(QNetworkRequest(url));
|
||||
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
|
||||
reply->deleteLater();
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
emit erreur(reply->errorString());
|
||||
return;
|
||||
}
|
||||
const auto doc = QJsonDocument::fromJson(reply->readAll());
|
||||
const auto obj = doc.object();
|
||||
if (obj["status"].toString() != "ok") {
|
||||
emit erreur(obj["message"].toString());
|
||||
return;
|
||||
}
|
||||
emit famillesRecues(obj["familles"].toArray());
|
||||
});
|
||||
}
|
||||
|
||||
void DataProvider::get_elements(const QString &type, int famille)
|
||||
{
|
||||
QUrl url(m_baseUrl + "/elements");
|
||||
QUrlQuery query;
|
||||
query.addQueryItem("type", type);
|
||||
query.addQueryItem("famille", QString::number(famille));
|
||||
url.setQuery(query);
|
||||
|
||||
auto reply = m_manager.get(QNetworkRequest(url));
|
||||
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
|
||||
reply->deleteLater();
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
emit erreur(reply->errorString());
|
||||
return;
|
||||
}
|
||||
const auto doc = QJsonDocument::fromJson(reply->readAll());
|
||||
const auto obj = doc.object();
|
||||
if (obj["status"].toString() != "ok") {
|
||||
emit erreur(obj["message"].toString());
|
||||
return;
|
||||
}
|
||||
emit elementsRecus(obj["elements"].toArray());
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user