Database: PageDao/CategoryDao: Return 'Page' object, not pagename string

This commit is contained in:
Albert S. 2022-04-03 11:11:28 +02:00
parent 5f83981d68
commit 622ef5af6a
9 ha cambiato i file con 43 aggiunte e 27 eliminazioni

Vedi File

@ -141,10 +141,9 @@ std::pair<bool, std::string> CLIHandler::page_list([[maybe_unused]] const std::v
QueryOption o;
auto result = pageDao->getPageList(o);
std::stringstream stream;
for(std::string pagename : result)
for(Page &page : result)
{
Page p = pageDao->find(pagename).value();
stream << p.name << " " << p.pageid << " " << std::string(p.listed ? "listed" : "unlisted") << std::endl;
stream << page.name << " " << page.pageid << " " << std::string(page.listed ? "listed" : "unlisted") << std::endl;
}
return {true, stream.str()};
}
@ -271,9 +270,9 @@ std::pair<bool, std::string> CLIHandler::category_show(const std::vector<std::st
auto categoryDao = this->db->createCategoryDao();
auto members = categoryDao->fetchMembers(args.at(0), QueryOption{});
std::stringstream stream;
for(std::string &member : members)
for(Page &member : members)
{
stream << member << std::endl;
stream << member.name << std::endl;
}
return {true, stream.str()};
}

Vedi File

@ -5,7 +5,7 @@
#include <optional>
#include "queryoption.h"
#include "../category.h"
#include "../page.h"
class CategoryDao
{
public:
@ -14,7 +14,7 @@ class CategoryDao
virtual std::vector<std::string> fetchList(QueryOption o) = 0;
virtual std::optional<Category> find(std::string name) = 0;
virtual void deleteCategory(std::string name) = 0;
virtual std::vector<std::string> fetchMembers(std::string name, QueryOption o) = 0;
virtual std::vector<Page> fetchMembers(std::string name, QueryOption o) = 0;
};
#endif // CATEGORYDAO_H

Vedi File

