Refactor Entry loading

This commit is contained in:
Tobias Fella 2020-06-10 00:07:08 +02:00
parent 846437dbcb
commit a14a4c325e
3 changed files with 29 additions and 45 deletions

View File

@ -25,18 +25,35 @@
#include "database.h" #include "database.h"
Entry::Entry(Feed *feed, QString id, QString title, QString content, QVector<Author *> authors, QDateTime created, QDateTime updated, QString link, bool read, QObject *parent) Entry::Entry(Feed *feed, int index)
: QObject(parent) : QObject(nullptr)
, m_feed(feed) , 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() Entry::~Entry()

View File

@ -45,7 +45,7 @@ class Entry : public QObject
Q_PROPERTY(bool read READ read WRITE setRead NOTIFY readChanged); Q_PROPERTY(bool read READ read WRITE setRead NOTIFY readChanged);
public: public:
Entry(Feed *feed, QString id, QString title, QString content, QVector<Author *> authors, QDateTime created, QDateTime updated, QString link, bool read, QObject *parent = nullptr); Entry(Feed *feed, int index);
~Entry(); ~Entry();
QString id() const; QString id() const;

View File

@ -71,40 +71,7 @@ int EntryListModel::rowCount(const QModelIndex &parent) const
void EntryListModel::loadEntry(int index) const void EntryListModel::loadEntry(int index) const
{ {
QSqlQuery entryQuery; m_entries[index] = new Entry(m_feed, index);
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<Author *> 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;
} }
Feed *EntryListModel::feed() const Feed *EntryListModel::feed() const