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 changed files with 43 additions and 27 deletions

View File

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

View File

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

View File

@ -94,9 +94,10 @@ std::vector<std::string> CategoryDaoSqlite::fetchList(QueryOption o)
} }
return result; 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}; SqliteQueryOption queryOption{o};
std::string queryoptions = std::string queryoptions =
@ -104,11 +105,18 @@ std::vector<std::string> CategoryDaoSqlite::fetchMembers(std::string name, Query
try 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 " + "categorymember.page WHERE category = (SELECT id FROM category WHERE name = ? ) AND " +
queryoptions queryoptions
<< name; << 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) catch(const sqlite::exceptions::no_rows &e)
{ {

View File

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

View File

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

View File

@ -127,21 +127,28 @@ void PageDaoSqlite::save(const Page &page)
throwFrom(e); 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 try
{ {
std::string queryOption = SqliteQueryOption(option) std::string queryOption = SqliteQueryOption(option)
.setOrderByColumn("lower(name)") .setOrderByColumn("lower(name)")
.setVisibleColumnName("visible") .setVisibleColumnName("visible")
.setPrependWhere(true) .setPrependWhere(true)
.build(); .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) catch(const sqlite::errors::no_rows &e)
{ {

View File

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

View File

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

View File

@ -10,7 +10,9 @@ std::vector<HandlerFeedGenerator::EntryRevisionPair> HandlerFeedGenerator::fetch
QueryOption option; QueryOption option;
option.includeInvisible = false; option.includeInvisible = false;
// option.limit = 20; // 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()) if(categories.empty())
{ {
auto pages = pageDao->getPageList(option); 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())); 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(member.name, 1).value();
auto revision = revisionDao->getRevisionForPage(page.name, 1).value(); result.push_back({member, revision});
result.push_back({page, revision});
} }
std::sort(result.begin(), result.end(), std::sort(result.begin(), result.end(),
[](EntryRevisionPair &a, EntryRevisionPair &b) { return a.second.timestamp > b.second.timestamp; }); [](EntryRevisionPair &a, EntryRevisionPair &b) { return a.second.timestamp > b.second.timestamp; });