@ -94,9 +94,10 @@ std::vector<std::string> CategoryDaoSqlite::fetchList(QueryOption o)
}
return result;
}
std::vector<std::string> CategoryDaoSqlite::fetchMembers(std::string name, QueryOption o)
std::vector<Page> CategoryDaoSqlite::fetchMembers(std::string name, QueryOption o)
{
std::vector<std::string> result;
std::vector<Page> result;
SqliteQueryOption queryOption{o};
std::string queryoptions =
@ -104,11 +105,18 @@ std::vector<std::string> CategoryDaoSqlite::fetchMembers(std::string name, Query
try
{
auto query = *db << "SELECT page.name AS name FROM categorymember INNER JOIN page ON page.id = "
auto query = *db << "SELECT page.id, page.name AS name, page.title, page.lastrevision, page.visible FROM categorymember INNER JOIN page ON page.id = "
"categorymember.page WHERE category = (SELECT id FROM category WHERE name = ? ) AND " +
queryoptions
<< name;
query >> [&](std::string p) { result.push_back(p); };
query >> [&](unsigned int id, std::string name, std::string title, unsigned int lastrevision, bool visible) {
Page p;
p.name = name;
p.pageid = id;
p.title = title;
p.current_revision = lastrevision;
p.listed = visible;
result.push_back(p); };
}
catch(const sqlite::exceptions::no_rows &e)
{

Vedi File

@ -3,12 +3,13 @@
#include "categorydao.h"
#include "sqlitedao.h"
#include "../page.h"
class CategoryDaoSqlite : public CategoryDao, protected SqliteDao
{
public:
CategoryDaoSqlite();
std::vector<std::string> fetchList(QueryOption o) override;
std::vector<std::string> fetchMembers(std::string name, QueryOption o) override;
std::vector<Page> fetchMembers(std::string name, QueryOption o) override;
void save(const Category &c) override;
void deleteCategory(std::string name) override;
std::optional<Category> find(std::string name) override;

Vedi File

@ -15,7 +15,7 @@ class PageDao
virtual std::optional<Page> find(std::string name) = 0;
virtual std::optional<Page> findByTitle(std::string title) = 0;
virtual std::optional<Page> find(unsigned int id) = 0;
virtual std::vector<std::string> getPageList(QueryOption option) = 0;
virtual std::vector<Page> getPageList(QueryOption option) = 0;
virtual std::vector<std::string> fetchCategories(std::string pagename, QueryOption option) = 0;
virtual void deletePage(std::string page) = 0;
virtual void save(const Page &page) = 0;

Vedi File

@ -127,21 +127,28 @@ void PageDaoSqlite::save(const Page &page)
throwFrom(e);
}
}
std::vector<std::string> PageDaoSqlite::getPageList(QueryOption option)
std::vector<Page> PageDaoSqlite::getPageList(QueryOption option)
{
std::vector<std::string> result;
std::vector<Page> result;
try
{
std::string queryOption = SqliteQueryOption(option)
.setOrderByColumn("lower(name)")
.setVisibleColumnName("visible")
.setPrependWhere(true)
.build();
std::string query = "SELECT name FROM page " + queryOption;
std::string query = "SELECT id, name, title, lastrevision, visible FROM page " + queryOption;
*db << query >> [&](unsigned int pageid, std::string name, std::string title,unsigned int current_revision, bool visible ) {
*db << query >> [&](std::string name) { result.push_back(name); };
Page tmp;
tmp.pageid = pageid;
tmp.name = name;
tmp.title = title;
tmp.current_revision = current_revision;
tmp.listed = visible;
result.push_back(tmp); };
}
catch(const sqlite::errors::no_rows &e)
{

Vedi File

@ -22,7 +22,7 @@ class PageDaoSqlite : public PageDao, protected SqliteDao
std::optional<Page> find(std::string name) override;
std::optional<Page> findByTitle(std::string title) override;
std::optional<Page> find(unsigned int id) override;
std::vector<std::string> getPageList(QueryOption option) override;
std::vector<Page> getPageList(QueryOption option) override;
std::vector<std::string> fetchCategories(std::string pagename, QueryOption option) override;
using SqliteDao::SqliteDao;
int fetchPageId(std::string pagename);

Vedi File

@ -10,10 +10,10 @@ std::string DynamicContentPostList::render()
option.includeInvisible = false;
auto members = categoryDao->fetchMembers(this->argument, option);
std::vector<std::pair<std::string, time_t>> pageList;
for(std::string &member : members)
for(const Page &member : members)
{
auto revision = revisionDao->getRevisionForPage(member, 1);
pageList.push_back({member, revision->timestamp});
auto revision = revisionDao->getRevisionForPage(member.name, 1);
pageList.push_back({member.name, revision->timestamp});
}
std::sort(pageList.begin(), pageList.end(),
[](std::pair<std::string, time_t> &a, std::pair<std::string, time_t> &b) { return a.second > b.second; });

Vedi File

@ -10,7 +10,9 @@ std::vector<HandlerFeedGenerator::EntryRevisionPair> HandlerFeedGenerator::fetch
QueryOption option;
option.includeInvisible = false;
// option.limit = 20;
std::set<std::string> members;
auto comppage = [](const Page &a, const Page &b) { return a.name == b.name; };
std::set<Page, decltype(comppage)> members (comppage);
if(categories.empty())
{
auto pages = pageDao->getPageList(option);
@ -29,11 +31,10 @@ std::vector<HandlerFeedGenerator::EntryRevisionPair> HandlerFeedGenerator::fetch
std::copy(catmembers.begin(), catmembers.end(), std::inserter(members, members.end()));
}
}
for(const std::string &member : members)
for(const Page &member : members)
{
auto page = pageDao->find(member).value();
auto revision = revisionDao->getRevisionForPage(page.name, 1).value();
result.push_back({page, revision});
auto revision = revisionDao->getRevisionForPage(member.name, 1).value();
result.push_back({member, revision});
}
std::sort(result.begin(), result.end(),
[](EntryRevisionPair &a, EntryRevisionPair &b) { return a.second.timestamp > b.second.timestamp; });