Skip to content

Commit

Permalink
Reset action for user (set to active = false on logout rather than do…
Browse files Browse the repository at this point in the history
…ing it manually)
  • Loading branch information
cristianfrasineanu committed Dec 4, 2016
1 parent 3ca8bce commit e1accb0
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 31 deletions.
1 change: 0 additions & 1 deletion app/Bootstrap.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "Console.h"
#include <vld.h>

using namespace std;

Expand Down
6 changes: 1 addition & 5 deletions app/Console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void Console::showPrompt()
void Console::loadActions()
{
// TODO: load actions via config file
vector<char> actions = { 'q', 'b', 'n', 'y' };
vector<char> actions = { 'q', 'b', 'n' };

this->actions = actions;
}
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion app/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>& Controller::getControllerAttributions()
Expand Down
7 changes: 3 additions & 4 deletions app/QuestionRepository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}
Expand Down Expand Up @@ -155,7 +153,8 @@ void QuestionRepository::validateItems(map<string, string> &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);
}
Expand Down
22 changes: 17 additions & 5 deletions app/UserModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<char *>(&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)
Expand All @@ -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;

Expand Down
5 changes: 3 additions & 2 deletions app/UserModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> &);

Expand Down
22 changes: 11 additions & 11 deletions app/UserRepository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,26 +104,26 @@ void UserRepository::receiveCleanInput(map<string, string> &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"));
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion app/UserRepository.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "RepositoryInterface.h"
#include "UserModel.h"
#include "QuestionModel.h"

using namespace std;

Expand All @@ -12,17 +13,18 @@ class UserRepository : public RepositoryInterface {
static string alias;

UserModel model;
QuestionModel questions;

void defineValidation();
void receiveCleanInput(map<string, string> &);
public:
static string &getAlias();
static void logOutUser();

UserRepository();

void validateItems(map<string, string> &);
void echo(const string &);
void apply(const string &);

~UserRepository();
};
2 changes: 1 addition & 1 deletion app/View.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void View::loadViewsOptions()

{ dashboardView, { { '1', findOrAskView }, { '4', logoutView } } },

{ logoutView, { { 'y', "" }, { 'b', "" } } },
{ logoutView, { { 'y', "home.view" }, { 'b', "" } } },

{ faqView, { { 'q', "" }, { 'b', "" } } },

Expand Down
1 change: 1 addition & 0 deletions views/home.view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ _\ \ || (_| | (__| </ ___/| | |_| \__ \/ ___/| | |_| \__ \
-3- Browse Questions as Guest
-4- FAQ
-5- Help
@action-user-reset

*** Press -q- to quit or -b- to go to the previous menu. ***

Expand Down
13 changes: 13 additions & 0 deletions views/user/questions.view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
__ _ _ ___ _ ___ _
/ _\ |_ __ _ ___| | __ / _ \ |_ _ ___ / _ \ |_ _ ___
\ \| __/ _` |/ __| |/ // /_)/ | | | / __| / /_)/ | | | / __|
_\ \ || (_| | (__| </ ___/| | |_| \__ \/ ___/| | |_| \__ \
\__/\__\__,_|\___|_|\_\/ |_|\__,_|___/\/ |_|\__,_|___/

~~~ Here are all your questions:

@output-user-questions

===========================================================
# (c) 2016 Cristian-Adrian Frasineanu | GNU GPLv3 licence #
===========================================================

0 comments on commit e1accb0

Please sign in to comment.