mirror of
https://github.com/KDE/kasts.git
synced 2024-12-24 15:40:44 +01:00
Refactor Entry loading
This commit is contained in:
parent
846437dbcb
commit
a14a4c325e
@ -25,18 +25,35 @@
|
||||
|
||||
#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)
|
||||
: 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()
|
||||
|
@ -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<Author *> authors, QDateTime created, QDateTime updated, QString link, bool read, QObject *parent = nullptr);
|
||||
Entry(Feed *feed, int index);
|
||||
~Entry();
|
||||
|
||||
QString id() const;
|
||||
|
@ -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<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;
|
||||
m_entries[index] = new Entry(m_feed, index);
|
||||
}
|
||||
|
||||
Feed *EntryListModel::feed() const
|
||||
|
Loading…
Reference in New Issue
Block a user