Skip to content

Commit

Permalink
Support for @output strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianfrasineanu committed Nov 28, 2016
1 parent 4ab2691 commit d511b96
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 79 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,6 @@ ModelManifest.xml

# FAKE - F# Make
.fake/

.database/*.store
.database/*.txt
62 changes: 40 additions & 22 deletions app/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//----------------
// Well, just a general controller... not much to see here.
string Controller::userInputString = "@input-";
string Controller::outputString = "@output-";
string Controller::userOutputString = "@output-";
string Controller::actionString = "@action-";

vector<string> Controller::errorBag = {};
Expand All @@ -24,7 +24,7 @@ void Controller::prepareView()
{
this->controllerAttributions.push_back("input");

// Pass the corresposing action to userInputs.
// Pass the corresposing action to the repository.
this->prepareAction(copyChunk);
}
if (this->hasOutput(copyChunk))
Expand All @@ -36,22 +36,21 @@ void Controller::prepareView()
{
if (!this->hasOutput(copyChunk))
{
this->chopChunkAndGetAlias(copyChunk);
this->chopChunkAndGetAlias(copyChunk, "input");
}
else if (!this->hasInput(copyChunk))
{
// To avoid the type issue, let the model to the outputting via a method,
// same way as getting the input, chopping the chunk.
this->chopChunkAndGetAlias(copyChunk, "output");
}
else if (this->hasInput(copyChunk) || this->hasOutput(copyChunk))
{
if (copyChunk.find(Controller::userInputString) < copyChunk.find(Controller::outputString))
if (copyChunk.find(Controller::userInputString) < copyChunk.find(Controller::userOutputString))
{
this->chopChunkAndGetAlias(copyChunk);
this->chopChunkAndGetAlias(copyChunk, "input");
}
else
{
// this->bringDataOnceInChunk()
this->chopChunkAndGetAlias(copyChunk, "output");
}
}
}
Expand All @@ -74,8 +73,7 @@ void Controller::prepareViewInput(const string &subChunk, const string &inputAli
string userInput;
map<string, string> currentInput;

addstr((subChunk + " ").c_str());
refresh();
addstr(subChunk.c_str());

// Hide the input when typing any sensitive data.
if (inputAlias.find("password") != string::npos)
Expand Down Expand Up @@ -110,6 +108,12 @@ void Controller::prepareViewInput(const string &subChunk, const string &inputAli
this->userInputs[inputAlias] = userInput;
}

void Controller::prepareViewOutput(const string &subChunk, const string &outputAlias)
{
addstr(subChunk.c_str());
this->model.render(outputAlias);
}

void Controller::prepareAction(string &chunk)
{
this->userInputs["action"] = chunk.substr(chunk.find(Controller::actionString) + Controller::actionString.size(),
Expand All @@ -119,6 +123,31 @@ void Controller::prepareAction(string &chunk)
(Controller::actionString + this->userInputs["action"]).size() + 1);
}

void Controller::chopChunkAndGetAlias(string &chunk, const string &type)
{
// Splice the alias (take the part after the "-" in the template string).
string alias;

if (type == "input")
{
alias = chunk.substr(chunk.find(Controller::userInputString) + Controller::userInputString.size(),
chunk.find("\n", chunk.find(Controller::userInputString)) - (chunk.find(Controller::userInputString) + Controller::userInputString.size()));
this->prepareViewInput(chunk.substr(0, chunk.find(Controller::userInputString)), alias);
}
else if (type == "output")
{
alias = chunk.substr(chunk.find(Controller::userOutputString) + Controller::userOutputString.size(),
chunk.find_first_of("!?.,)\n ", chunk.find(Controller::userOutputString)) - (chunk.find(Controller::userOutputString) + Controller::userOutputString.size()));
this->prepareViewOutput(chunk.substr(0, chunk.find(Controller::userOutputString)), alias);
}
else
{
throw invalid_argument("No input or output!");
}

chunk.erase(0, chunk.find(alias) + alias.size());
}

void Controller::pushError(string &error)
{
if (error == "")
Expand Down Expand Up @@ -174,17 +203,6 @@ char *Controller::getControllerName()
return this->controllerName;
}

void Controller::chopChunkAndGetAlias(string &chunk)
{
// Splice the alias (take the part after the "-" in the template string).
string inputAlias = chunk.substr(chunk.find(Controller::userInputString) + Controller::userInputString.size(),
chunk.find("\n", chunk.find(Controller::userInputString)) - (chunk.find(Controller::userInputString) + Controller::userInputString.size()));

this->prepareViewInput(chunk.substr(0, chunk.find(Controller::userInputString) - 1), inputAlias);

chunk.erase(0, chunk.find(inputAlias) + inputAlias.size() + 1);
}

// Don't assign it multiple times, stupid.
void Controller::operator=(const Controller &controller)
{
Expand All @@ -203,7 +221,7 @@ bool Controller::hasInput(const string &raw)

bool Controller::hasOutput(const string &raw)
{
return raw.find(Controller::outputString) != string::npos;
return raw.find(Controller::userOutputString) != string::npos;
}

Controller::~Controller()
Expand Down
5 changes: 3 additions & 2 deletions app/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using namespace std;
class Controller {
private:
static string userInputString;
static string outputString;
static string userOutputString;
static string actionString;

static vector<string> errorBag;
Expand All @@ -24,8 +24,9 @@ class Controller {
void justShow();
void prepareView();
void prepareViewInput(const string &, const string &);
void prepareViewOutput(const string &, const string &);
void prepareAction(string &);
void chopChunkAndGetAlias(string &);
void chopChunkAndGetAlias(string &, const string &);
public:
static void pushError(string &);
static vector<string> getErrorBag();
Expand Down
14 changes: 14 additions & 0 deletions app/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ void Model::confirmInput(const map<string, string> &payLoad)
this->repository->validateItems(this->truncatedInput);
}

void Model::render(const string &outputAlias)
{
string entityName = this->parseEntityName(outputAlias),
truncatedAlias = outputAlias;

if (this->repository == NULL)
{
this->attachEntity(entityName);
}

truncatedAlias.erase(0, entityName.size() + 1);
this->repository->echo(truncatedAlias);
}

Model::~Model()
{
delete this->repository;
Expand Down
1 change: 1 addition & 0 deletions app/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Model {
Model();

void confirmInput(const map<string, string> &);
void render(const string &);

~Model();
};
1 change: 1 addition & 0 deletions app/RepositoryInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class RepositoryInterface {
map<string, string> ValidationErrors;
public:
virtual void validateItems(map<string, string> &) = 0;
virtual void echo(const string &) = 0;

virtual ~RepositoryInterface();
};
14 changes: 10 additions & 4 deletions app/UserModel.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#include "UserModel.h"

User UserModel::getAfterUser(string &username)
char *UserModel::getFullName()
{
return this->user.full_name;
}

User UserModel::setAfterUser(string &username)
{
this->io.seekg(0, this->io.beg);

Expand All @@ -17,15 +22,15 @@ User UserModel::getAfterUser(string &username)
throw invalid_argument("Username not found!");
}

User UserModel::getAfterId(int id)
User UserModel::setAfterId(int id)
{
this->io.seekg((id - 1) * sizeof(User), this->io.beg);
this->io.read(reinterpret_cast<char *>(&this->user), sizeof(User));

return this->user;
}

User UserModel::getActive()
User UserModel::setActive()
{
this->io.seekg(0, this->io.beg);

Expand Down Expand Up @@ -59,7 +64,7 @@ bool UserModel::userExists(string &username)

void UserModel::markAs(string &status, int id)
{
this->getAfterId(id);
this->setAfterId(id);

this->user.active = (status == "active") ? true : false;

Expand Down Expand Up @@ -111,6 +116,7 @@ void UserModel::setAttributes(map<string, string> &cleanInputs)
{
time_t t = time(nullptr);
strftime(this->user.created_at, sizeof(this->user.created_at), "%c", localtime(&t));
this->user.active = true;
}

this->user.id = ++this->lastId;
Expand Down
8 changes: 5 additions & 3 deletions app/UserModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ class UserModel : public ModelInterface {
static void dumpFile();
UserModel();

User getAfterUser(string &);
User getAfterId(int);
User getActive();
User setAfterUser(string &);
User setAfterId(int);
User setActive();

char *getFullName();

bool userExists(string &);
void markAs(string &, int);
Expand Down
30 changes: 19 additions & 11 deletions app/UserRepository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@

string UserRepository::alias = "user";

string &UserRepository::getAlias()
{
return UserRepository::alias;
}

UserRepository::UserRepository()
{
this->defineValidation();
}

void UserRepository::defineValidation()
{
// Stand back, I am going to try Regex!
Expand All @@ -24,13 +34,6 @@ void UserRepository::defineValidation()
Controller::pushError(string(""));
}

UserRepository::UserRepository()
{
this->defineValidation();
}

// If the user tries to login, compare the password with the one from the db and mark it as active.
// Determine which action it's taken based on the payload contents.
void UserRepository::receiveCleanInput(map<string, string> &cleanInput)
{
this->model.setAttributes(cleanInput);
Expand All @@ -41,7 +44,7 @@ void UserRepository::receiveCleanInput(map<string, string> &cleanInput)
{
try
{
User user = this->model.getAfterUser(cleanInput.find("username")->second);
User user = this->model.setAfterUser(cleanInput.find("username")->second);
if (!strcmp(user.password, cleanInput.find("password")->second.c_str())
&& !strcmp(user.deleted_at, "")
&& user.banned != true)
Expand Down Expand Up @@ -97,9 +100,14 @@ void UserRepository::receiveCleanInput(map<string, string> &cleanInput)
// TODO: for a ban action, check if the current user has the according privilegies.
}

string &UserRepository::getAlias()
// Determine what to output via the model.
void UserRepository::echo(const string &alias)
{
return UserRepository::alias;
if (alias == "fullname")
{
this->model.setActive();
printString(this->model.getFullName());
}
}

void UserRepository::logOutUser()
Expand All @@ -108,7 +116,7 @@ void UserRepository::logOutUser()

try
{
User activeUser = users.model.getActive();
User activeUser = users.model.setActive();
users.model.markAs(string("logged_out"), activeUser.id);
users.model.save();
}
Expand Down
1 change: 1 addition & 0 deletions app/UserRepository.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class UserRepository : public RepositoryInterface {
UserRepository();

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

~UserRepository();
};
Empty file.
Empty file.
Binary file added database/example.users.store
Binary file not shown.
Binary file modified database/users.store
Binary file not shown.
50 changes: 14 additions & 36 deletions database/users.txt
Original file line number Diff line number Diff line change
@@ -1,53 +1,31 @@
1
Frawda awdawd
awdawdawd@yahoo.com
frawdaw123
Adrianule12@
Mon Nov 28 12:11:22 2016

user
0
0

2
Frasin Frawd
awdawd@yahoo.com
frawd123
Adrianule1!
Mon Nov 28 12:14:39 2016

user
0
0

3
Frasineanu Cristian-Adrian
Frasineanu frac
frawdaw@yahoo.com
frawd2222
farwraw123
Adrianule1!
Mon Nov 28 12:15:27 2016
Mon Nov 28 16:56:31 2016

user
0
0

4
Frasineanu Cristian-Adrian
frasineanu@yahoo.com
frawd1234
2
frasi frasii
frasi@frasi.com
frasi1234
Adrianule1!
Mon Nov 28 12:17:25 2016
Mon Nov 28 16:57:19 2016

user
0
0

5
Frasiii Fras
frac@yahoo.com
fraaac1234
Adr5$32
Mon Nov 28 15:25:36 2016
3
Foo Bar
foo@bar.com
foobar123
Foobar1!
Mon Nov 28 17:00:39 2016

user
0
Expand Down
Loading

0 comments on commit d511b96

Please sign in to comment.