diff options
author | Orangerot <purple@orangerot.dev> | 2024-08-15 21:24:28 +0200 |
---|---|---|
committer | Orangerot <purple@orangerot.dev> | 2024-08-15 21:24:28 +0200 |
commit | c09350b7c9adf6327f0a0475c897a71dd23b6cae (patch) | |
tree | aa40a8f5c3aabc28360a0851a5f1220611d888bf /src/model/Sport.cpp | |
parent | f17c2a73e17bb0a2309ae2aba76761f480206656 (diff) |
feat(SportModel): request API, parse and display Model
Diffstat (limited to 'src/model/Sport.cpp')
-rw-r--r-- | src/model/Sport.cpp | 68 |
1 files changed, 67 insertions, 1 deletions
diff --git a/src/model/Sport.cpp b/src/model/Sport.cpp index 21b9b9f..38173ce 100644 --- a/src/model/Sport.cpp +++ b/src/model/Sport.cpp @@ -1,7 +1,9 @@ - #include "Sport.h" // categories +#include <QNetworkReply> +#include <qlogging.h> +#include <qobject.h> #include <set> // sorting and filtering @@ -18,6 +20,70 @@ #include <QJsonValueRef> #include <QString> +namespace { + const QString &k_requestUrl = "https://sph-s-api.olympics.com/summer/schedules/api/ENG/schedule/discipline/ARC"; +} + +SportModel::SportModel(QObject *parent) : QAbstractListModel(parent) { +} + +int SportModel::rowCount(const QModelIndex &parent) const { + Q_UNUSED(parent); + return m_sportList.size(); +} + +QVariant SportModel::data(const QModelIndex &index, int role) const { + if (index.isValid() && index.row() >= 0 && index.row() < m_sportList.size()) { + QString sportName = m_sportList[index.row()]; + + return sportName; + // switch ((Role) role) { + // case SportName: + // } + } + + return {}; +} + + +QHash<int, QByteArray> SportModel::roleNames() const { + QHash<int, QByteArray> names; + names[SportName] = "sportName"; + + return names; +} + +void SportModel::request() { + m_reply = m_networkManager.get(QNetworkRequest( k_requestUrl )); + qDebug() << m_reply; + connect(m_reply, &QNetworkReply::finished, this, &SportModel::parseData); +} + +void SportModel::parseData() { + + if (m_reply->error() == QNetworkReply::NoError) { + beginResetModel(); + // qDeleteAll(m_sportList); + // m_sportList.clear(); + + + + QByteArray strReply = m_reply->readAll(); + + //parse json + // qDebug() << "Response:" << strReply; + QJsonDocument jsonDocument = QJsonDocument::fromJson(strReply); + + QJsonArray sports = jsonDocument["units"].toArray(); + for (const auto &sport : sports) { + QJsonObject entry = sport.toObject(); + qDebug() << entry["eventUnitName"].toString(); + m_sportList << entry["eventUnitName"].toString(); + } + endResetModel(); + } +} + // QJsonArray filter function, provide with input array and evaluation function QJsonArray filter(QJsonArray input, function<bool (QJsonObject)> eval) { QJsonArray output; |