From 0f28f3bb221f2aa906dd082a0a83c2350ca8825a Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 21 Nov 2021 16:15:29 +0100 Subject: [PATCH] Implemented blog listing. --- HEADS | 2 +- app/wp_application.cpp | 111 ++++++++++++++++++++++++++++++++++++++++- app/wp_application.h | 12 ++++- 3 files changed, 121 insertions(+), 4 deletions(-) diff --git a/HEADS b/HEADS index 67a29d8..db7b116 100644 --- a/HEADS +++ b/HEADS @@ -1 +1 @@ -{"engine": {"master": "557c2a3934fda8bcc76be3bde525bead5adcaead"}} \ No newline at end of file +{"engine": {"master": "86b890eb8f911d1fcdae28dcbe6330f421eeae3d"}} \ No newline at end of file diff --git a/app/wp_application.cpp b/app/wp_application.cpp index c079ac7..b9a5dbe 100644 --- a/app/wp_application.cpp +++ b/app/wp_application.cpp @@ -9,11 +9,15 @@ #include "core/http/handler_instance.h" #include "core/database/database_manager.h" +#include "core/database/query_builder.h" +#include "core/database/query_result.h" #include "core/html/html_builder.h" #include "core/http/http_session.h" #include "core/http/session_manager.h" +#include "core/utils.h" + void WPApplication::index_fun(Object *instance, Request *request) { WPApplication *app = Object::cast_to(instance); @@ -46,10 +50,115 @@ void WPApplication::index(Request *request) { } void WPApplication::blog(Request *request) { - request->body += "test blog"; + String blog = request->get_current_path_segment(); + + Database *db = nullptr; + + for (int i = 0; i < _blog_data.size(); ++i) { + BlogData &bd = _blog_data[i]; + + if (bd.name == blog) { + db = bd.db; + break; + } + } + + if (!db) { + request->send_error(404); + return; + } + + request->push_path(); + + HTMLBuilder b; + + int page = 1; + + String action_segment = request->get_current_path_segment(); + + if (action_segment == "") { + // nothign to do + } else if (action_segment == "page") { + request->push_path(); + + page = request->get_current_path_segment().to_int(); + } else if (action_segment == "post") { + request->body += "test blog post"; + request->compile_and_send_body(); + return; + } else { + request->send_error(404); + return; + } + + if (page <= 0) { + page = 1; + } + + --page; + + Vector posts = get_posts(db, page); + int post_count = (get_post_count(db) / 5.0) + 0.9; + + b.div("content"); + + b.w(Utils::get_pagination("/blog/" + blog + "/page/", post_count, page)); + + if (posts.size() != 0) { + for (int i = 0; i < posts.size(); ++i) { + PostData *p = posts[i]; + + b.div("blog_content_row"); + b.div("blog_entry_link")->f()->fa(request->get_url_root_current() + "post/" + String::num(p->id), "Open")->cdiv(); + b.w(p->data); + b.cdiv(); + } + } else { + b.fdiv("No saved blog posts.", "blog_content_row"); + } + + b.w(Utils::get_pagination("/blog/" + blog + "/page/", post_count, page)); + + b.cdiv(); + + request->body += b.result; request->compile_and_send_body(); } +Vector WPApplication::get_posts(Database *db, const int page, const int num_per_page) { + Ref qb = db->get_query_builder(); + + qb->select("id,url,extracted_data")->from("data")->order_by_desc("id")->corder_by()->limit(num_per_page)->offset(page * num_per_page)->end_command(); + Ref res = qb->run(); + + Vector r; + + while (res->next_row()) { + PostData *p = new PostData(); + + p->id = res->get_cell_int(0); + p->url = res->get_cell(1); + p->data = res->get_cell(2); + + r.push_back(p); + } + + return r; +} + +int WPApplication::get_post_count(Database *db) { + Ref qb = db->get_query_builder(); + + qb->select("COUNT(id)")->from("data")->end_command(); + Ref res = qb->run(); + + if (!res->next_row()) { + return 0; + } + + return res->get_cell_int(0); +} + void WPApplication::routing_middleware(Object *instance, Request *request) { String path = request->get_path_full(); diff --git a/app/wp_application.h b/app/wp_application.h index 3d6e47b..17a730d 100644 --- a/app/wp_application.h +++ b/app/wp_application.h @@ -6,8 +6,7 @@ #include "core/string.h" #include "modules/drogon/web_application.h" -#undef LOG_TRACE -#undef LOG_WARN +class Database; class WPApplication : public DWebApplication { RCPP_OBJECT(WPApplication, DWebApplication); @@ -19,6 +18,15 @@ public: void index(Request *request); void blog(Request *request); + struct PostData { + int id; + String url; + String data; + }; + + Vector get_posts(Database *db, const int page, const int num_per_page = 5); + int get_post_count(Database *db); + static void routing_middleware(Object *instance, Request *request); virtual void setup_routes();