mirror of
https://github.com/Relintai/wp_saver_rcpp_fw.git
synced 2024-11-18 09:07:21 +01:00
Implemented blog listing.
This commit is contained in:
parent
8bd3c03468
commit
0f28f3bb22
2
HEADS
2
HEADS
@ -1 +1 @@
|
||||
{"engine": {"master": "557c2a3934fda8bcc76be3bde525bead5adcaead"}}
|
||||
{"engine": {"master": "86b890eb8f911d1fcdae28dcbe6330f421eeae3d"}}
|
@ -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<WPApplication>(instance);
|
||||
|
||||
@ -46,8 +50,113 @@ 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<PostData *> 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::PostData *> WPApplication::get_posts(Database *db, const int page, const int num_per_page) {
|
||||
Ref<QueryBuilder> 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<QueryResult> res = qb->run();
|
||||
|
||||
Vector<PostData *> 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<QueryBuilder> qb = db->get_query_builder();
|
||||
|
||||
qb->select("COUNT(id)")->from("data")->end_command();
|
||||
Ref<QueryResult> res = qb->run();
|
||||
|
||||
if (!res->next_row()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return res->get_cell_int(0);
|
||||
}
|
||||
|
||||
void WPApplication::routing_middleware(Object *instance, Request *request) {
|
||||
|
@ -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<PostData *> 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();
|
||||
|
Loading…
Reference in New Issue
Block a user