From 5b31cb7df8adf8c79cab15a994e5ee401d6e2967 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 31 Oct 2021 02:13:01 +0200 Subject: [PATCH] Added a new UserController and added the new render methods from the core USerController. --- HEADS | 2 +- app/mourne_user_controller.cpp | 182 +++++++++++++++++++++++++++++++++ app/mourne_user_controller.h | 23 +++++ main.cpp | 4 +- 4 files changed, 208 insertions(+), 3 deletions(-) create mode 100644 app/mourne_user_controller.cpp create mode 100644 app/mourne_user_controller.h diff --git a/HEADS b/HEADS index e70e408..9fcf904 100644 --- a/HEADS +++ b/HEADS @@ -1 +1 @@ -{"engine": {"master": "6aae520ed45117c7ad956b2c8cfbd809d1ac8e8c"}} \ No newline at end of file +{"engine": {"master": "594ab744c52b4697464a49bf09f9895d97eb8723"}} \ No newline at end of file diff --git a/app/mourne_user_controller.cpp b/app/mourne_user_controller.cpp new file mode 100644 index 0000000..0474523 --- /dev/null +++ b/app/mourne_user_controller.cpp @@ -0,0 +1,182 @@ +#include "mourne_user_controller.h" + +#include "core/html/form_validator.h" +#include "core/html/html_builder.h" +#include "core/http/cookie.h" +#include "core/http/http_session.h" +#include "core/http/request.h" +#include "core/http/session_manager.h" +#include "modules/users/user_model.h" + +void MourneUserController::render_login_request_default(Request *request, LoginRequestData *data) { + HTMLBuilder b; + + b.w("Login"); + b.br(); + + { + if (data->error_str.size() != 0) { + b.div()->cls("error"); + + b.w(data->error_str); + + b.cdiv(); + } + } + + b.div()->cls("login"); + { + + //todo href path helper + b.form()->method("POST")->href("/user/login"); + { + b.w("Username"); + b.br(); + b.input()->type("text")->name("username")->value(data->uname_val); + b.cinput(); + b.br(); + + b.w("Password"); + b.br(); + b.input()->type("password")->name("password"); + b.cinput(); + b.br(); + + b.input()->type("submit")->value("Send"); + b.cinput(); + } + b.cform(); + } + b.cdiv(); + + request->body += b.result; + + request->compile_and_send_body(); +} + +void MourneUserController::render_register_request_default(Request *request, RegisterRequestData *data) { + HTMLBuilder b; + + b.w("Registration"); + b.br(); + + { + if (data->error_str.size() != 0) { + b.div()->cls("error"); + + b.w(data->error_str); + + b.cdiv(); + } + } + + b.div()->cls("register"); + { + //todo href path helper + b.form()->method("POST")->href("/user/register"); + { + b.w("Username"); + b.br(); + b.input()->type("text")->name("username")->value(data->uname_val); + b.cinput(); + b.br(); + + b.w("Email"); + b.br(); + b.input()->type("email")->name("email")->value(data->email_val); + b.cinput(); + b.br(); + + b.w("Password"); + b.br(); + b.input()->type("password")->name("password"); + b.cinput(); + b.br(); + + b.w("Password again"); + b.br(); + b.input()->type("password")->name("password_check"); + b.cinput(); + b.br(); + + b.input()->type("submit")->value("Register"); + b.cinput(); + } + b.cform(); + } + b.cdiv(); + + request->body += b.result; + + request->compile_and_send_body(); +} + +void MourneUserController::render_already_logged_in_error(Request *request) { + request->body += "You are already logged in."; + + request->compile_and_send_body(); +} + +void MourneUserController::render_settings_request(Ref &user, Request *request, SettingsRequestData *data) { + HTMLBuilder b; + + b.w("Settings"); + b.br(); + + { + if (data->error_str.size() != 0) { + b.div()->cls("error"); + + b.w(data->error_str); + + b.cdiv(); + } + } + + b.div()->cls("settings"); + { + //todo href path helper + b.form()->method("POST")->href("/user/settings"); + { + b.w("Username"); + b.br(); + b.input()->type("text")->name("username")->placeholder(user->name_user_input)->value(data->uname_val); + b.cinput(); + b.br(); + + b.w("Email"); + b.br(); + b.input()->type("email")->name("email")->placeholder(user->email_user_input)->value(data->email_val); + b.cinput(); + b.br(); + + b.w("Password"); + b.br(); + b.input()->type("password")->placeholder("*******")->name("password"); + b.cinput(); + b.br(); + + b.w("Password again"); + b.br(); + b.input()->type("password")->placeholder("*******")->name("password_check"); + b.cinput(); + b.br(); + + b.input()->type("submit")->value("Save"); + b.cinput(); + } + b.cform(); + } + b.cdiv(); + + request->body += b.result; + + request->compile_and_send_body(); +} + +MourneUserController::MourneUserController() : + UserController() { +} + +MourneUserController::~MourneUserController() { +} diff --git a/app/mourne_user_controller.h b/app/mourne_user_controller.h new file mode 100644 index 0000000..31d8873 --- /dev/null +++ b/app/mourne_user_controller.h @@ -0,0 +1,23 @@ +#ifndef MOURNE_USER_CONTROLLER_H +#define MOURNE_USER_CONTROLLER_H + +#include "modules/users/user_controller.h" + +#include +#include "modules/users/user.h" + +class Request; +class FormValidator; + +class MourneUserController : public UserController { +public: + void render_login_request_default(Request *request, LoginRequestData *data); + void render_register_request_default(Request *request, RegisterRequestData *data); + void render_already_logged_in_error(Request *request); + void render_settings_request(Ref &user, Request *request, SettingsRequestData *data); + + MourneUserController(); + ~MourneUserController(); +}; + +#endif \ No newline at end of file diff --git a/main.cpp b/main.cpp index 28b6683..bac64cd 100644 --- a/main.cpp +++ b/main.cpp @@ -22,7 +22,7 @@ #include "backends/hash_hashlib/setup.h" #include "modules/users/user.h" -#include "modules/users/user_controller.h" +#include "app/mourne_user_controller.h" #include "modules/users/user_model.h" #include "core/database/database_manager.h" @@ -56,7 +56,7 @@ int main(int argc, char **argv) { ::SessionManager *session_manager = new ::SessionManager(); //todo init these in the module automatically - UserController *user_controller = new UserController(); + UserController *user_controller = new MourneUserController(); UserModel *user_model = new UserModel(); //user_manager->set_path("./users/");