diff --git a/app/Bootstrap.cpp b/app/Bootstrap.cpp index 72662bc..d0769ba 100644 --- a/app/Bootstrap.cpp +++ b/app/Bootstrap.cpp @@ -1,5 +1,4 @@ #include "Console.h" -#include using namespace std; diff --git a/app/Console.cpp b/app/Console.cpp index abb90d1..66aad5e 100644 --- a/app/Console.cpp +++ b/app/Console.cpp @@ -40,7 +40,7 @@ void Console::showPrompt() void Console::loadActions() { // TODO: load actions via config file - vector actions = { 'q', 'b', 'n', 'y' }; + vector actions = { 'q', 'b', 'n' }; this->actions = actions; } @@ -267,10 +267,6 @@ void Console::takeActionOrNext() case 'n': // TODO: implement pagination break; - case 'y': - // TODO: Decouple this somehow - UserRepository::logOutUser(); - this->renderNextView(string(Console::initialView)); default: break; } diff --git a/app/Controller.cpp b/app/Controller.cpp index 368a5d6..dba3432 100644 --- a/app/Controller.cpp +++ b/app/Controller.cpp @@ -198,7 +198,7 @@ Controller::Controller(char *viewName, string &viewChunk, string &ViewExtension) this->controllerAttributions = {}; this->userInputs = {}; - (this->hasInput(viewChunk) || this->hasOutput(viewChunk)) ? this->prepareView() : this->justShow(); + (this->hasInput(viewChunk) || this->hasOutput(viewChunk) || this->hasAction(viewChunk)) ? this->prepareView() : this->justShow(); } vector& Controller::getControllerAttributions() diff --git a/app/QuestionRepository.cpp b/app/QuestionRepository.cpp index c90f90f..a5cc916 100644 --- a/app/QuestionRepository.cpp +++ b/app/QuestionRepository.cpp @@ -117,13 +117,11 @@ void QuestionRepository::echo(const string &alias) void QuestionRepository::apply(const string &action) { - // Forget the active question when the action is triggered. - // TODO: The changes aren't persisted, why? (the question remains active forever) if (action == "reset") { if (this->model.setActiveIfAny()) { - this->model.markAs("nonactive"); + this->model.markAs("non_active"); } } } @@ -155,7 +153,8 @@ void QuestionRepository::validateItems(map &truncatedInput) { if (truncatedInput.find("action")->second == "create") { - truncatedInput["userId"] = to_string(this->users.setActive().id); + this->users.setActiveIfAny(); + truncatedInput["userId"] = to_string(this->users.getId()); } this->receiveCleanInput(truncatedInput); } diff --git a/app/UserModel.cpp b/app/UserModel.cpp index 37ac2e1..2c67a42 100644 --- a/app/UserModel.cpp +++ b/app/UserModel.cpp @@ -5,6 +5,11 @@ char *UserModel::getFullName() return this->user.full_name; } +int UserModel::getId() +{ + return this->user.id; +} + User UserModel::setAfterUser(string &username) { this->io.seekg(0, this->io.beg); @@ -28,19 +33,23 @@ User UserModel::setAfterId(int id) return this->user; } -User UserModel::setActive() +bool UserModel::setActiveIfAny() { - this->io.seekg(0, this->io.beg); + if (this->user.active == true) + { + return true; + } + this->io.seekg(0, this->io.beg); while (this->io.read(reinterpret_cast(&this->user), sizeof(User))) { if (this->user.active == true) { - return this->user; + return true; } } - throw exception("No active user!"); + return false; } bool UserModel::userExists(string &username) @@ -62,7 +71,10 @@ bool UserModel::userExists(string &username) void UserModel::markAs(const string &status, int id) { - this->setAfterId(id); + if (id != NULL) + { + this->setAfterId(id); + } this->user.active = (status == "active") ? true : false; diff --git a/app/UserModel.h b/app/UserModel.h index dcb2ede..094c823 100644 --- a/app/UserModel.h +++ b/app/UserModel.h @@ -41,12 +41,13 @@ class UserModel : public ModelInterface { User setAfterUser(string &); User setAfterId(int); - User setActive(); + bool setActiveIfAny(); char *getFullName(); + int getId(); bool userExists(string &); - void markAs(const string &, int); + void markAs(const string &, int = NULL); void save(); void setAttributes(map &); diff --git a/app/UserRepository.cpp b/app/UserRepository.cpp index ea9f454..9c60d70 100644 --- a/app/UserRepository.cpp +++ b/app/UserRepository.cpp @@ -104,26 +104,26 @@ void UserRepository::receiveCleanInput(map &cleanInput) // Determine what to output via the model. void UserRepository::echo(const string &alias) { - this->model.setActive(); + this->model.setActiveIfAny(); if (alias == "fullname") { printString(this->model.getFullName()); } + else if (alias == "questions") + { + + } } -void UserRepository::logOutUser() +void UserRepository::apply(const string &action) { - UserRepository users; - - try + if (action == "reset") { - User activeUser = users.model.setActive(); - users.model.markAs(string("logged_out"), activeUser.id); - } - catch (const exception &e) - { - toast(string(e.what()), string("error")); + if (this->model.setActiveIfAny()) + { + this->model.markAs(string("logged_out")); + } } } diff --git a/app/UserRepository.h b/app/UserRepository.h index a436ab5..e532221 100644 --- a/app/UserRepository.h +++ b/app/UserRepository.h @@ -4,6 +4,7 @@ #include "RepositoryInterface.h" #include "UserModel.h" +#include "QuestionModel.h" using namespace std; @@ -12,17 +13,18 @@ class UserRepository : public RepositoryInterface { static string alias; UserModel model; + QuestionModel questions; void defineValidation(); void receiveCleanInput(map &); public: static string &getAlias(); - static void logOutUser(); UserRepository(); void validateItems(map &); void echo(const string &); + void apply(const string &); ~UserRepository(); }; \ No newline at end of file diff --git a/app/View.cpp b/app/View.cpp index 7cc6223..d0017cb 100644 --- a/app/View.cpp +++ b/app/View.cpp @@ -73,7 +73,7 @@ void View::loadViewsOptions() { dashboardView, { { '1', findOrAskView }, { '4', logoutView } } }, - { logoutView, { { 'y', "" }, { 'b', "" } } }, + { logoutView, { { 'y', "home.view" }, { 'b', "" } } }, { faqView, { { 'q', "" }, { 'b', "" } } }, diff --git a/views/home.view b/views/home.view index 182a38f..b8aaa82 100644 --- a/views/home.view +++ b/views/home.view @@ -15,6 +15,7 @@ _\ \ || (_| | (__|