From a14a4c325e755be84e54a82c4b3c5a5b30d5b1b6 Mon Sep 17 00:00:00 2001 From: Tobias Fella Date: Wed, 10 Jun 2020 00:07:08 +0200 Subject: [PATCH] Refactor Entry loading --- src/entry.cpp | 37 +++++++++++++++++++++++++++---------- src/entry.h | 2 +- src/entryListModel.cpp | 35 +---------------------------------- 3 files changed, 29 insertions(+), 45 deletions(-) diff --git a/src/entry.cpp b/src/entry.cpp index a45281ef..c6902ff3 100644 --- a/src/entry.cpp +++ b/src/entry.cpp @@ -25,18 +25,35 @@ #include "database.h" -Entry::Entry(Feed *feed, QString id, QString title, QString content, QVector authors, QDateTime created, QDateTime updated, QString link, bool read, QObject *parent) - : QObject(parent) +Entry::Entry(Feed *feed, int index) + : QObject(nullptr) , m_feed(feed) - , m_id(id) - , m_title(title) - , m_content(content) - , m_authors(authors) - , m_created(created) - , m_updated(updated) - , m_link(link) - , m_read(read) { + QSqlQuery entryQuery; + entryQuery.prepare(QStringLiteral("SELECT * FROM Entries WHERE feed=:feed ORDER BY updated DESC LIMIT 1 OFFSET :index;")); + entryQuery.bindValue(QStringLiteral(":feed"), m_feed->url()); + entryQuery.bindValue(QStringLiteral(":index"), index); + Database::instance().execute(entryQuery); + if (!entryQuery.next()) + qWarning() << "No element with index" << index << "found in feed" << m_feed->url(); + + QSqlQuery authorQuery; + authorQuery.prepare(QStringLiteral("SELECT * FROM Authors WHERE id=:id")); + authorQuery.bindValue(QStringLiteral(":id"), entryQuery.value(QStringLiteral("id")).toString()); + Database::instance().execute(authorQuery); + + while (authorQuery.next()) { + m_authors += new Author(authorQuery.value(QStringLiteral("name")).toString(), authorQuery.value(QStringLiteral("email")).toString(), authorQuery.value(QStringLiteral("uri")).toString(), nullptr); + } + + m_created.setSecsSinceEpoch(entryQuery.value(QStringLiteral("created")).toInt()); + m_updated.setSecsSinceEpoch(entryQuery.value(QStringLiteral("updated")).toInt()); + + m_id = entryQuery.value(QStringLiteral("id")).toString(); + m_title = entryQuery.value(QStringLiteral("title")).toString(); + m_content = entryQuery.value(QStringLiteral("content")).toString(); + m_link = entryQuery.value(QStringLiteral("link")).toString(); + m_read = entryQuery.value(QStringLiteral("read")).toBool(); } Entry::~Entry() diff --git a/src/entry.h b/src/entry.h index 3c9f6160..2b263f9f 100644 --- a/src/entry.h +++ b/src/entry.h @@ -45,7 +45,7 @@ class Entry : public QObject Q_PROPERTY(bool read READ read WRITE setRead NOTIFY readChanged); public: - Entry(Feed *feed, QString id, QString title, QString content, QVector authors, QDateTime created, QDateTime updated, QString link, bool read, QObject *parent = nullptr); + Entry(Feed *feed, int index); ~Entry(); QString id() const; diff --git a/src/entryListModel.cpp b/src/entryListModel.cpp index 2d84f472..25feb22f 100644 --- a/src/entryListModel.cpp +++ b/src/entryListModel.cpp @@ -71,40 +71,7 @@ int EntryListModel::rowCount(const QModelIndex &parent) const void EntryListModel::loadEntry(int index) const { - QSqlQuery entryQuery; - entryQuery.prepare(QStringLiteral("SELECT * FROM Entries WHERE feed=:feed ORDER BY updated DESC LIMIT 1 OFFSET :index;")); - entryQuery.bindValue(QStringLiteral(":feed"), m_feed->url()); - entryQuery.bindValue(QStringLiteral(":index"), index); - Database::instance().execute(entryQuery); - if (!entryQuery.next()) - qWarning() << "No element with index" << index << "found in feed" << m_feed->url(); - - QSqlQuery authorQuery; - authorQuery.prepare(QStringLiteral("SELECT * FROM Authors WHERE id=:id")); - authorQuery.bindValue(QStringLiteral(":id"), entryQuery.value(QStringLiteral("id")).toString()); - Database::instance().execute(authorQuery); - QVector authors; - while (authorQuery.next()) { - authors += new Author(authorQuery.value(QStringLiteral("name")).toString(), authorQuery.value(QStringLiteral("email")).toString(), authorQuery.value(QStringLiteral("uri")).toString(), nullptr); - } - - QDateTime created; - created.setSecsSinceEpoch(entryQuery.value(QStringLiteral("created")).toInt()); - - QDateTime updated; - updated.setSecsSinceEpoch(entryQuery.value(QStringLiteral("updated")).toInt()); - - Entry *entry = new Entry(m_feed, - entryQuery.value(QStringLiteral("id")).toString(), - entryQuery.value(QStringLiteral("title")).toString(), - entryQuery.value(QStringLiteral("content")).toString(), - authors, - created, - updated, - entryQuery.value(QStringLiteral("link")).toString(), - entryQuery.value(QStringLiteral("read")).toBool(), - nullptr); - m_entries[index] = entry; + m_entries[index] = new Entry(m_feed, index); } Feed *EntryListModel::feed() const