diff --git a/dynamic/dynamiccontent.cpp b/dynamic/dynamiccontent.cpp new file mode 100644 index 0000000..eb9eb8f --- /dev/null +++ b/dynamic/dynamiccontent.cpp @@ -0,0 +1,8 @@ +#include "dynamiccontent.h" + +DynamicContent::DynamicContent(Template &templ, Database &database, UrlProvider &provider) +{ + this->templ = &templ; + this->database = &database; + this->urlProvider = &provider; +} diff --git a/dynamic/dynamiccontent.h b/dynamic/dynamiccontent.h new file mode 100644 index 0000000..44505ca --- /dev/null +++ b/dynamic/dynamiccontent.h @@ -0,0 +1,22 @@ +#ifndef DYNAMICCONTENT_H +#define DYNAMICCONTENT_H +#include +#include "../database/database.h" +#include "../template.h" +#include "../urlprovider.h" +class DynamicContent +{ + protected: + Template *templ; + Database *database; + UrlProvider *urlProvider; + + public: + DynamicContent(Template &templ, Database &database, UrlProvider &urlProvider); + virtual std::string render() = 0; + virtual ~DynamicContent() + { + } +}; + +#endif // DYNAMICCONTENT_H diff --git a/dynamic/dynamiccontentpostlist.cpp b/dynamic/dynamiccontentpostlist.cpp new file mode 100644 index 0000000..4b1ce25 --- /dev/null +++ b/dynamic/dynamiccontentpostlist.cpp @@ -0,0 +1,43 @@ +#include +#include "dynamiccontentpostlist.h" + +void DynamicContentPostList::setCategory(std::string catname) +{ + this->catname = catname; +} + +std::string DynamicContentPostList::render() +{ + auto categoryDao = this->database->createCategoryDao(); + auto pageDao = this->database->createPageDao(); + auto revisionDao = this->database->createRevisionDao(); + QueryOption option; + auto members = categoryDao->fetchMembers(this->catname, option); + std::vector> pageList; + for(std::string &member : members) + { + auto revision = revisionDao->getRevisionForPage(member, 1); + pageList.push_back({member, revision->timestamp}); + } + std::sort(pageList.begin(), pageList.end(), + [](std::pair &a, std::pair &b) { return a.second > b.second; }); + + std::string postListBegin = this->templ->loadResolvedPart("dynamic/postlistbegin"); + std::string postListEnd = this->templ->loadResolvedPart("dynamic/postlistend"); + std::string postLink = this->templ->loadResolvedPart("dynamic/postlistlink"); + std::stringstream stream; + stream << postListBegin; + for(auto &pair : pageList) + { + std::string link = this->urlProvider->page(pair.first); + std::string date = utils::toISODate(pair.second); + Varreplacer replacer{"{"}; + replacer.addKeyValue("url", link); + replacer.addKeyValue("date", date); + replacer.addKeyValue("page", pair.first); + + stream << replacer.parse(postLink); + } + stream << postListEnd; + return stream.str(); +} diff --git a/dynamic/dynamiccontentpostlist.h b/dynamic/dynamiccontentpostlist.h new file mode 100644 index 0000000..55d982f --- /dev/null +++ b/dynamic/dynamiccontentpostlist.h @@ -0,0 +1,16 @@ +#ifndef DYNAMICCONTENTPOSTLIST_H +#define DYNAMICCONTENTPOSTLIST_H + +#include "dynamiccontent.h" +class DynamicContentPostList : public DynamicContent +{ + private: + std::string catname; + + public: + using DynamicContent::DynamicContent; + void setCategory(std::string catname); + std::string render() override; +}; + +#endif // DYNAMICCONTENTPOSTLIST_H diff --git a/handlers/handler.h b/handlers/handler.h index 48ed491..9fc70e4 100644 --- a/handlers/handler.h +++ b/handlers/handler.h @@ -1,5 +1,6 @@ #ifndef HANDLER_H #define HANDLER_H +#include #include "../config.h" #include "../response.h" #include "../request.h" @@ -9,6 +10,8 @@ #include "../database/queryoption.h" #include "../logger.h" #include "../cache/icache.h" +#include "../dynamic/dynamiccontent.h" + class Handler { protected: @@ -53,6 +56,12 @@ class Handler virtual ~Handler() { } + + template inline std::shared_ptr createDynamic() + { + return std::make_shared(*this->templ, *this->database, *this->urlProvider); + } + Response errorResponse(std::string errortitle, std::string errormessage, int status = 200); std::string createPageTitle(std::string append); };